]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
docs: adapt pinia proposal
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 6 Dec 2019 14:44:34 +0000 (15:44 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 6 Dec 2019 14:44:52 +0000 (15:44 +0100)
README.md
__tests__/pinia/stores/combined.ts
src/devtools.ts
src/pinia.ts [new file with mode: 0644]
src/store.ts

index 0284d2ae162c7bce7744c7175f6c34b053eb9238..3301f65495ed5507ecb665c573302123b974aea9 100644 (file)
--- a/README.md
+++ b/README.md
@@ -248,10 +248,16 @@ import { pinia } from 'pinia'
 import { useUserStore } from './user'
 import { useCartStore, emptyCart } from './cart'
 
-export const useCartUserStore = pinia([useUserStore, useCartStore], {
-  combinedGetter: state =>
-    `Hi ${user.state.name}, you have ${cart.state.list.length} items in your cart. It costs ${cart.price}.`,
-})
+export const useCartUserStore = pinia(
+  {
+    user: useUserStore,
+    cart: useCartStore,
+  },
+  {
+    combinedGetter: state =>
+      `Hi ${user.state.name}, you have ${cart.state.list.length} items in your cart. It costs ${cart.price}.`,
+  }
+)
 
 export async function orderCart() {
   const store = useCartUserStore()
index 939ce7063a93e3ec8ffab7f978a507c826d40585..924ba95972f9b09d82bdfb4bb93a5063ba37fd71 100644 (file)
@@ -1,2 +1,15 @@
+import { useUserStore } from './user'
+import { useCartStore } from './cart'
+import { pinia, CombinedState } from '../../../src/pinia'
 // in this file we could import other stores that use one each other while
 // avoiding any recursive import
+
+type S = CombinedState<{
+  user: typeof useUserStore
+  cart: typeof useCartStore
+}>
+
+let a: S
+
+a.user.isAdmin = false
+a.cart.rawItems.push()
index 1804245eba4b2e38193cb41b8cba7b4d75bc272f..9f9e6a2226d200312b54be4e7a17c44186082b90 100644 (file)
@@ -29,7 +29,7 @@ interface RootState {
 
 let rootStore: RootState
 
-export function devtoolPlugin(store: Store<string, StateTree>) {
+export function useStoreDevtools(store: Store<string, StateTree>) {
   if (!devtoolHook) return
 
   if (!rootStore) {
diff --git a/src/pinia.ts b/src/pinia.ts
new file mode 100644 (file)
index 0000000..ce7d937
--- /dev/null
@@ -0,0 +1,62 @@
+import { Store, StoreGetter, StateTree, StoreGetters } from './types'
+import { CombinedStore } from './store'
+
+export type CombinedState<
+  S extends Record<
+    string,
+    (
+      ...args: any[]
+    ) => CombinedStore<
+      string,
+      StateTree,
+      Record<string, StoreGetter<StateTree>>
+    >
+  >
+> = {
+  [k in keyof S]: S[k] extends (
+    ...args: any[]
+  ) => CombinedStore<
+    string,
+    infer State,
+    Record<string, StoreGetter<infer State>>
+  >
+    ? State
+    : never
+}
+
+export type CombinedGetters<
+  S extends Record<
+    string,
+    (
+      ...args: any[]
+    ) => CombinedStore<
+      string,
+      StateTree,
+      Record<string, StoreGetter<StateTree>>
+    >
+  >
+> = {
+  [k in keyof S]: S[k] extends (
+    ...args: any[]
+  ) => CombinedStore<string, infer State, infer Getters>
+    ? StoreGetters<State, Getters>
+    : never
+}
+
+export function pinia<
+  S extends Record<
+    string,
+    (
+      ...args: any[]
+    ) => CombinedStore<
+      string,
+      StateTree,
+      Record<string, StoreGetter<StateTree>>
+    >
+  >
+>(stores: S): Store<'', CombinedState<S>> & CombinedGetters<S> {
+  // TODO: implement if makes sense
+
+  // @ts-ignore
+  return {}
+}
index be2b5581443c91ed944e8a2b9ce065b60e986907..958f8a33bc6dcd3e8ae412235155d838b7a5c2ff 100644 (file)
@@ -9,7 +9,7 @@ import {
   StoreGetters,
   StoreGetter,
 } from './types'
-import { devtoolPlugin } from './devtools'
+import { useStoreDevtools } from './devtools'
 
 function innerPatch<T extends StateTree>(
   target: T,
@@ -35,7 +35,7 @@ function innerPatch<T extends StateTree>(
  * they want, no? like user/cart
  */
 
-type CombinedStore<
+export type CombinedStore<
   Id extends string,
   S extends StateTree,
   G extends Record<string, StoreGetter<S>>
@@ -134,9 +134,6 @@ export function buildStore<
     },
   })
 
-  // Devtools injection hue hue
-  devtoolPlugin(store)
-
   return store
 }
 
@@ -163,6 +160,8 @@ export function createStore<
   return function useStore(forceNewStore = false): CombinedStore<Id, S, G> {
     if (!store || forceNewStore) store = buildStore(id, buildState, getters)
 
+    useStoreDevtools(store)
+
     return store
   }
 }