blob: f1997bd824e8e165cc119a52082d742bd63f4e4d (
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
'use strict';
const installEventFired = new Promise(resolve => {
self.fireInstallEvent = resolve;
});
const installFinished = new Promise(resolve => {
self.finishInstall = resolve;
});
addEventListener('install', event => {
fireInstallEvent();
event.waitUntil(installFinished);
});
addEventListener('message', event => {
let resolveWaitUntil;
event.waitUntil(new Promise(resolve => { resolveWaitUntil = resolve; }));
// Use a dedicated MessageChannel for every request so senders can wait for
// individual requests to finish, and concurrent requests (to different
// workers) don't cause race conditions.
const port = event.data;
port.onmessage = (event) => {
switch (event.data) {
case 'awaitInstallEvent':
installEventFired.then(() => {
port.postMessage('installEventFired');
}).finally(resolveWaitUntil);
break;
case 'finishInstall':
installFinished.then(() => {
port.postMessage('installFinished');
}).finally(resolveWaitUntil);
finishInstall();
break;
case 'callUpdate': {
const channel = new MessageChannel();
registration.update().then(() => {
channel.port2.postMessage({
success: true,
});
}).catch((exception) => {
channel.port2.postMessage({
success: false,
exception: exception.name,
});
}).finally(resolveWaitUntil);
port.postMessage(channel.port1, [channel.port1]);
break;
}
default:
port.postMessage('Unexpected command ' + event.data);
resolveWaitUntil();
break;
}
};
});
|