From c10fa4249c087d8d119f615eabe2cb6b57c4f5c7 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 6 Dec 2019 15:44:34 +0100 Subject: [PATCH] docs: adapt pinia proposal --- README.md | 14 +++++-- __tests__/pinia/stores/combined.ts | 13 +++++++ src/devtools.ts | 2 +- src/pinia.ts | 62 ++++++++++++++++++++++++++++++ src/store.ts | 9 ++--- 5 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 src/pinia.ts diff --git a/README.md b/README.md index 0284d2ae..3301f654 100644 --- 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() diff --git a/__tests__/pinia/stores/combined.ts b/__tests__/pinia/stores/combined.ts index 939ce706..924ba959 100644 --- a/__tests__/pinia/stores/combined.ts +++ b/__tests__/pinia/stores/combined.ts @@ -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() diff --git a/src/devtools.ts b/src/devtools.ts index 1804245e..9f9e6a22 100644 --- a/src/devtools.ts +++ b/src/devtools.ts @@ -29,7 +29,7 @@ interface RootState { let rootStore: RootState -export function devtoolPlugin(store: Store) { +export function useStoreDevtools(store: Store) { if (!devtoolHook) return if (!rootStore) { diff --git a/src/pinia.ts b/src/pinia.ts new file mode 100644 index 00000000..ce7d9375 --- /dev/null +++ b/src/pinia.ts @@ -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> + > + > +> = { + [k in keyof S]: S[k] extends ( + ...args: any[] + ) => CombinedStore< + string, + infer State, + Record> + > + ? State + : never +} + +export type CombinedGetters< + S extends Record< + string, + ( + ...args: any[] + ) => CombinedStore< + string, + StateTree, + Record> + > + > +> = { + [k in keyof S]: S[k] extends ( + ...args: any[] + ) => CombinedStore + ? StoreGetters + : never +} + +export function pinia< + S extends Record< + string, + ( + ...args: any[] + ) => CombinedStore< + string, + StateTree, + Record> + > + > +>(stores: S): Store<'', CombinedState> & CombinedGetters { + // TODO: implement if makes sense + + // @ts-ignore + return {} +} diff --git a/src/store.ts b/src/store.ts index be2b5581..958f8a33 100644 --- a/src/store.ts +++ b/src/store.ts @@ -9,7 +9,7 @@ import { StoreGetters, StoreGetter, } from './types' -import { devtoolPlugin } from './devtools' +import { useStoreDevtools } from './devtools' function innerPatch( target: T, @@ -35,7 +35,7 @@ function innerPatch( * they want, no? like user/cart */ -type CombinedStore< +export type CombinedStore< Id extends string, S extends StateTree, G extends Record> @@ -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 { if (!store || forceNewStore) store = buildStore(id, buildState, getters) + useStoreDevtools(store) + return store } } -- 2.47.2