diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /dom/push/test/lifetime_worker.js | |
parent | Initial commit. (diff) | |
download | thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/push/test/lifetime_worker.js')
-rw-r--r-- | dom/push/test/lifetime_worker.js | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/dom/push/test/lifetime_worker.js b/dom/push/test/lifetime_worker.js new file mode 100644 index 0000000000..02c09d966e --- /dev/null +++ b/dom/push/test/lifetime_worker.js @@ -0,0 +1,90 @@ +var state = "from_scope"; +var resolvePromiseCallback; + +self.onfetch = function (event) { + if (event.request.url.includes("lifetime_frame.html")) { + event.respondWith(new Response("iframe_lifetime")); + return; + } + + var currentState = state; + event.waitUntil( + self.clients.matchAll().then(clients => { + clients.forEach(client => { + client.postMessage({ type: "fetch", state: currentState }); + }); + }) + ); + + if (event.request.url.includes("update")) { + state = "update"; + } else if (event.request.url.includes("wait")) { + 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.includes("release")) { + state = "release"; + resolvePromise(); + } +}; + +function resolvePromise() { + if (resolvePromiseCallback === undefined || resolvePromiseCallback == null) { + dump("ERROR: wait promise was not set.\n"); + return; + } + resolvePromiseCallback(); + resolvePromiseCallback = null; +} + +self.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 }); +}; + +self.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" + self.clients.matchAll().then(function (client) { + if (!client.length) { + dump("ERROR: no clients to send the response to.\n"); + } + + client[0].postMessage({ type: "push", state }); + + state = "wait"; + if (resolvePromiseCallback) { + dump("ERROR: service worker was already waiting on a promise.\n"); + } else { + resolvePromiseCallback = pushResolve; + } + }); +}; |