OLD | NEW |
1 <script> | 1 <script> |
2 var port = undefined; | 2 var port = undefined; |
3 var scope = 'fetch-request-body-file-test'; | 3 var scope = 'fetch-request-body-file-test'; |
4 var script = 'fetch-request-body-file-worker.js'; | 4 var script = 'fetch-request-body-file-worker.js'; |
5 | 5 |
6 window.addEventListener('message', function(evt) { | 6 window.addEventListener('message', function(evt) { |
7 port = evt.ports[0]; | 7 port = evt.ports[0]; |
8 start(); | 8 start(); |
9 }, false); | 9 }, false); |
10 | 10 |
11 function wait_for_update(registration) { | 11 function wait_for_activated(registration) { |
12 return new Promise(function(resolve) { | 12 return new Promise(function(resolve) { |
13 registration.addEventListener('updatefound', function() { | 13 var worker = registration.installing; |
14 resolve(registration.installing); | |
15 }); | |
16 }); | |
17 } | |
18 | |
19 function wait_for_activated(worker) { | |
20 return new Promise(function(resolve) { | |
21 worker.addEventListener('statechange', function() { | 14 worker.addEventListener('statechange', function() { |
22 if (worker.state == 'activated') { | 15 if (worker.state == 'activated') { |
23 resolve(worker); | 16 resolve(worker); |
24 } | 17 } |
25 }); | 18 }); |
26 }); | 19 }); |
27 } | 20 } |
28 | 21 |
29 function start() { | 22 function start() { |
30 if (location.search == '?register') { | 23 if (location.search == '?register') { |
31 navigator.serviceWorker.register(script, {scope: scope}) | 24 navigator.serviceWorker.register(script, {scope: scope}) |
32 .then(function(registration) { | 25 .then(function(registration) { |
33 return registration.unregister(); | 26 return registration.unregister(); |
34 }) | 27 }) |
35 .then(function() { | 28 .then(function() { |
36 return navigator.serviceWorker.register(script, {scope: scope}); | 29 return navigator.serviceWorker.register(script, {scope: scope}); |
37 }) | 30 }) |
38 .then(wait_for_update) | |
39 .then(wait_for_activated) | 31 .then(wait_for_activated) |
40 .then(function(worker) { | 32 .then(function(worker) { |
41 port.postMessage({msg: 'registered'}); | 33 port.postMessage({msg: 'registered'}); |
42 }); | 34 }); |
43 } else if (location.search == '?unregister') { | 35 } else if (location.search == '?unregister') { |
44 navigator.serviceWorker.register(script, {scope: scope}) | 36 navigator.serviceWorker.register(script, {scope: scope}) |
45 .then(function(registration) { | 37 .then(function(registration) { |
46 return registration.unregister(); | 38 return registration.unregister(); |
47 }) | 39 }) |
48 .then(function() { | 40 .then(function() { |
49 port.postMessage({msg: 'unregistered'}); | 41 port.postMessage({msg: 'unregistered'}); |
50 }); | 42 }); |
51 } | 43 } |
52 } | 44 } |
53 </script> | 45 </script> |
OLD | NEW |