]> 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>
Thu, 8 Apr 2021 12:55:28 +0000 (14:55 +0200)
__tests__/tds/store.test-d.ts
src/index.ts
src/store.ts
src/types.ts

index 67db2a6bad5f02a95bc212fc9f16085620e649b0..b998854b6c3a13fb61ddf3a9b0a2d736c5cf43b6 100644 (file)
@@ -1,5 +1,6 @@
-import { defineStore } from '../../src'
+import { defineStore, mapStores, Store } from '../../src'
 import { expectType, expectError } from 'tsd'
+import { StoreDefinition } from 'src/types'
 
 const useStore = defineStore({
   id: 'name',
@@ -11,10 +12,31 @@ const useStore = defineStore({
   },
 })
 
+const useCounter = defineStore({
+  id: 'counter',
+  state: () => ({ n: 0 }),
+})
+
+const useStoreDos = defineStore({
+  id: 'dos',
+  state: () => ({}),
+})
+
 const store = useStore()
 
 expectType<{ a: 'on' | 'off' }>(store.$state)
+expectType<string>(store.upper)
 
 expectType<{ upper: string }>(store)
 
 expectError(() => store.nonExistant)
+
+type MainStore = ReturnType<typeof useStore>
+type DosStore = ReturnType<typeof useStoreDos>
+type CounterStore = ReturnType<typeof useCounter>
+
+expectType<{
+  name: () => MainStore
+  dos: () => DosStore
+  counter: () => CounterStore
+}>(mapStores(useStore, useStoreDos, useCounter))
index 50217f25b4f2f7443c0d296c0948f41fe5216683..97725ae7cfdf27feaf696f3f6967f779e3436983 100644 (file)
@@ -15,5 +15,6 @@ export {
   PiniaCustomProperties,
 } from './types'
 
+export { mapStores } from './mapHelpers'
 // TODO: remove in beta
 export { createStore } from './deprecated'
index 8eff1e268cc778349b378f7b4ef7a6b1cb3d91d2..64beb7c0658e3da9aae4621896c81248363a8bbe 100644 (file)
@@ -19,6 +19,7 @@ import {
   Method,
   StateDescriptor,
   PiniaCustomProperties,
+  StoreDefinition,
 } from './types'
 import { useStoreDevtools } from './devtools'
 import {
@@ -276,10 +277,10 @@ export function defineStore<
         StoreWithGetters<G> &
         PiniaCustomProperties
     >
-}) {
+}): 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> {
     // const vm = getCurrentInstance()
     // pinia = pinia || (vm && ((vm as any).$pinia as Pinia))
     pinia = pinia || (getCurrentInstance() && inject(piniaSymbol))
@@ -324,4 +325,9 @@ export function defineStore<
       actions as Record<string, Method> | undefined
     )
   }
+
+  // used by devtools
+  useStore.$id = id
+
+  return useStore
 }
index 3c774617b9892adae7f16cb279d3a5295da8c8b7..736fd9458cb7cc83295c02e9549eed9e2cdea376 100644 (file)
@@ -124,6 +124,22 @@ export type Store<
   StoreWithActions<A> &
   PiniaCustomProperties<Id, S, G, A>
 
+/**
+ * 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,
   StateTree,
@@ -131,6 +147,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
+}
+
 export interface DevtoolHook {
   on(
     event: string,