Skip to content

Commit

Permalink
feat: expose getActivePinia
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Aug 18, 2021
1 parent 3f186a2 commit 8b8d0c1
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 26 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions packages/pinia/__tests__/rootState.spec.ts
Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion 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'

Expand Down
26 changes: 12 additions & 14 deletions 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,
Expand Down Expand Up @@ -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
Expand Down
21 changes: 12 additions & 9 deletions packages/pinia/src/store.ts
Expand Up @@ -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'
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 8b8d0c1

Please sign in to comment.