]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
fix(plugins): ensure plugins are used only once (#745)
authorJesse van Assen <jesse.v.assen@gmail.com>
Tue, 26 Oct 2021 14:34:14 +0000 (16:34 +0200)
committerGitHub <noreply@github.com>
Tue, 26 Oct 2021 14:34:14 +0000 (16:34 +0200)
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com>
packages/pinia/__tests__/storePlugins.spec.ts
packages/pinia/src/createPinia.ts

index cc484b4fe80365044e99f15c87938c88b26ba35b..e5ed8720b01f6949f5b4b99a3b42834d54e29819 100644 (file)
@@ -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)
+  })
 })
index 20df76efeacb5e61d20668cec95f2c835db7fe25..4b362c6f4f61291364e56a8a5d0c9f3812ac9a36 100644 (file)
@@ -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 = []
       }
     },