4 onmessage = function(e) {
6 if ('port' in message) {
7 var resolve = self.portResolves.shift();
11 self.pendingPorts.push(message.port);
15 function fulfillPromise() {
16 return new Promise(function(resolve) {
17 // Make sure the oninstall/onactivate callback returns first.
18 Promise.resolve().then(function() {
19 var port = self.pendingPorts.shift();
23 self.portResolves.push(resolve);
25 }).then(function(port) {
26 port.postMessage('SYNC');
27 return new Promise(function(resolve) {
28 port.onmessage = function(e) {
36 function rejectPromise() {
37 return new Promise(function(resolve, reject) {
38 // Make sure the oninstall/onactivate callback returns first.
39 Promise.resolve().then(reject);
43 function stripScopeName(url) {
44 return url.split('/').slice(-1)[0];
47 oninstall = function(e) {
48 switch (stripScopeName(self.location.href)) {
49 case 'install-fulfilled':
50 e.waitUntil(fulfillPromise());
52 case 'install-rejected':
53 e.waitUntil(rejectPromise());
55 case 'install-multiple-fulfilled':
56 e.waitUntil(fulfillPromise());
57 e.waitUntil(fulfillPromise());
59 case 'install-reject-precedence':
60 e.waitUntil(fulfillPromise());
61 e.waitUntil(rejectPromise());
66 onactivate = function(e) {
67 switch (stripScopeName(self.location.href)) {
68 case 'activate-fulfilled':
69 e.waitUntil(fulfillPromise());
71 case 'activate-rejected':
72 e.waitUntil(rejectPromise());