Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 pendingPorts = []; | |
| 2 portResolves = []; | |
| 3 | |
| 4 onmessage = function(e) { | |
| 5 var message = e.data; | |
| 6 if ('port' in message) { | |
| 7 var resolve = portResolves.shift(); | |
| 8 if (resolve) | |
| 9 resolve(message.port); | |
| 10 else | |
| 11 pendingPorts.push(message.port); | |
| 12 } | |
| 13 }; | |
| 14 | |
| 15 function fulfillPromise() { | |
| 16 return new Promise(function(resolve) { | |
| 17 // Make sure oninstall/onactivate callback finishes first. | |
| 18 setTimeout(function() { | |
| 19 var port = pendingPorts.shift(); | |
| 20 if (port) | |
| 21 resolve(port); | |
| 22 else | |
| 23 portResolves.push(resolve); | |
| 24 }, 0); | |
| 25 }).then(function(port) { | |
| 26 port.postMessage('SYNC'); | |
| 27 return new Promise(function(resolve) { | |
| 28 port.onmessage = function(e) { | |
| 29 if (e.data == 'ACK') | |
| 30 resolve(); | |
| 31 }; | |
| 32 }); | |
| 33 }); | |
| 34 } | |
| 35 | |
| 36 function rejectPromise() { | |
| 37 return new Promise(function(resolve, reject) { | |
| 38 setTimeout(reject, 0); | |
| 39 }); | |
| 40 } | |
| 41 | |
| 42 function stripScopeName(scope) { | |
| 43 return scope.split('/').slice(-1)[0]; | |
| 44 } | |
| 45 | |
| 46 oninstall = function(e) { | |
| 47 var test_switch = stripScopeName(scope); | |
|
falken
2014/06/30 04:35:31
could you use "self.scope" to make clear it's a gl
xiang
2014/06/30 06:37:11
OK, I will change that
| |
| 48 if (test_switch == 'install-fulfilled') | |
| 49 e.waitUntil(fulfillPromise()); | |
| 50 else if (test_switch == 'install-rejected') | |
| 51 e.waitUntil(rejectPromise()); | |
| 52 }; | |
| 53 | |
| 54 onactivate = function(e) { | |
| 55 var test_switch = stripScopeName(scope); | |
| 56 if (test_switch == 'activate-fulfilled') { | |
| 57 e.waitUntil(fulfillPromise()); | |
| 58 } else if (test_switch == 'activate-rejected') { | |
|
falken
2014/06/30 04:35:31
these look like they'd be more readable as a switc
xiang
2014/06/30 06:37:11
Yes, will change, thanks.
| |
| 59 e.waitUntil(rejectPromise()); | |
| 60 } else if (test_switch == 'activate-multiple-fulfilled') { | |
| 61 e.waitUntil(fulfillPromise()); | |
| 62 e.waitUntil(fulfillPromise()); | |
| 63 } else if (test_switch == 'activate-reject-precedence') { | |
| 64 e.waitUntil(fulfillPromise()); | |
| 65 e.waitUntil(new Promise(function(resolve, reject) { | |
| 66 setTimeout(reject, 0); | |
| 67 })); | |
| 68 } | |
| 69 }; | |
| OLD | NEW |