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

48 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';
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) {
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;
}