From: Eduardo San Martin Morote Date: Fri, 12 Mar 2021 16:15:11 +0000 (+0100) Subject: feat: add mapStores X-Git-Tag: v2.0.0-alpha.11~26 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d3d9327918081feaffe63b7debf4e5dbbcf55890;p=thirdparty%2Fvuejs%2Fpinia.git feat: add mapStores --- diff --git a/src/index.ts b/src/index.ts index af105179..11b72d96 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,5 +15,6 @@ export { DefineStoreOptions, } from './types' +export { mapStores } from './mapHelpers' // TODO: remove in beta export { createStore } from './deprecated' diff --git a/src/store.ts b/src/store.ts index ed350ea8..c4f58124 100644 --- a/src/store.ts +++ b/src/store.ts @@ -12,6 +12,7 @@ import { Method, PiniaCustomProperties, DefineStoreOptions, + StoreDefinition, } from './types' import { getActivePinia, @@ -258,10 +259,10 @@ export function defineStore< S extends StateTree, G /* extends Record */, A /* extends Record */ ->(options: DefineStoreOptions) { +>(options: DefineStoreOptions): StoreDefinition { const { id, state, getters, actions } = options - return function useStore(pinia?: Pinia | null): Store { + function useStore(pinia?: Pinia | null): Store { // avoid injecting if `useStore` when not possible pinia = pinia || (getCurrentInstance() && inject(piniaSymbol)) if (pinia) setActivePinia(pinia) @@ -318,4 +319,9 @@ export function defineStore< actions as Record | undefined ) } + + // used by devtools + useStore.$id = id + + return useStore } diff --git a/src/types.ts b/src/types.ts index 42a9dd51..1df5055a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -152,7 +152,20 @@ export type Store< PiniaCustomProperties /** - * 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 */, + A /* extends Record */ +> { + (pinia?: Pinia | null | undefined): Store + $id: Id +} + +/** + * Generic version of Store */ export type GenericStore = Store< string, @@ -161,6 +174,19 @@ export type GenericStore = Store< Record > +/** + * Generic version of `StoreDefinition` + */ +export interface GenericStoreDefinition { + (pinia?: Pinia | null | undefined): Store< + string, + StateTree, + Record, + Record + > + $id: string +} + /** * Properties that are added to every store by `pinia.use()` */