33 lines
733 B
TypeScript
33 lines
733 B
TypeScript
export const WEB_READINESS_CACHE_TTL_MS = 1_000;
|
|
|
|
let cached:
|
|
| {
|
|
check: () => Promise<unknown>;
|
|
expiresAt: number;
|
|
pending: Promise<void>;
|
|
}
|
|
| undefined;
|
|
|
|
export function checkCachedWebReadiness(check: () => Promise<unknown>, now = Date.now()): Promise<void> {
|
|
if (cached?.check === check && cached.expiresAt > now) {
|
|
return cached.pending;
|
|
}
|
|
|
|
let pending: Promise<void>;
|
|
try {
|
|
pending = Promise.resolve(check()).then(() => undefined);
|
|
} catch (error) {
|
|
pending = Promise.reject(error);
|
|
}
|
|
cached = {
|
|
check,
|
|
expiresAt: now + WEB_READINESS_CACHE_TTL_MS,
|
|
pending,
|
|
};
|
|
return pending;
|
|
}
|
|
|
|
export function resetWebReadinessCacheForTests(): void {
|
|
cached = undefined;
|
|
}
|