-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathutil.js
30 lines (27 loc) · 927 Bytes
/
util.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
import {CLIENT, createPortProxy} from '@/js/port';
import {workerPath} from '@/js/urls';
import {clientDataJobs} from './common';
import offscreen from './offscreen';
/** @return {WindowClient[]} */
export const getWindowClients = () => self.clients.matchAll({
includeUncontrolled: true,
type: 'window',
});
const getWorkerPortFromClient = async () => {
let proxy;
__.DEBUGPORT('sw -> worker -> offscreen client', offscreen[CLIENT]);
if (!offscreen[CLIENT]) {
for (const client of await getWindowClients()) {
if (!clientDataJobs.has(client.url)) {
__.DEBUGPORT('sw -> worker -> client', client);
proxy = createPortProxy(client, {once: true});
break;
}
}
}
return (proxy || offscreen).getWorkerPort(workerPath);
};
/** @type {WorkerAPI} */
export const worker = __.MV3
? createPortProxy(getWorkerPortFromClient, {lock: workerPath})
: createPortProxy(workerPath);