-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add inline namespace for global helpers (#58)
- Loading branch information
Showing
8 changed files
with
548 additions
and
109 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,120 @@ | ||
import {Store} from 'vuex/types'; | ||
import {computed} from '@vue/composition-api'; | ||
import {computedGetter, getAction, getMutation, getStoreFromInstance, useMapping, ExtractGetterTypes, ExtractTypes, KnownKeys, RefTypes} from './util'; | ||
import {computedGetter, getAction, getMutation, getStoreFromInstance, useMapping, ExtractGetterTypes, ExtractTypes, KnownKeys, RefTypes, Namespace, Nullish} from './util'; | ||
|
||
function computedState(store: any, prop: string) { | ||
return computed(() => store.state[prop]); | ||
} | ||
|
||
function computedNamespacedState(store: any, namespace: string, prop: string) { | ||
let module = namespace.split('/').reduce((module, key) => module[key], store.state) | ||
return computed(() => module[prop]) | ||
} | ||
|
||
export function useStore<TState = any>() { | ||
return getStoreFromInstance() as Store<TState> | ||
} | ||
|
||
export function useState<TState = any>(storeOrMap: Store<TState> | KnownKeys<TState>[], map?: KnownKeys<TState>[]): RefTypes<TState> { | ||
let store = storeOrMap; | ||
|
||
if (arguments.length === 1) { | ||
map = store as KnownKeys<TState>[]; | ||
store = getStoreFromInstance(); | ||
export function useState<TState = any>(map: KnownKeys<TState>[]): RefTypes<TState>; | ||
export function useState<TState = any>(storeOrNamespace: Store<any> | Namespace, map?: KnownKeys<TState>[]): RefTypes<TState>; | ||
export function useState<TState = any>(store: Store<any> | Nullish, namespace?: Namespace, map?: KnownKeys<TState>[]): RefTypes<TState>; | ||
export function useState<TState = any>(storeOrNamespace: KnownKeys<TState>[] | Store<any> | Namespace, namespaceOrMap?: KnownKeys<TState>[] | Namespace, map?: KnownKeys<TState>[]): RefTypes<TState> { | ||
let realStore, realNamespace, realMap; | ||
if (arguments.length >= 3) { | ||
realStore = storeOrNamespace as Store<any> || getStoreFromInstance(); | ||
realNamespace = namespaceOrMap as string || null; | ||
realMap = map as KnownKeys<TState>[] || null; | ||
} else if (arguments.length === 2) { | ||
if (typeof storeOrNamespace === 'string') { | ||
realStore = getStoreFromInstance(); | ||
realNamespace = storeOrNamespace as string || null; | ||
} else { | ||
realStore = storeOrNamespace as Store<any> || getStoreFromInstance(); | ||
realNamespace = null | ||
} | ||
realMap = namespaceOrMap as KnownKeys<TState>[] || null; | ||
} else { | ||
realStore = getStoreFromInstance(); | ||
realNamespace = null; | ||
realMap = storeOrNamespace as KnownKeys<TState>[] || null; | ||
} | ||
return useMapping(store, null, map, computedState); | ||
return useMapping(realStore, realNamespace, realMap, !realNamespace ? computedState : computedNamespacedState); | ||
} | ||
|
||
export function useGetters<TGetters = any>(storeOrMap: Store<any> | KnownKeys<TGetters>[], map?: KnownKeys<TGetters>[]): ExtractGetterTypes<TGetters> { | ||
let store = storeOrMap; | ||
if (arguments.length === 1) { | ||
map = store as KnownKeys<TGetters>[]; | ||
store = getStoreFromInstance(); | ||
export function useGetters<TGetters = any>(map: KnownKeys<TGetters>[]): ExtractGetterTypes<TGetters>; | ||
export function useGetters<TGetters = any>(storeOrNamespace: Store<any> | Namespace, map?: KnownKeys<TGetters>[]): ExtractGetterTypes<TGetters>; | ||
export function useGetters<TGetters = any>(store: Store<any> | Nullish, namespace?: Namespace, map?: KnownKeys<TGetters>[]): ExtractGetterTypes<TGetters>; | ||
export function useGetters<TGetters = any>(storeOrNamespace: KnownKeys<TGetters>[] | Store<any> | Namespace, namespaceOrMap?: KnownKeys<TGetters>[] | Namespace, map?: KnownKeys<TGetters>[]): ExtractGetterTypes<TGetters> { | ||
let realStore, realNamespace, realMap; | ||
if (arguments.length >= 3) { | ||
realStore = storeOrNamespace as Store<any> || getStoreFromInstance(); | ||
realNamespace = namespaceOrMap as string || null; | ||
realMap = map as KnownKeys<TGetters>[] || null; | ||
} else if (arguments.length === 2) { | ||
if (typeof storeOrNamespace === 'string') { | ||
realStore = getStoreFromInstance(); | ||
realNamespace = storeOrNamespace as string || null; | ||
} else { | ||
realStore = storeOrNamespace as Store<any> || getStoreFromInstance(); | ||
realNamespace = null | ||
} | ||
realMap = namespaceOrMap as KnownKeys<TGetters>[] || null; | ||
} else { | ||
realStore = getStoreFromInstance(); | ||
realNamespace = null; | ||
realMap = storeOrNamespace as KnownKeys<TGetters>[] || null; | ||
} | ||
return useMapping(store, null, map, computedGetter); | ||
return useMapping(realStore, realNamespace, realMap, computedGetter); | ||
} | ||
|
||
export function useMutations<TMutations = any>(storeOrMap: Store<any> | KnownKeys<TMutations>[], map?: KnownKeys<TMutations>[]): ExtractTypes<TMutations, Function> { | ||
let store = storeOrMap; | ||
|
||
if (arguments.length === 1) { | ||
map = store as KnownKeys<TMutations>[]; | ||
store = getStoreFromInstance(); | ||
export function useMutations<TMutations = any>(map: KnownKeys<TMutations>[]): ExtractTypes<TMutations, Function>; | ||
export function useMutations<TMutations = any>(storeOrNamespace: Store<any> | Namespace, map?: KnownKeys<TMutations>[]): ExtractTypes<TMutations, Function>; | ||
export function useMutations<TMutations = any>(store: Store<any> | Nullish, namespace?: Namespace, map?: KnownKeys<TMutations>[]): ExtractTypes<TMutations, Function>; | ||
export function useMutations<TMutations = any>(storeOrNamespace: KnownKeys<TMutations>[] | Store<any> | Namespace, namespaceOrMap?: KnownKeys<TMutations>[] | Namespace, map?: KnownKeys<TMutations>[]): ExtractTypes<TMutations, Function> { | ||
let realStore, realNamespace, realMap; | ||
if (arguments.length >= 3) { | ||
realStore = storeOrNamespace as Store<any> || getStoreFromInstance(); | ||
realNamespace = namespaceOrMap as string || null; | ||
realMap = map as KnownKeys<TMutations>[] || null; | ||
} else if (arguments.length === 2) { | ||
if (typeof storeOrNamespace === 'string') { | ||
realStore = getStoreFromInstance(); | ||
realNamespace = storeOrNamespace as string || null; | ||
} else { | ||
realStore = storeOrNamespace as Store<any> || getStoreFromInstance(); | ||
realNamespace = null | ||
} | ||
realMap = namespaceOrMap as KnownKeys<TMutations>[] || null; | ||
} else { | ||
realStore = getStoreFromInstance(); | ||
realNamespace = null; | ||
realMap = storeOrNamespace as KnownKeys<TMutations>[] || null; | ||
} | ||
return useMapping(store, null, map, getMutation); | ||
return useMapping(realStore, realNamespace, realMap, getMutation); | ||
} | ||
|
||
export function useActions<TActions = any>(storeOrMap: Store<any> | KnownKeys<TActions>[], map?: KnownKeys<TActions>[]): ExtractTypes<TActions, Function> { | ||
let store = storeOrMap; | ||
|
||
if (arguments.length === 1) { | ||
map = store as KnownKeys<TActions>[]; | ||
store = getStoreFromInstance(); | ||
export function useActions<TActions = any>(map: KnownKeys<TActions>[]): ExtractTypes<TActions, Function>; | ||
export function useActions<TActions = any>(storeOrNamespace: Store<any> | Namespace, map?: KnownKeys<TActions>[]): ExtractTypes<TActions, Function>; | ||
export function useActions<TActions = any>(store: Store<any> | Nullish, namespace?: Namespace, map?: KnownKeys<TActions>[]): ExtractTypes<TActions, Function>; | ||
export function useActions<TActions = any>(storeOrNamespace: KnownKeys<TActions>[] | Store<any> | Namespace, namespaceOrMap?: KnownKeys<TActions>[] | Namespace, map?: KnownKeys<TActions>[]): ExtractTypes<TActions, Function> { | ||
let realStore, realNamespace, realMap; | ||
if (arguments.length >= 3) { | ||
realStore = storeOrNamespace as Store<any> || getStoreFromInstance(); | ||
realNamespace = namespaceOrMap as string || null; | ||
realMap = map as KnownKeys<TActions>[] || null; | ||
} else if (arguments.length === 2) { | ||
if (typeof storeOrNamespace === 'string') { | ||
realStore = getStoreFromInstance(); | ||
realNamespace = storeOrNamespace as string || null; | ||
} else { | ||
realStore = storeOrNamespace as Store<any> || getStoreFromInstance(); | ||
realNamespace = null | ||
} | ||
realMap = namespaceOrMap as KnownKeys<TActions>[] || null; | ||
} else { | ||
realStore = getStoreFromInstance(); | ||
realNamespace = null; | ||
realMap = storeOrNamespace as KnownKeys<TActions>[] || null; | ||
} | ||
return useMapping(store, null, map, getAction); | ||
} | ||
return useMapping(realStore, realNamespace, realMap, getAction); | ||
} |
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
Oops, something went wrong.