]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat: disposePinia
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 17 Oct 2023 08:04:46 +0000 (10:04 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 17 Oct 2023 08:04:46 +0000 (10:04 +0200)
Close vuejs/pinia#2453

packages/pinia/src/createPinia.ts
packages/pinia/src/index.ts

index f71497fd31a865c6bbfe2eb124d4b38bcaf5d0ba..fe2f0194c20dcd0df806e18d2691ed7af2e552af 100644 (file)
@@ -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
+}
index fc28044f4d75fc0a673ed82f37772aff76f4b39e..d06aedc4fafe899e4991dcad2dccd5bd1df2ca49 100644 (file)
@@ -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