From: Eduardo San Martin Morote Date: Wed, 18 Aug 2021 21:20:43 +0000 (+0200) Subject: feat: expose getActivePinia X-Git-Tag: @pinia/nuxt@0.0.1~17 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8b8d0c17958e3b4e2d9bc809c78a28931d1b00f0;p=thirdparty%2Fvuejs%2Fpinia.git feat: expose getActivePinia --- diff --git a/packages/pinia/nuxt/README.md b/packages/nuxt/README.md similarity index 100% rename from packages/pinia/nuxt/README.md rename to packages/nuxt/README.md diff --git a/packages/pinia/nuxt/index.ts b/packages/nuxt/index.ts similarity index 100% rename from packages/pinia/nuxt/index.ts rename to packages/nuxt/index.ts diff --git a/packages/pinia/nuxt/plugin.ts b/packages/nuxt/plugin.ts similarity index 100% rename from packages/pinia/nuxt/plugin.ts rename to packages/nuxt/plugin.ts diff --git a/packages/pinia/nuxt/types.d.ts b/packages/nuxt/types.d.ts similarity index 100% rename from packages/pinia/nuxt/types.d.ts rename to packages/nuxt/types.d.ts diff --git a/packages/pinia/__tests__/rootState.spec.ts b/packages/pinia/__tests__/rootState.spec.ts index 3103756d..d16fa1c1 100644 --- a/packages/pinia/__tests__/rootState.spec.ts +++ b/packages/pinia/__tests__/rootState.spec.ts @@ -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', () => { diff --git a/packages/pinia/src/index.ts b/packages/pinia/src/index.ts index 61bb863e..e0f8af46 100644 --- a/packages/pinia/src/index.ts +++ b/packages/pinia/src/index.ts @@ -1,4 +1,4 @@ -export { setActivePinia } from './rootStore' +export { setActivePinia, getActivePinia } from './rootStore' export { createPinia } from './createPinia' export type { Pinia, PiniaStorePlugin, PiniaPluginContext } from './rootStore' diff --git a/packages/pinia/src/rootStore.ts b/packages/pinia/src/rootStore.ts index ff6cf9fb..15ff28e1 100644 --- a/packages/pinia/src/rootStore.ts +++ b/packages/pinia/src/rootStore.ts @@ -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 diff --git a/packages/pinia/src/store.ts b/packages/pinia/src/store.ts index 2eb199dc..c9b9428c 100644 --- a/packages/pinia/src/store.ts +++ b/packages/pinia/src/store.ts @@ -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(