From: Jesse van Assen Date: Tue, 26 Oct 2021 14:34:14 +0000 (+0200) Subject: fix(plugins): ensure plugins are used only once (#745) X-Git-Tag: pinia@2.0.0~9 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=150fdfc8abe2577046af947b2e4fbbde2efb057e;p=thirdparty%2Fvuejs%2Fpinia.git fix(plugins): ensure plugins are used only once (#745) Co-authored-by: Eduardo San Martin Morote --- diff --git a/packages/pinia/__tests__/storePlugins.spec.ts b/packages/pinia/__tests__/storePlugins.spec.ts index cc484b4f..e5ed8720 100644 --- a/packages/pinia/__tests__/storePlugins.spec.ts +++ b/packages/pinia/__tests__/storePlugins.spec.ts @@ -247,4 +247,19 @@ describe('store plugins', () => { store.n++ expect(spy).toHaveBeenCalledTimes(1) }) + + it('only executes plugins once after multiple installs', async () => { + const pinia = createPinia() + + const spy = jest.fn() + pinia.use(spy) + + for (let i = 0; i < 3; i++) { + mount({ template: 'none' }, { global: { plugins: [pinia] } }).unmount() + } + + useStore(pinia) + + expect(spy).toHaveBeenCalledTimes(1) + }) }) diff --git a/packages/pinia/src/createPinia.ts b/packages/pinia/src/createPinia.ts index 20df76ef..4b362c6f 100644 --- a/packages/pinia/src/createPinia.ts +++ b/packages/pinia/src/createPinia.ts @@ -20,7 +20,7 @@ export function createPinia(): Pinia { let _p: Pinia['_p'] = [] // plugins added before calling app.use(pinia) - const toBeInstalled: PiniaStorePlugin[] = [] + let toBeInstalled: PiniaStorePlugin[] = [] const pinia: Pinia = markRaw({ install(app: App) { @@ -36,6 +36,7 @@ export function createPinia(): Pinia { registerPiniaDevtools(app, pinia) } toBeInstalled.forEach((plugin) => _p.push(plugin)) + toBeInstalled = [] } },