]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat: expose getActivePinia
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 18 Aug 2021 21:20:43 +0000 (23:20 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 18 Aug 2021 21:20:43 +0000 (23:20 +0200)
packages/nuxt/README.md [moved from packages/pinia/nuxt/README.md with 100% similarity]
packages/nuxt/index.ts [moved from packages/pinia/nuxt/index.ts with 100% similarity]
packages/nuxt/plugin.ts [moved from packages/pinia/nuxt/plugin.ts with 100% similarity]
packages/nuxt/types.d.ts [moved from packages/pinia/nuxt/types.d.ts with 100% similarity]
packages/pinia/__tests__/rootState.spec.ts
packages/pinia/src/index.ts
packages/pinia/src/rootStore.ts
packages/pinia/src/store.ts

index 3103756d147d645e01488487a86fa90d76c74964..d16fa1c1e5aede0083f607c6a5417b198a822c49 100644 (file)
@@ -14,8 +14,7 @@ describe('Root State', () => {
   })
 
   it('warns if creating a store without a pinia', () => {
-    expect(() => useA()).toThrow()
-    expect('with no active Pinia').toHaveBeenWarned()
+    expect(() => useA()).toThrowError(/with no active Pinia/)
   })
 
   it('works with no stores', () => {
index 61bb863e9aa208bbbd1b2ece06074dd4b48b6764..e0f8af46eac6b5ad29c838b3b7572d01549d6627 100644 (file)
@@ -1,4 +1,4 @@
-export { setActivePinia } from './rootStore'
+export { setActivePinia, getActivePinia } from './rootStore'
 export { createPinia } from './createPinia'
 export type { Pinia, PiniaStorePlugin, PiniaPluginContext } from './rootStore'
 
index ff6cf9fbd79c8d078939f848470cffeb6b5757a3..15ff28e10ce5c53361544da4e0206b286b1fe8da 100644 (file)
@@ -1,4 +1,12 @@
-import { App, EffectScope, InjectionKey, Plugin, Ref, warn } from 'vue-demi'
+import {
+  App,
+  EffectScope,
+  getCurrentInstance,
+  inject,
+  InjectionKey,
+  Plugin,
+  Ref,
+} from 'vue-demi'
 import {
   StateTree,
   PiniaCustomProperties,
@@ -27,20 +35,10 @@ export const setActivePinia = (pinia: Pinia | undefined) =>
   (activePinia = pinia)
 
 /**
- * Get the currently active pinia
+ * Get the currently active pinia if there is any.
  */
-export const getActivePinia = () => {
-  if (__DEV__ && !activePinia) {
-    warn(
-      `[🍍]: getActivePinia was called with no active Pinia. Did you forget to install pinia?\n\n` +
-        `const pinia = createPinia()\n` +
-        `app.use(pinia)\n\n` +
-        `This will fail in production.`
-    )
-  }
-
-  return activePinia!
-}
+export const getActivePinia = () =>
+  (getCurrentInstance() && inject(piniaSymbol)) || activePinia
 
 /**
  * Every application must own its own pinia to be able to create stores
index 2eb199dcdc25550d69e92b24aac97449ed466170..c9b9428c0d6d3995a9b74905066345c136ddd8f4 100644 (file)
@@ -45,13 +45,7 @@ import {
   _ExtractStateFromSetupStore,
   StoreWithState,
 } from './types'
-import {
-  getActivePinia,
-  setActivePinia,
-  piniaSymbol,
-  Pinia,
-  activePinia,
-} from './rootStore'
+import { setActivePinia, piniaSymbol, Pinia, activePinia } from './rootStore'
 import { IS_CLIENT } from './env'
 import { patchObject } from './hmr'
 import { addSubscription, triggerSubscriptions } from './subscriptions'
@@ -757,8 +751,17 @@ export function defineStore(
       (__TEST__ && activePinia && activePinia._testing ? null : pinia) ||
       (currentInstance && inject(piniaSymbol))
     if (pinia) setActivePinia(pinia)
-    // TODO: worth warning on server if no piniaKey as it can leak data
-    pinia = getActivePinia()
+
+    if (__DEV__ && !activePinia) {
+      throw new Error(
+        `[🍍]: getActivePinia was called with no active Pinia. Did you forget to install pinia?\n\n` +
+          `const pinia = createPinia()\n` +
+          `app.use(pinia)\n\n` +
+          `This will fail in production.`
+      )
+    }
+
+    pinia = activePinia!
 
     if (!pinia._s.has(id)) {
       pinia._s.set(