Skip to content

Commit

Permalink
feat: vuex mutation 完善全局 commit
Browse files Browse the repository at this point in the history
  • Loading branch information
imsunhao committed Jul 29, 2019
1 parent 102ea87 commit c5b9adf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
23 changes: 13 additions & 10 deletions doc/@types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Store } from 'vuex'
import { TMutations as GlobalMutations } from 'src/store/mutations'

declare namespace Doc {
type DictOf<T> = { [key: string]: T }
Expand Down Expand Up @@ -75,20 +76,22 @@ declare namespace Doc {
}

/**
* vuex Mutation-Payload-tree
* vuex Mutation-tree
*/
interface MutationPayloads {
SET_IS_MOBILE: boolean
SET_HELLO: { hello: string }
SET_testHotLoadingVuex: { number: number }

type Mutations = GlobalMutations & {
/**
* 编辑器
*/
editor: {
SET_HELLO_EDITOR: string
test: undefined
test2?: string
editor: GlobalMutations & {
/**
* 编辑器
*/
editor2: GlobalMutations & {
/**
* 编辑器
*/
editor3: GlobalMutations
}
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions doc/src/store/mutations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { globalHelper } from 'src/store/helpers'
import { Tstore } from '@types'

export const mutations = globalHelper.makeMutations({
SET_IS_MOBILE: (state, isMobile: boolean) => {
Expand All @@ -15,10 +16,14 @@ export const mutations = globalHelper.makeMutations({

export default mutations

export const commit = globalHelper.createCommit<typeof mutations>()
export type TMutations = typeof mutations

export const commit = globalHelper.createCommit<Tstore.Mutations>()
export const getState = globalHelper.createGetState()

// const store = this.store

// commit(store, 'SET_IS_MOBILE', true)
// commit(store, 'editor', 'test', undefined)
// commit(store, 'editor', 'SET_IS_MOBILE', true)
// commit(store, 'editor', 'editor2', 'SET_IS_MOBILE', true)
// commit(store, 'editor', 'editor2', 'editor3', 'SET_IS_MOBILE', true)
14 changes: 7 additions & 7 deletions doc/src/store/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Tstore } from '@types'
import { ActionTree, Store, MutationTree } from 'vuex'

export type SniffMutationPayload<T> = T extends (state: any, payload: infer P) => any ? P: unknown
export type SniffMutationPayload<T> = T extends (state: any, payload: infer P) => any ? P: T
export type SniffMutationPayloadTree<S, M extends MutationTree<S>> = {
[K in keyof M]: SniffMutationPayload<M[K]>
}

export type SniffActionPayload<T> = T extends (state: any, payload: infer P) => infer V ? { payload: P, value: V }: { payload: unknown, value: unknown }
export type SniffActionPayloadTree<S, M extends ActionTree<S, any>> = {
export type SniffActionPayloadTree<S, M extends ActionTree<S, Tstore.state>> = {
[K in keyof M]: SniffActionPayload<M[K]>
}

Expand Down Expand Up @@ -55,8 +55,8 @@ export function makeWrapper<T>(namespace: keyof Tstore.state = ('' as any)) {
return mutationTree
}

function createCommit<M extends MutationTree<T>>() {
type MutationPayloadTree = SniffMutationPayloadTree<T, M>
function createCommit<Mutation extends MutationTree<T>>() {
type MutationPayloadTree = SniffMutationPayloadTree<T, Mutation>
function commit<M extends keyof MutationPayloadTree>(
context: Store<any>,
mutation: M,
Expand All @@ -66,22 +66,22 @@ export function makeWrapper<T>(namespace: keyof Tstore.state = ('' as any)) {
context: Store<any>,
path: P,
mutation: M,
payload: MutationPayloadTree[P][M],
payload: SniffMutationPayloadTree<T, MutationPayloadTree[P]>[M],
): void
function commit<P extends keyof MutationPayloadTree, P1 extends keyof MutationPayloadTree[P], M extends keyof MutationPayloadTree[P][P1]>(
context: Store<any>,
path: P,
path1: P1,
mutation: M,
payload: MutationPayloadTree[P][P1][M],
payload: SniffMutationPayloadTree<T, MutationPayloadTree[P][P1]>[M],
): void
function commit<P extends keyof MutationPayloadTree, P1 extends keyof MutationPayloadTree[P], P2 extends keyof MutationPayloadTree[P][P1], M extends keyof MutationPayloadTree[P][P1][P2]>(
context: Store<any>,
path: P,
path1: P1,
path2: P2,
mutation: M,
payload: MutationPayloadTree[P][P1][P2][M],
payload: SniffMutationPayloadTree<T, MutationPayloadTree[P][P1][P2]>[M],
): void
function commit(context: Store<any>, ...args: any[]) {
if (!checkStore(context)) return
Expand Down

0 comments on commit c5b9adf

Please sign in to comment.