Skip to content

Commit

Permalink
fix(scripts): clean up expired promises
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Sep 15, 2024
1 parent 5eb827d commit 59640c1
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/unhead/src/composables/useScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,15 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
else if (trigger instanceof Promise) {
script._triggerAbortController = script._triggerAbortController || new AbortController()
script._triggerPromises = script._triggerPromises || []
script._triggerPromises.push(Promise.race([
trigger.then(() => script.load),
const idx = script._triggerPromises.push(Promise.race([
trigger.then(v => typeof v === 'undefined' || v ? script.load : undefined),
new Promise<void>((resolve) => {
script._triggerAbortController!.signal.addEventListener('abort', () => resolve())
}),
]).then((res) => {
res?.()
// remove the promise from the list
script._triggerPromises?.splice(idx, 1)
}))
}
else if (typeof trigger === 'function') {
Expand Down

0 comments on commit 59640c1

Please sign in to comment.