Urara-Blog/node_modules/.pnpm-store/v3/files/39/f5022c24378a94a0579c28dda64e8ef5086b41aada16eca26dfe3233028dc8733a146a2a31e3d1bbb10dcb3345be76d9490b05233d3104e20e36b4601ed97a
2022-08-14 01:14:53 +08:00

62 lines
1.7 KiB
Text

/*
Copyright 2019 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import {timeout} from './timeout.js';
import '../_version.js';
// Give TypeScript the correct global.
declare let self: ServiceWorkerGlobalScope;
const MAX_RETRY_TIME = 2000;
/**
* Returns a promise that resolves to a window client matching the passed
* `resultingClientId`. For browsers that don't support `resultingClientId`
* or if waiting for the resulting client to apper takes too long, resolve to
* `undefined`.
*
* @param {string} [resultingClientId]
* @return {Promise<Client|undefined>}
* @private
*/
export async function resultingClientExists(
resultingClientId?: string,
): Promise<Client | undefined> {
if (!resultingClientId) {
return;
}
let existingWindows = await self.clients.matchAll({type: 'window'});
const existingWindowIds = new Set(existingWindows.map((w) => w.id));
let resultingWindow;
const startTime = performance.now();
// Only wait up to `MAX_RETRY_TIME` to find a matching client.
while (performance.now() - startTime < MAX_RETRY_TIME) {
existingWindows = await self.clients.matchAll({type: 'window'});
resultingWindow = existingWindows.find((w) => {
if (resultingClientId) {
// If we have a `resultingClientId`, we can match on that.
return w.id === resultingClientId;
} else {
// Otherwise match on finding a window not in `existingWindowIds`.
return !existingWindowIds.has(w.id);
}
});
if (resultingWindow) {
break;
}
// Sleep for 100ms and retry.
await timeout(100);
}
return resultingWindow;
}