summaryrefslogtreecommitdiffstats
path: root/dom/push/test/lifetime_worker.js
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /dom/push/test/lifetime_worker.js
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'dom/push/test/lifetime_worker.js')
-rw-r--r--dom/push/test/lifetime_worker.js85
1 files changed, 85 insertions, 0 deletions
diff --git a/dom/push/test/lifetime_worker.js b/dom/push/test/lifetime_worker.js
new file mode 100644
index 000000000..46e713f4e
--- /dev/null
+++ b/dom/push/test/lifetime_worker.js
@@ -0,0 +1,85 @@
+var state = "from_scope";
+var resolvePromiseCallback;
+
+onfetch = function(event) {
+ if (event.request.url.indexOf("lifetime_frame.html") >= 0) {
+ event.respondWith(new Response("iframe_lifetime"));
+ return;
+ }
+
+ var currentState = state;
+ event.waitUntil(
+ clients.matchAll()
+ .then(clients => {
+ clients.forEach(client => {
+ client.postMessage({type: "fetch", state: currentState});
+ });
+ })
+ );
+
+ if (event.request.url.indexOf("update") >= 0) {
+ state = "update";
+ } else if (event.request.url.indexOf("wait") >= 0) {
+ event.respondWith(new Promise(function(res, rej) {
+ if (resolvePromiseCallback) {
+ dump("ERROR: service worker was already waiting on a promise.\n");
+ }
+ resolvePromiseCallback = function() {
+ res(new Response("resolve_respondWithPromise"));
+ };
+ }));
+ state = "wait";
+ } else if (event.request.url.indexOf("release") >= 0) {
+ state = "release";
+ resolvePromise();
+ }
+}
+
+function resolvePromise() {
+ if (resolvePromiseCallback === undefined || resolvePromiseCallback == null) {
+ dump("ERROR: wait promise was not set.\n");
+ return;
+ }
+ resolvePromiseCallback();
+ resolvePromiseCallback = null;
+}
+
+onmessage = function(event) {
+ var lastState = state;
+ state = event.data;
+ if (state === 'wait') {
+ event.waitUntil(new Promise(function(res, rej) {
+ if (resolvePromiseCallback) {
+ dump("ERROR: service worker was already waiting on a promise.\n");
+ }
+ resolvePromiseCallback = res;
+ }));
+ } else if (state === 'release') {
+ resolvePromise();
+ }
+ event.source.postMessage({type: "message", state: lastState});
+}
+
+onpush = function(event) {
+ var pushResolve;
+ event.waitUntil(new Promise(function(resolve) {
+ pushResolve = resolve;
+ }));
+
+ // FIXME(catalinb): push message carry no data. So we assume the only
+ // push message we get is "wait"
+ clients.matchAll().then(function(client) {
+ if (client.length == 0) {
+ dump("ERROR: no clients to send the response to.\n");
+ }
+
+ client[0].postMessage({type: "push", state: state});
+
+ state = "wait";
+ if (resolvePromiseCallback) {
+ dump("ERROR: service worker was already waiting on a promise.\n");
+ } else {
+ resolvePromiseCallback = pushResolve;
+ }
+ });
+}