From: Eduardo San Martin Morote Date: Tue, 17 Oct 2023 08:04:46 +0000 (+0200) Subject: feat: disposePinia X-Git-Tag: @pinia/nuxt@0.5.2-beta.0~125 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bb8bf60581747c76e42796b82eb3aa04e2336fdf;p=thirdparty%2Fvuejs%2Fpinia.git feat: disposePinia Close vuejs/pinia#2453 --- diff --git a/packages/pinia/src/createPinia.ts b/packages/pinia/src/createPinia.ts index f71497fd..fe2f0194 100644 --- a/packages/pinia/src/createPinia.ts +++ b/packages/pinia/src/createPinia.ts @@ -63,3 +63,18 @@ export function createPinia(): Pinia { return pinia } + +/** + * Dispose a Pinia instance by stopping its effectScope and removing the state, plugins and stores. This is mostly + * useful in tests, with both a testing pinia or a regular pinia and in applications that use multiple pinia instances. + * + * @param pinia - pinia instance + */ +export function disposePinia(pinia: Pinia) { + pinia._e.stop() + pinia._s.clear() + pinia._p.splice(0) + pinia.state.value = {} + // @ts-expect-error: non valid + pinia._a = null +} diff --git a/packages/pinia/src/index.ts b/packages/pinia/src/index.ts index fc28044f..d06aedc4 100644 --- a/packages/pinia/src/index.ts +++ b/packages/pinia/src/index.ts @@ -2,7 +2,7 @@ * @module pinia */ export { setActivePinia, getActivePinia } from './rootStore' -export { createPinia } from './createPinia' +export { createPinia, disposePinia } from './createPinia' export type { Pinia, // TODO: remove in next release