fix(nuxt): do not rerun synchronous callOnce callbacks (#25431)

This commit is contained in:
Becem 2024-01-25 15:29:50 +01:00 committed by GitHub
parent 3f51277219
commit aa4faaab3c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 1 deletions

View File

@ -25,7 +25,7 @@ export async function callOnce (...args: any): Promise<void> {
return return
} }
nuxtApp._once = nuxtApp._once || {} nuxtApp._once = nuxtApp._once || {}
nuxtApp._once[_key] = nuxtApp._once[_key] || fn() nuxtApp._once[_key] = nuxtApp._once[_key] || fn() || true
await nuxtApp._once[_key] await nuxtApp._once[_key]
nuxtApp.payload.once.add(_key) nuxtApp.payload.once.add(_key)
delete nuxtApp._once[_key] delete nuxtApp._once[_key]

View File

@ -595,6 +595,11 @@ describe('callOnce', () => {
const execute = () => callOnce(fn) const execute = () => callOnce(fn)
await Promise.all([execute(), execute(), execute()]) await Promise.all([execute(), execute(), execute()])
expect(fn).toHaveBeenCalledTimes(1) expect(fn).toHaveBeenCalledTimes(1)
const fnSync = vi.fn().mockImplementation(() => { })
const executeSync = () => callOnce(fnSync)
await Promise.all([executeSync(), executeSync(), executeSync()])
expect(fnSync).toHaveBeenCalledTimes(1)
}) })
it('should use key to dedupe', async () => { it('should use key to dedupe', async () => {