diff --git a/doc/src/store/actions.js b/doc/src/store/actions.js deleted file mode 100644 index efba7fa..0000000 --- a/doc/src/store/actions.js +++ /dev/null @@ -1,2 +0,0 @@ -export default { -} diff --git a/doc/src/store/actions.ts b/doc/src/store/actions.ts new file mode 100644 index 0000000..4400f5a --- /dev/null +++ b/doc/src/store/actions.ts @@ -0,0 +1,25 @@ +import { globalHelper } from 'src/store/helpers' + +export const actions = globalHelper.makeActions({ + GET_SERVER_DATA(ctx, { id }: { id: number }) { + let resove + const promise = new Promise<{ id: number }>((res) => resove = res) + setTimeout(() => { + resove({ + id, + }) + }, 1000) + return promise + } +}) + +export default actions + +export const dispatch = globalHelper.createDispatch() + +// const store = this.store + +// dispatch(store, 'GET_SERVER_DATA', { id: 1 }).then(({ id }) => { +// id = 'st' +// id = 1 +// }) \ No newline at end of file diff --git a/doc/src/store/utils.ts b/doc/src/store/utils.ts index 8833312..f0229ea 100644 --- a/doc/src/store/utils.ts +++ b/doc/src/store/utils.ts @@ -1,12 +1,56 @@ import { Tstore } from '@types' -import { ActionContext, Store, MutationTree } from 'vuex' +import { ActionTree, Store, MutationTree } from 'vuex' export type SniffMutationPayload = T extends (state: any, payload: infer P) => any ? P: unknown export type SniffMutationPayloadTree> = { [K in keyof M]: SniffMutationPayload } +export type SniffActionPayload = T extends (state: any, payload: infer P) => infer V ? { payload: P, value: V }: { payload: unknown, value: unknown } +export type SniffActionPayloadTree> = { + [K in keyof M]: SniffActionPayload +} + export function makeWrapper(namespace: keyof Tstore.state = ('' as any)) { + + function createGetState() { + function getState( + context: Store, + stateKey: K, + ): T[K] + function getState( + context: Store, + stateKey: K, + state1Key: K1, + ): T[K][K1] + function getState( + context: Store, + stateKey: K, + state1Key: K1, + state2Key: K2, + ): T[K][K1][K2] + function getState( + context: Store, + stateKey: K, + state1Key: K1, + state2Key: K2, + state3Key: K3, + ): T[K][K1][K2][K3] + function getState(context: Store, ...args: string[]) { + if (!checkStore(context)) return + checkNamespace(namespace, args) + let result + for (let index = 0; index < args.length; index++) { + const key = args[index]; + if (!result) result = context.state[key] + else result = result[key] + if (!result) return + } + return result + } + return getState + } + function makeMutations>(mutationTree: M) { return mutationTree } @@ -54,48 +98,40 @@ export function makeWrapper(namespace: keyof Tstore.state = ('' as any)) { return commit } - function createGetState() { - function getState( - context: Store, - stateKey: K, - ): T[K] - function getState( - context: Store, - stateKey: K, - state1Key: K1, - ): T[K][K1] - function getState( - context: Store, - stateKey: K, - state1Key: K1, - state2Key: K2, - ): T[K][K1][K2] - function getState( + type TActionTree = ActionTree + + function makeActions(actionTree: A) { + return actionTree + } + + function createDispatch() { + type actionPayloadTree = SniffActionPayloadTree + function dispatch( context: Store, - stateKey: K, - state1Key: K1, - state2Key: K2, - state3Key: K3, - ): T[K][K1][K2][K3] - function getState(context: Store, ...args: string[]) { + type: M, + payload: actionPayloadTree[M]['payload'], + ): actionPayloadTree[M]['value'] + function dispatch(context: Store, ...args: any[]): any { if (!checkStore(context)) return - checkNamespace(namespace, args) - let result - for (let index = 0; index < args.length; index++) { - const key = args[index]; - if (!result) result = context.state[key] - else result = result[key] - if (!result) return + if (args.length < 2) { + console.error('commit args.length must > 2') + return } - return result + checkNamespace(namespace, args) + const payload = args.pop() + const paths = args.join('/') + return context.dispatch(paths, payload) } - return getState + + return dispatch } return { + createGetState, makeMutations, createCommit, - createGetState, + makeActions, + createDispatch, } }