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 navigate_test(e) { | |
12 var port = e.data.port; | |
13 var url = e.data.url; | |
14 | |
15 return clients.matchAll({ includeUncontrolled : true }) | |
16 .then(function(client_list) { | |
17 for (var i = 0; i < client_list.length; i++) { | |
18 var client = client_list[i]; | |
19 if (client.frameType == 'nested') { | |
20 return client.navigate(url); | |
21 } | |
22 } | |
23 port.postMessage('Could not found window client.'); | |
24 }) | |
25 .then(function(new_client) { | |
26 if (new_client === null) | |
27 port.postMessage(new_client); | |
28 else | |
29 port.postMessage(new_client.url); | |
30 }) | |
31 .catch(function(error) { | |
32 port.postMessage(error.name); | |
33 }); | |
34 } | |
35 if (match_query('installing')) { | |
36 // If the query string is "?installing", then do test on installing worker. | |
37 // This is only for in-scope-but-not-controlled test. | |
38 receive_event('install').then(function(e) { | |
39 console.log(self.registration.installing); | |
nhiroki
2016/02/16 07:33:22
Can you remove this logging?
zino
2016/02/16 07:41:12
Done.
| |
40 e.waitUntil(receive_event('message').then(navigate_test)); | |
41 }); | |
42 } else { | |
43 receive_event('message').then(function(e) { | |
44 e.waitUntil(navigate_test(e)); | |
45 }); | |
46 } | |
OLD | NEW |