OLD | NEW |
---|---|
1 var port = null; | 1 function match_query(query_string) { |
2 return self.location.search.substr(1) == query_string; | |
3 } | |
2 | 4 |
3 self.onmessage = function(e) { | 5 function receive_event(event_name) { |
4 port = e.data.port; | 6 return new Promise(function(resolve) { |
5 port.onmessage = on_message; | 7 self.addEventListener(event_name, resolve, false); |
6 port.postMessage('ready'); | |
7 }; | |
8 | |
9 function on_message(e) { | |
10 var url = e.data; | |
11 var client; | |
12 | |
13 self.clients.matchAll({ includeUncontrolled : true }).then(function(cs) { | |
14 cs.forEach(function(c) { c.frameType == 'nested' && (client = c); }); | |
15 return client.navigate(url); | |
16 }) | |
17 .then(function(c) { | |
18 if (!c) | |
19 port.postMessage(c); | |
20 else | |
21 port.postMessage(c.url); | |
22 }) | |
23 .catch(function(e) { | |
24 port.postMessage(e.name); | |
25 }); | 8 }); |
26 } | 9 } |
10 | |
11 function get_test_data() { | |
12 return receive_event('message'); | |
13 } | |
14 | |
15 function navigate_test(e) { | |
16 var port = e.data.port; | |
17 var url = e.data.url; | |
18 | |
19 return clients.matchAll({ includeUncontrolled : true }) | |
20 .then(function(client_list) { | |
21 for (var i = 0; i < client_list.length; i++) { | |
22 var client = client_list[i]; | |
23 if (client.frameType == 'nested') { | |
24 return client.navigate(url); | |
25 } | |
26 } | |
27 port.postMessage('Could not found window client.'); | |
28 }) | |
29 .then(function(new_client) { | |
30 if (new_client === null) | |
31 port.postMessage(new_client); | |
32 else | |
33 port.postMessage(new_client.url); | |
34 }) | |
35 .catch(function(error) { | |
36 port.postMessage(error.name); | |
37 }); | |
38 } | |
39 | |
40 receive_event('install').then(function(e) { | |
41 if (match_query('installing')) | |
42 e.waitUntil(get_test_data().then(navigate_test)); | |
43 }); | |
44 | |
45 receive_event('activate').then(function(e) { | |
46 if (match_query('activating')) { | |
47 // TODO(zino): Should do test in this case. | |
48 } | |
49 get_test_data().then(navigate_test); | |
nhiroki
2016/02/01 07:13:55
Hmmm... there could be deadlock.
e.waitUntil(get_
zino
2016/02/02 15:58:31
I have some questions.
(1) Can the fetch event be
nhiroki
2016/02/08 04:37:15
No. When the worker is in installed/activating sta
| |
50 }); | |
51 | |
OLD | NEW |