})
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', () => {
-export { setActivePinia } from './rootStore'
+export { setActivePinia, getActivePinia } from './rootStore'
export { createPinia } from './createPinia'
export type { Pinia, PiniaStorePlugin, PiniaPluginContext } from './rootStore'
-import { App, EffectScope, InjectionKey, Plugin, Ref, warn } from 'vue-demi'
+import {
+ App,
+ EffectScope,
+ getCurrentInstance,
+ inject,
+ InjectionKey,
+ Plugin,
+ Ref,
+} from 'vue-demi'
import {
StateTree,
PiniaCustomProperties,
(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
_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'
(__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(