]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat: add mapStores
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 12 Mar 2021 16:15:11 +0000 (17:15 +0100)
committerEduardo San Martin Morote <posva@users.noreply.github.com>
Fri, 9 Apr 2021 11:08:56 +0000 (13:08 +0200)
src/index.ts
src/store.ts
src/types.ts

index af105179dcc9e95ba7559fac7282116edbbe04fa..11b72d9603726f164dabeeb9db1f35f9b1cb58d4 100644 (file)
@@ -15,5 +15,6 @@ export {
   DefineStoreOptions,
 } from './types'
 
+export { mapStores } from './mapHelpers'
 // TODO: remove in beta
 export { createStore } from './deprecated'
index ed350ea82975b3747c56613011452caf31e147a4..c4f5812450c7428697d28354d60ea1352bca57a5 100644 (file)
@@ -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<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)
@@ -318,4 +319,9 @@ export function defineStore<
       actions as Record<string, Method> | undefined
     )
   }
+
+  // used by devtools
+  useStore.$id = id
+
+  return useStore
 }
index 42a9dd51d625abc4185c42b1202ad52bc1ab1da2..1df5055a0c73135d07ef9b8d167832218af959af 100644 (file)
@@ -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,
@@ -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()`
  */