Skip to content

Commit

Permalink
fix(useScript): avoid calling proxy functions before loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Oct 14, 2024
1 parent d5520c8 commit 843e00c
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/unhead/src/composables/useScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
// script is ready
loadPromise
.then((api) => {
if (api) {
if (api !== false) {
script.instance = api
_cbs.loaded?.forEach(cb => cb(api))
_cbs.loaded = null
Expand Down Expand Up @@ -237,10 +237,11 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
}
let fn = access(script.instance)
if (!fn) {
const instance = await loadPromise
if (instance !== false) {
fn = access(instance)
}
fn = await (new Promise<T | undefined>((resolve) => {
script.onLoaded(api => {
resolve(access(api))
})
}))
}
return typeof fn === 'function' ? Reflect.apply(fn, instance, args) : fn
},
Expand Down

0 comments on commit 843e00c

Please sign in to comment.