Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 var pendingPorts = []; | |
| 2 var portResolves = []; | |
| 3 | |
| 4 onmessage = function(e) { | |
| 5 var message = e.data; | |
| 6 if ('port' in message) { | |
| 7 var resolve = self.portResolves.shift(); | |
| 8 if (resolve) | |
| 9 resolve(message.port); | |
| 10 else | |
| 11 self.pendingPorts.push(message.port); | |
| 12 } | |
| 13 }; | |
| 14 | |
| 15 function fulfillPromise() { | |
| 16 return new Promise(function(resolve) { | |
| 17 // Make sure oninstall/onactivate callback finishes first. | |
|
jsbell
2014/07/01 16:16:48
Is it really necessary for this to run in a subseq
xiang
2014/07/02 05:10:42
Yes, microtask is sufficient here, thanks!
| |
| 18 setTimeout(function() { | |
| 19 var port = self.pendingPorts.shift(); | |
| 20 if (port) | |
| 21 resolve(port); | |
| 22 else | |
| 23 self.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); | |
|
jsbell
2014/07/01 16:16:48
Add comment similar to the above, i.e. why `return
xiang
2014/07/02 05:10:42
will add.
| |
| 39 }); | |
| 40 } | |
| 41 | |
| 42 function stripScopeName(scope) { | |
| 43 return scope.split('/').slice(-1)[0]; | |
| 44 } | |
| 45 | |
| 46 oninstall = function(e) { | |
| 47 switch (stripScopeName(self.scope)) { | |
| 48 case 'install-fulfilled': | |
| 49 e.waitUntil(fulfillPromise()); | |
| 50 break; | |
| 51 case 'install-rejected': | |
| 52 e.waitUntil(rejectPromise()); | |
| 53 break; | |
| 54 } | |
| 55 }; | |
| 56 | |
| 57 onactivate = function(e) { | |
| 58 switch (stripScopeName(self.scope)) { | |
| 59 case 'activate-fulfilled': | |
| 60 e.waitUntil(fulfillPromise()); | |
| 61 break; | |
| 62 case 'activate-rejected': | |
| 63 e.waitUntil(rejectPromise()); | |
| 64 break; | |
| 65 case 'activate-multiple-fulfilled': | |
| 66 e.waitUntil(fulfillPromise()); | |
| 67 e.waitUntil(fulfillPromise()); | |
| 68 break; | |
| 69 case 'activate-reject-precedence': | |
| 70 e.waitUntil(fulfillPromise()); | |
| 71 e.waitUntil(new Promise(function(resolve, reject) { | |
|
jsbell
2014/07/01 16:16:48
Why not rejectPromise() here?
xiang
2014/07/02 05:10:42
Sure, I will change it.
| |
| 72 setTimeout(reject, 0); | |
| 73 })); | |
| 74 break; | |
| 75 } | |
| 76 }; | |
| OLD | NEW |