Skip to content

Commit

Permalink
feat(Worker): waitUntil and respondWith must be bound to e in order t…
Browse files Browse the repository at this point in the history
…o work
  • Loading branch information
gja committed Jan 4, 2019
1 parent 7ccf5cd commit 06bc243
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions app/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,28 @@ function buildRequest(url, opts) {
return new Request(request, { headers });
}

class FetchEvent {
constructor(request) {
this.responsePromise = null;
this.waitEvents = [];
this.type = "fetch";
this.request = request;
}

waitUntil(e) {
this.waitEvents.push(e);
}

respondWith(e) {
this.responsePromise = e;
}

async __response() {
const [response, ...others] = await Promise.all([this.responsePromise].concat(this.waitEvents));
return response;
}
}

class Worker {
constructor(origin, workerContents, opts = {}) {
const { upstreamHost, kvStores = {} } = opts;
Expand Down Expand Up @@ -86,16 +108,9 @@ class Worker {
}

async executeFetchEvent(url, opts = {}) {
let responsePromise = null;
let waitUntil = [];
this.triggerEvent("fetch", {
type: "fetch",
request: buildRequest(url, opts),
respondWith: r => (responsePromise = r),
waitUntil: e => waitUntil.push(e)
});
const [response, ...others] = await Promise.all([responsePromise].concat(waitUntil));
return response;
const fetchEvent = new FetchEvent(buildRequest(url, opts));
this.triggerEvent("fetch", fetchEvent);
return fetchEvent.__response();
}

addEventListener(event, listener) {
Expand Down

0 comments on commit 06bc243

Please sign in to comment.