Skip to content

Commit

Permalink
feat: add mapStores
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Apr 9, 2021
1 parent 2efb77c commit d3d9327
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Expand Up @@ -15,5 +15,6 @@ export {
DefineStoreOptions,
} from './types'

export { mapStores } from './mapHelpers'
// TODO: remove in beta
export { createStore } from './deprecated'
10 changes: 8 additions & 2 deletions src/store.ts
Expand Up @@ -12,6 +12,7 @@ import {
Method,
PiniaCustomProperties,
DefineStoreOptions,
StoreDefinition,
} from './types'
import {
getActivePinia,
Expand Down Expand Up @@ -258,10 +259,10 @@ export function defineStore<
S extends StateTree,
G /* extends Record<string, StoreGetterThis> */,
A /* extends Record<string, StoreAction> */
>(options: DefineStoreOptions<Id, S, G, A>) {
>(options: DefineStoreOptions<Id, S, G, A>): StoreDefinition<Id, S, G, A> {
const { id, state, getters, actions } = options

return function useStore(pinia?: Pinia | null): Store<Id, S, G, A> {
function useStore(pinia?: Pinia | null): Store<Id, S, G, A> {
// avoid injecting if `useStore` when not possible
pinia = pinia || (getCurrentInstance() && inject(piniaSymbol))
if (pinia) setActivePinia(pinia)
Expand Down Expand Up @@ -318,4 +319,9 @@ export function defineStore<
actions as Record<string, Method> | undefined
)
}

// used by devtools
useStore.$id = id

return useStore
}
28 changes: 27 additions & 1 deletion src/types.ts
Expand Up @@ -152,7 +152,20 @@ export type Store<
PiniaCustomProperties<Id, S, G, A>

/**
* Generic store type
* Return type of `defineStore()`. Function that allows instantiating a store.
*/
export interface StoreDefinition<
Id extends string,
S extends StateTree,
G /* extends Record<string, StoreGetterThis> */,
A /* extends Record<string, StoreAction> */
> {
(pinia?: Pinia | null | undefined): Store<Id, S, G, A>
$id: Id
}

/**
* Generic version of Store
*/
export type GenericStore = Store<
string,
Expand All @@ -161,6 +174,19 @@ export type GenericStore = Store<
Record<string, Method>
>

/**
* Generic version of `StoreDefinition`
*/
export interface GenericStoreDefinition {
(pinia?: Pinia | null | undefined): Store<
string,
StateTree,
Record<string, Method>,
Record<string, Method>
>
$id: string
}

/**
* Properties that are added to every store by `pinia.use()`
*/
Expand Down

0 comments on commit d3d9327

Please sign in to comment.