Skip to content

Commit

Permalink
fix(plugins): ensure plugins are used only once (#745)
Browse files Browse the repository at this point in the history
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com>
  • Loading branch information
jessevanassen and posva committed Oct 26, 2021
1 parent 1b397f1 commit 150fdfc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packages/pinia/__tests__/storePlugins.spec.ts
Expand Up @@ -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)
})
})
3 changes: 2 additions & 1 deletion packages/pinia/src/createPinia.ts
Expand Up @@ -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) {
Expand All @@ -36,6 +36,7 @@ export function createPinia(): Pinia {
registerPiniaDevtools(app, pinia)
}
toBeInstalled.forEach((plugin) => _p.push(plugin))
toBeInstalled = []
}
},

Expand Down

0 comments on commit 150fdfc

Please sign in to comment.