Skip to content

Commit

Permalink
fix: make sure to stop waiting for assets to be loaded if timeout ela…
Browse files Browse the repository at this point in the history
…psed
  • Loading branch information
benjaminrobinet committed Nov 13, 2024
1 parent 16cc938 commit 270d284
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/waitForAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,23 @@ export function waitForStylesheet(
element: HTMLLinkElement,
timeoutMs: number = 0
): Promise<HTMLLinkElement> {
let timeoutId: ReturnType<typeof setTimeout>;

const whenLoaded = (cb: () => void) => {
if (element.sheet) {
cb();
} else {
setTimeout(() => whenLoaded(cb), 10);
timeoutId = setTimeout(() => whenLoaded(cb), 10);
}
};

return new Promise((resolve) => {
whenLoaded(() => resolve(element));
if (timeoutMs > 0) {
setTimeout(() => resolve(element), timeoutMs);
setTimeout(() => {
if (timeoutId) clearTimeout(timeoutId);
resolve(element);
}, timeoutMs);
}
});
}

0 comments on commit 270d284

Please sign in to comment.