forked from GoogleChrome/workbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request GoogleChrome#1985 from GoogleChrome/migrate-google…
…-analytics-tests Migrate google analytics tests to run in a SW environment
- Loading branch information
Showing
3 changed files
with
191 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
Copyright 2018 Google LLC | ||
Use of this source code is governed by an MIT-style | ||
license that can be found in the LICENSE file or at | ||
https://opensource.org/licenses/MIT. | ||
*/ | ||
|
||
|
||
const extendLifetimePromises = new WeakMap(); | ||
|
||
export const eventDoneWaiting = async (event) => { | ||
const promises = extendLifetimePromises.get(event); | ||
let promise; | ||
|
||
while (promise = promises.shift()) { | ||
// Ignore errors. | ||
await promise.catch((e) => e); | ||
} | ||
}; | ||
|
||
export const watchEvent = (event) => { | ||
const promises = []; | ||
extendLifetimePromises.set(event, promises); | ||
|
||
event.waitUntil = (promise) => { | ||
promises.push(promise); | ||
}; | ||
|
||
if (event instanceof FetchEvent) { | ||
event.respondWith = (responseOrPromise) => { | ||
promises.push(Promise.resolve(responseOrPromise)); | ||
|
||
// TODO(philipwalton): we cannot currently call the native | ||
// `respondWith()` due to this bug in Firefix: | ||
// https://bugzilla.mozilla.org/show_bug.cgi?id=1538756 | ||
// FetchEvent.prototype.respondWith.call(event, responseOrPromise); | ||
}; | ||
} | ||
}; | ||
|
||
export const dispatchAndWaitUntilDone = async (event) => { | ||
watchEvent(event); | ||
self.dispatchEvent(event); | ||
await eventDoneWaiting(event); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.