blob: 5a46ff9cf44010c5f9375bbb7b284932bd21cca7 (
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
|
let savedPort = null;
let savedResultingClientId = null;
async function getTestingPage() {
const clientList = await self.clients.matchAll({ type: 'window', includeUncontrolled: true });
for (let c of clientList) {
if (c.url.endsWith('clients-get.https.html')) {
c.focus();
return c;
}
}
return null;
}
async function destroyResultingClient(testingPage) {
const destroyedPromise = new Promise(resolve => {
self.addEventListener('message', e => {
if (e.data.msg == 'resultingClientDestroyed') {
resolve();
}
}, {once: true});
});
testingPage.postMessage({ msg: 'destroyResultingClient' });
return destroyedPromise;
}
self.addEventListener('fetch', async (e) => {
let { resultingClientId } = e;
savedResultingClientId = resultingClientId;
if (e.request.url.endsWith('simple.html?fail')) {
e.waitUntil((async () => {
const testingPage = await getTestingPage();
await destroyResultingClient(testingPage);
testingPage.postMessage({ msg: 'resultingClientDestroyedAck',
resultingDestroyedClientId: savedResultingClientId });
})());
return;
}
e.respondWith(fetch(e.request));
});
self.addEventListener('message', (e) => {
let { msg, resultingClientId } = e.data;
e.waitUntil((async () => {
if (msg == 'getIsResultingClientUndefined') {
const client = await self.clients.get(resultingClientId);
let isUndefined = typeof client == 'undefined';
e.source.postMessage({ msg: 'getIsResultingClientUndefined',
isResultingClientUndefined: isUndefined });
return;
}
if (msg == 'getResultingClientId') {
e.source.postMessage({ msg: 'getResultingClientId',
resultingClientId: savedResultingClientId });
return;
}
})());
});
|