-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
78 lines (69 loc) · 2.6 KB
/
app.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
var _reg = null;
async function initSW() {
return new Promise((resolve, reject) => {
navigator.serviceWorker.register("sw9.js", { scope: '/' }).then(function (reg) {
_reg = reg;
if (_reg.active === null) {
var installingWorker = _reg.installing;
installingWorker.onstatechange = function (stateEvt) {
if (stateEvt.target.state === "activated") {
console.log('cold reload SW activated, resolving!!!');
resolve();
}
};
} else {
console.log('warm reload SW active:' + _reg.active.scriptURL);
_installingWarmWorker = _reg.installing;
if (_installingWarmWorker) {
console.log('We are going to install SW:' + _installingWarmWorker.scriptURL);
_reg.onupdatefound = function (newreg) {
_reg = null;
newreg.currentTarget.installing.onstatechange = function (stateEvt) {
console.log('warm reload.onstatechange: svc wkr stateEvt.target.state:' + stateEvt.target.state);
if (stateEvt.target.state === 'activated') {
console.log('warm reload: svc wkr state is activated, resolving now!!!');
resolve();
_reg = newreg.currentTarget;
}
};
};
} else {
console.log('Posting claim');
reg.active.postMessage('claim');
resolve();
}
}
});
});
}
let _resolveIdComplete = null;
let _winId = null;
async function getId() {
return new Promise((resolve, reject) => {
_resolveIdComplete = resolve;
_reg.active.postMessage('resolveId');
})
}
function handleResolve(Id) {
console.log('SW is activated!!! proceed');
_winId = Id;
}
async function init() {
await initSW();
await getId().then(handleResolve);
}
async function main() {
await init();
// This is the starting!!!
console.log('Creating worker with winId:' + _winId);
let w = new Worker('worker.js');
//w.postMessage({eventType:'initWndId', wndId:_winId});
w.postMessage(_winId);
}
main();
navigator.serviceWorker.addEventListener('message', function (e) {
if (e.data.resolveIdDone = 1) {
console.log('app.js received resolveIdDone!!!');
_resolveIdComplete(e.data.winId);
}
});