OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var g_activated_service_worker_promise = null; | |
Michael van Ouwerkerk
2015/04/15 11:02:09
nit: use camelCase for js, the entire language is
Peter Beverloo
2015/04/15 17:13:35
Done.
| |
6 | |
5 // Returns a promise that will be resolved with an activated Service | 7 // Returns a promise that will be resolved with an activated Service |
6 // Worker, or rejects when the Service Worker could not be started. There | 8 // Worker, or rejects when the Service Worker could not be started. There |
7 // will be a message port to and from the worker in |messagePort|. | 9 // will be a message port to and from the worker in |messagePort|. |
8 function GetActivatedServiceWorker(script, scope) { | 10 function GetActivatedServiceWorker(script, scope) { |
9 return navigator.serviceWorker.getRegistration(scope) | 11 if (g_activated_service_worker_promise == null) { |
10 .then(function(registration) { | 12 g_activated_service_worker_promise = |
11 // Unregister any existing Service Worker. | 13 navigator.serviceWorker.getRegistration(scope) |
12 if (registration) | 14 .then(function(registration) { |
13 return registration.unregister(); | 15 // Unregister any existing Service Worker. |
14 }).then(function() { | 16 if (registration) |
15 // Register the Service Worker again. | 17 return registration.unregister(); |
16 return navigator.serviceWorker.register(script, { scope: scope }); | 18 }).then(function() { |
17 }).then(function(registration) { | 19 // Register the Service Worker again. |
18 if (registration.active) { | 20 return navigator.serviceWorker.register(script, { scope: scope }); |
19 return registration; | 21 }).then(function(registration) { |
20 } else if (registration.waiting || registration.installing) { | 22 if (registration.active) { |
21 var worker = registration.waiting || registration.installing; | 23 return registration; |
24 } else if (registration.waiting || registration.installing) { | |
25 var worker = registration.waiting || registration.installing; | |
26 return new Promise(function(resolve) { | |
27 worker.addEventListener('statechange', function () { | |
28 if (worker.state === 'activated') | |
29 resolve(registration); | |
30 }); | |
31 }); | |
32 } else { | |
33 return Promise.reject('Service Worker in invalid state.'); | |
34 } | |
35 }).then(function(registration) { | |
22 return new Promise(function(resolve) { | 36 return new Promise(function(resolve) { |
23 worker.addEventListener('statechange', function () { | 37 var channel = new MessageChannel(); |
24 if (worker.state === 'activated') | 38 channel.port1.addEventListener('message', function(event) { |
39 if (event.data == 'ready') | |
25 resolve(registration); | 40 resolve(registration); |
26 }); | 41 }); |
42 | |
43 registration.active.postMessage(channel.port2, | |
44 [ channel.port2 ]); | |
45 | |
46 messagePort = channel.port1; | |
47 messagePort.start(); | |
27 }); | 48 }); |
28 } else { | 49 }); |
29 return Promise.reject('Service Worker in invalid state.'); | 50 } |
30 } | |
31 }).then(function(registration) { | |
32 return new Promise(function(resolve) { | |
33 var channel = new MessageChannel(); | |
34 channel.port1.addEventListener('message', function(event) { | |
35 if (event.data == 'ready') | |
36 resolve(registration); | |
37 }); | |
38 | 51 |
39 registration.active.postMessage(channel.port2, | 52 return g_activated_service_worker_promise; |
40 [ channel.port2 ]); | |
41 | |
42 messagePort = channel.port1; | |
43 messagePort.start(); | |
44 }); | |
45 }); | |
46 } | 53 } |
OLD | NEW |