-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
198 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Tstore } from '@types' | ||
import { makeWrapper } from 'src/store/utils' | ||
|
||
export const globalHelper = makeWrapper<Tstore.state>() | ||
export const editorHelper = makeWrapper<Tstore.state['editor']>('editor') | ||
|
||
/** | ||
* 例子 1 | ||
*/ | ||
// const store = this.store | ||
|
||
// const getState = globalHelper.createGetState() | ||
|
||
// const deepTest = getState(store, 'editor', 'deepTest') | ||
// const test1 = getState(store, 'editor', 'deepTest', 'test1') | ||
// const test2 = getState(store, 'editor', 'deepTest').test2 | ||
|
||
/** | ||
* 例子 2 | ||
*/ | ||
// const store = this.store | ||
// const getEditorState = editorHelper.createGetState() | ||
|
||
// const deepTest = getEditorState(store, 'deepTest') | ||
// const test1 = getEditorState(store, 'deepTest', 'test1') | ||
// const test2 = getEditorState(store, 'deepTest').test2 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,24 @@ | ||
import { Tstore } from "@types" | ||
import { globalHelper } from 'src/store/helpers' | ||
|
||
export const mutations: Tstore.createUtils.ModuleMutations<Tstore.state['global'], Tstore.MutationPayloads['global']> = { | ||
SET_IS_MOBILE: (state, isMobile) => { | ||
export const mutations = globalHelper.makeMutations({ | ||
SET_IS_MOBILE: (state, isMobile: boolean) => { | ||
console.log('SET_IS_MOBILE', isMobile) | ||
state.isMobile = isMobile | ||
}, | ||
SET_HELLO: (state, { hello }) => { | ||
SET_HELLO: (state, { hello }: { hello: string }) => { | ||
state.hello = hello | ||
}, | ||
SET_testHotLoadingVuex: (state, { number }) => { | ||
SET_testHotLoadingVuex: (state, { number }: { number: number }) => { | ||
state.testHotLoadingVuex += number * 1 | ||
}, | ||
} | ||
}) | ||
|
||
export default mutations | ||
export default mutations | ||
|
||
export const commit = globalHelper.createCommit<typeof mutations>() | ||
export const getState = globalHelper.createGetState() | ||
|
||
// const store = this.store | ||
|
||
// commit(store, 'SET_IS_MOBILE', true) | ||
// commit(store, 'editor', 'test', undefined) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { Tstore } from '@types' | ||
import { ActionContext, Store, MutationTree } from 'vuex' | ||
|
||
export type SniffMutationPayload<T> = T extends (state: any, payload: infer P) => any ? P: unknown | ||
export type SniffMutationPayloadTree<S, M extends MutationTree<S>> = { | ||
[K in keyof M]: SniffMutationPayload<M[K]> | ||
} | ||
|
||
export function makeWrapper<T>(namespace: keyof Tstore.state = ('' as any)) { | ||
function makeMutations<M extends MutationTree<T>>(mutationTree: M) { | ||
return mutationTree | ||
} | ||
|
||
function createCommit<M extends MutationTree<T>>() { | ||
type MutationPayloadTree = SniffMutationPayloadTree<T, M> | ||
function commit<M extends keyof MutationPayloadTree>( | ||
context: Store<any>, | ||
mutation: M, | ||
payload: MutationPayloadTree[M], | ||
): void | ||
function commit<P extends keyof MutationPayloadTree, M extends keyof MutationPayloadTree[P]>( | ||
context: Store<any>, | ||
path: P, | ||
mutation: M, | ||
payload: 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], | ||
): 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], | ||
): void | ||
function commit(context: Store<any>, ...args: any[]) { | ||
if (!checkStore(context)) return | ||
if (args.length < 2) { | ||
console.error('commit args.length must > 2') | ||
return | ||
} | ||
checkNamespace(namespace, args) | ||
const payload = args.pop() | ||
const paths = args.join('/') | ||
return context.commit(paths, payload) | ||
} | ||
|
||
return commit | ||
} | ||
|
||
function createGetState() { | ||
function getState<K extends keyof T>( | ||
context: Store<any>, | ||
stateKey: K, | ||
): T[K] | ||
function getState<K extends keyof T, K1 extends keyof T[K]>( | ||
context: Store<any>, | ||
stateKey: K, | ||
state1Key: K1, | ||
): T[K][K1] | ||
function getState<K extends keyof T, K1 extends keyof T[K], K2 extends keyof T[K][K1]>( | ||
context: Store<any>, | ||
stateKey: K, | ||
state1Key: K1, | ||
state2Key: K2, | ||
): T[K][K1][K2] | ||
function getState<K extends keyof T, K1 extends keyof T[K], K2 extends keyof T[K][K1], K3 extends keyof T[K][K1][K2]>( | ||
context: Store<any>, | ||
stateKey: K, | ||
state1Key: K1, | ||
state2Key: K2, | ||
state3Key: K3, | ||
): T[K][K1][K2][K3] | ||
function getState(context: Store<any>, ...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 | ||
} | ||
|
||
return { | ||
makeMutations, | ||
createCommit, | ||
createGetState, | ||
} | ||
} | ||
|
||
function isStore(context: Store<any>) { | ||
return 'strict' in context | ||
} | ||
|
||
function checkStore(context: Store<any>) { | ||
if (!isStore(context)) { | ||
console.error('checkStore context is not a vuex store!', context) | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
function checkNamespace(namespace: keyof Tstore.state, args: any[]) { | ||
if (namespace) { | ||
args.unshift(namespace) | ||
} | ||
} |