blob: 20a9eb023f6213c793e148b2eefba7eab81ce79c (
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
var pendingPorts = [];
var portResolves = [];
onmessage = function(e) {
var message = e.data;
if ('port' in message) {
var resolve = self.portResolves.shift();
if (resolve)
resolve(message.port);
else
self.pendingPorts.push(message.port);
}
};
function fulfillPromise() {
return new Promise(function(resolve) {
// Make sure the oninstall/onactivate callback returns first.
Promise.resolve().then(function() {
var port = self.pendingPorts.shift();
if (port)
resolve(port);
else
self.portResolves.push(resolve);
});
}).then(function(port) {
port.postMessage('SYNC');
return new Promise(function(resolve) {
port.onmessage = function(e) {
if (e.data == 'ACK')
resolve();
};
});
});
}
function rejectPromise() {
return new Promise(function(resolve, reject) {
// Make sure the oninstall/onactivate callback returns first.
Promise.resolve().then(reject);
});
}
function stripScopeName(url) {
return url.split('/').slice(-1)[0];
}
oninstall = function(e) {
switch (stripScopeName(self.location.href)) {
case 'install-fulfilled':
e.waitUntil(fulfillPromise());
break;
case 'install-rejected':
e.waitUntil(rejectPromise());
break;
case 'install-multiple-fulfilled':
e.waitUntil(fulfillPromise());
e.waitUntil(fulfillPromise());
break;
case 'install-reject-precedence':
// Three "extend lifetime promises" are needed to verify that the user
// agent waits for all promises to settle even in the event of rejection.
// The first promise is fulfilled on demand by the client, the second is
// immediately scheduled for rejection, and the third is fulfilled on
// demand by the client (but only after the first promise has been
// fulfilled).
//
// User agents which simply expose `Promise.all` semantics in this case
// (by entering the "redundant state" following the rejection of the
// second promise but prior to the fulfillment of the third) can be
// identified from the client context.
e.waitUntil(fulfillPromise());
e.waitUntil(rejectPromise());
e.waitUntil(fulfillPromise());
break;
}
};
onactivate = function(e) {
switch (stripScopeName(self.location.href)) {
case 'activate-fulfilled':
e.waitUntil(fulfillPromise());
break;
case 'activate-rejected':
e.waitUntil(rejectPromise());
break;
}
};
|