-
Notifications
You must be signed in to change notification settings - Fork 0
/
workerb.js
64 lines (52 loc) · 2.11 KB
/
workerb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// The SW will be shutdown when not in use to save memory,
// be aware that any global state is likely to disappear
//Shift+F5 in Chrome to reload
console.log("SW startup");
var globalvar = 0;
//globalvar stays in scope through the life of the worker - which may be very short.
//the browser will start and stop the ServiceWorker at will - which will blow away global state
var installran = "no";
//again, this var in global scope will be blown away when the browser restarts the worker.
function Prom() {
var ret = this;
var p = new Promise(function(resolve, reject){
ret.res = resolve;
ret.rej = reject;
});
this.promise = p;
}
function someothercrazyfunction(res) {
var rand = Math.random() * 2000 + 2000;
setTimeout(function(){ res('waited ' + rand + ' ms!!'); }, rand)
}
var stateswitch = 0;
self.addEventListener('install', function(event) {
console.log("SW installed");
//this will only run if the browser has not seen this version of the SW yet
installran = "yes";
});
self.addEventListener('activate', function(event) {
console.log("SW activated");
//this will run when the old version is disposed of and this one is ready to rock
var pr = new Prom();
//You can pass a promise to event.waitUntil if you want to prolong this part of the lifecycle artifically. If the promise fails,
//the serviceworker will be garbagecollected. Otherwise, if the promise is fulfilled, this stage of the lifecycle will
//be complete.
event.waitUntil(pr.promise.then(function(v){ console.log('stuff done at activation - resolved with:' + v); }));
someothercrazyfunction(pr.res);
});
self.addEventListener('message', function handlemessage(ev){
console.log('MESSAGE FROM WINDOW: ' + m);
stateswitch = !stateswitch;
});
self.addEventListener('fetch', function(event) {
console.log("Fetch For::" + event.request.url);
if( event.request.url === 'https://bkimmel.github.io/') {
console.log("Globalvar:" + globalvar++);
console.log("install ran this time?: " + installran);
if(stateswitch) {
event.respondWith(new Response('Switched State'));
stateswitch = !stateswitch;
}
}
});