summaryrefslogtreecommitdiffstats
path: root/devtools/client/aboutdebugging/test/browser/resources/service-workers/controlled-sw.js
blob: 0a6d9cfdc667d46b65ecb7f9de168c3c67f507d7 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/* eslint-env worker */

"use strict";

// Copied from shared-head.js
function waitUntil(predicate, interval = 10) {
  if (predicate()) {
    return Promise.resolve(true);
  }
  return new Promise(resolve => {
    setTimeout(function () {
      waitUntil(predicate, interval).then(() => resolve(true));
    }, interval);
  });
}

// This flag will be flipped from controlled-sw.html::installServiceWorker()
let canInstall = false;
self.addEventListener("message", function (event) {
  if (event.data === "install-service-worker") {
    canInstall = true;
  }
});

// Wait for the canInstall flag to be flipped before completing the install.
self.addEventListener("install", function (event) {
  event.waitUntil(waitUntil(() => canInstall));
});