blob: ae5d25df5815f1d05583effc6032309a4d7fd33f (
plain)
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
|
<script>
const WORKER_URL = "worker_suspended.js";
var testUrl2 = "window_suspended.html?page2Shown";
let cacheDataPromise = {};
cacheDataPromise.promise = new Promise(resolve => {
cacheDataPromise.resolve = resolve;
});
var bcName = location.search.split('?')[1];
var bc = new BroadcastChannel(bcName);
if (bcName == "page1Shown") {
bc.onmessage = async (msgEvent) => {
var msg = msgEvent.data;
var command = msg.command;
if (command == "startWorker") {
// Create a worker and subworkers
let { worker, promise } = postMessageWorker(msg.workerMessage);
promise.then(function() {
bc.postMessage({command: "verifyCacheData"});
return cacheDataPromise.promise;
})
.then(function() {
location.href = testUrl2;
});
} else if (command == "changeLocation") {
cacheDataPromise.resolve();
} else if (command == "finish") {
bc.postMessage({command: "finished"});
bc.close();
window.close();
}
}
} else if (bcName == "page2Shown") {
bc.onmessage = (msgEvent) => {
var msg = msgEvent.data;
var command = msg.command;
if (command == "startWorker") {
let { worker, promise } = postMessageWorker(msg.workerMessage);
promise.then(function() {
bc.postMessage({command: "verifyCacheData"});
return cacheDataPromise.promise;
})
.then(function() {
bc.close();
history.back();
});
} else if (command == "goBack") {
cacheDataPromise.resolve();
}
}
}
function postMessageWorker(message) {
let worker = new window.Worker(WORKER_URL);
var promise = new Promise((resolve, reject) => {
// Waiting until workers are ready
worker.addEventListener("message", function onmessage(msg) {
bc.postMessage({command: "workerMessage", workerMessage: msg.data});
worker.removeEventListener("message", onmessage);
resolve();
});
worker.postMessage(message);
});
return { worker, promise };
}
onpageshow = function(e) {
bc.postMessage({command: "onpageshow", persisted: e.persisted, location: location.href});
}
</script>
|