From 150fdfc8abe2577046af947b2e4fbbde2efb057e Mon Sep 17 00:00:00 2001 From: Jesse van Assen Date: Tue, 26 Oct 2021 16:34:14 +0200 Subject: [PATCH] fix(plugins): ensure plugins are used only once (#745) Co-authored-by: Eduardo San Martin Morote --- packages/pinia/__tests__/storePlugins.spec.ts | 15 +++++++++++++++ packages/pinia/src/createPinia.ts | 3 ++- 2 files changed, 17 insertions(+), 1 deletion(-) 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 = [] } }, -- 2.47.3