summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/test/mochitest/examples/doc-all-workers.html
blob: bf6fa2426237cd4da39722f27d53ff381980cde0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<script>
var worker1 = new Worker("simple-worker.js");
var worker2 = new SharedWorker("shared-worker.js");

registerServiceWorker();

async function registerServiceWorker() {
  const sw = navigator.serviceWorker;
  const swr = await sw.register("service-worker.sjs", { scope: "" });

  await new Promise(resolve => {
    const worker = swr.installing || swr.waiting || swr.active;
    if (worker.state === 'activated') {
      return resolve();
    }

    worker.addEventListener('statechange', () => {
      if (worker.state === 'activated') {
        return resolve();
      }
    });
  });

  await fetch("whatever");
}

async function unregisterServiceWorker() {
  const sw = navigator.serviceWorker;
  const swr = await sw.register("service-worker.sjs", { scope: "" });
  return swr.unregister();
}
</script>