Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Commit

Permalink
made code drier
Browse files Browse the repository at this point in the history
  • Loading branch information
Goldziher committed May 8, 2020
1 parent a516099 commit b7d3b46
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 45 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"keywords": [
"vuex",
"composition-api",
"hooks"
"hooks",
"typescript"
],
"author": "Na'aman Hirschfeld",
"license": "ISC",
Expand Down
81 changes: 37 additions & 44 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mapActions, mapGetters, mapMutations, mapState, Store } from 'vuex'
import compositionApi, { computed, Ref } from '@vue/composition-api'
import { Store, mapActions, mapGetters, mapMutations, mapState } from 'vuex'
import compositionApi, { Ref, computed } from '@vue/composition-api'
import vue, { VueConstructor } from 'vue'

export type VuexStore<R> = Store<R> & {
Expand Down Expand Up @@ -101,44 +101,53 @@ const getModuleKeys = (
return Object.keys(subModule)
}

function generateComputedDict<S>(
mapper: ReturnType<typeof mapState> | ReturnType<typeof mapGetters>,
): MapToComputed<S> {
return Object.entries(mapper)
.map(
([key, value]) =>
[key, computed(() => value.call(_context.getVm()))] as [
string,
Computed<any>,
],
)
.reduce(
(obj, [key, value]) => ((obj[key] = value), obj),
{} as { [k: string]: Computed<any> },
) as MapToComputed<S>
}

function generateMethodDict<S, F>(
mapper: ReturnType<typeof mapActions> | ReturnType<typeof mapMutations>,
): FunctionMap<S, F> {
return Object.entries(mapper)
.map(
([key, value]) =>
[key, value.bind(_context.getVm())] as [string, Function],
)
.reduce(
(obj, [key, value]) => ((obj[key] = value), obj),
{} as { [k: string]: any },
) as FunctionMap<S, F>
}

export function useState<S = any>(namespace: string): MapToComputed<S> {
validateNamespace(namespace, 'useState')
if (!_context.state[namespace]) {
_context.state[namespace] = Object.entries(
_context.state[namespace] = generateComputedDict(
mapState(namespace, getModuleKeys(namespace, 'state')),
)
.map(
([key, value]) =>
[key, computed(() => value.call(_context.getVm()))] as [
string,
Computed<any>,
],
)
.reduce(
(obj, [key, value]) => ((obj[key] = value), obj),
{} as { [k: string]: Computed<any> },
)
}
return _context.state[namespace]
}

export function useGetters<S = any>(namespace: string): MapToComputed<S> {
validateNamespace(namespace, 'useGetters')
if (!_context.getters[namespace]) {
_context.getters[namespace] = Object.entries(
_context.getters[namespace] = generateComputedDict(
mapGetters(namespace, getModuleKeys(namespace, 'getters')),
)
.map(
([key, value]) =>
[key, computed(() => value.call(_context.getVm()))] as [
string,
Computed<any>,
],
)
.reduce(
(obj, [key, value]) => ((obj[key] = value), obj),
{} as { [k: string]: Computed<any> },
)
}
return _context.getters[namespace]
}
Expand All @@ -148,17 +157,9 @@ export function useMutations<S = any>(
): FunctionMap<S, VuexMutation> {
validateNamespace(namespace, 'useMutations')
if (!_context.mutations[namespace]) {
_context.mutations[namespace] = Object.entries(
_context.mutations[namespace] = generateMethodDict<S, VuexMutation>(
mapMutations(namespace, getModuleKeys(namespace, 'mutations')),
)
.map(
([key, value]) =>
[key, value.bind(_context.getVm())] as [string, Function],
)
.reduce(
(obj, [key, value]) => ((obj[key] = value), obj),
{} as { [k: string]: Function },
)
}
return _context.mutations[namespace]
}
Expand All @@ -168,17 +169,9 @@ export function useActions<S = any>(
): FunctionMap<S, VuexAction> {
validateNamespace(namespace, 'useActions')
if (!_context.actions[namespace]) {
_context.actions[namespace] = Object.entries(
_context.actions[namespace] = generateMethodDict<S, VuexAction>(
mapActions(namespace, getModuleKeys(namespace, 'actions')),
)
.map(
([key, value]) =>
[key, value.bind(_context.getVm())] as [string, Function],
)
.reduce(
(obj, [key, value]) => ((obj[key] = value), obj),
{} as { [k: string]: Function },
)
}
return _context.actions[namespace]
}

0 comments on commit b7d3b46

Please sign in to comment.