forked from tentwentyfour/callcise-vuex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (63 loc) · 2.28 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const { Optional } = require('lonad');
const Callcise = require('callcise');
const pipe = require('lodash.flow');
const evaluate = Callcise.internal.evaluate;
const namespacedVuexInvokers = (optionalNamespace = Optional.None()) => {
/*
* Returns a function that calls a VueX
* getter identified by its name that gets
* a function. It takes pre-bound, evaluated
* arguments and call-place arguments.
*/
function vuexQuery(queryName, ...parameters) {
return function query(...additionalParameters) {
const getter = this.$store.getters[makeMethodName(queryName)];
return getter(...evaluate(this, parameters).concat(additionalParameters));
};
}
// Returns a function that calls a VueX getter identified by its name.
function vuexGetter(getterName) {
return function getter() {
return this.$store.getters[makeMethodName(getterName)];
};
}
/*
* Returns a function that calls a VueX mutation that either takes a pre-bound
* evaluated payload or one that's passed at the call-place (or none at all).
*/
function vuexMutation(mutationName, predefinedPayload) {
return function mutation(callbackPayload) {
let payload = predefinedPayload ? evaluate(this, [predefinedPayload])[0] : callbackPayload;
return this.$store.commit(makeMethodName(mutationName), payload);
};
}
// Returns a function that calls a VueX action with evaluated arguments plus call-place arguments.
function vuexAction(actionName, ...parameters) {
return function action(...additionalParameters) {
return this.$store.dispatch(
makeMethodName(actionName),
...evaluate(this, parameters).concat(additionalParameters)
);
};
}
return {
query: vuexQuery,
getter: vuexGetter,
action: vuexAction,
mutation: vuexMutation
};
function makeMethodName(vuexMethodName) {
return optionalNamespace
.map(namespace => `${namespace}/${vuexMethodName}`)
.getOrElse(vuexMethodName);
}
};
const globalVuex = namespacedVuexInvokers();
module.exports = {
// Returns namespaced VueX invoker builders.
namespaced: pipe(Optional.Some, namespacedVuexInvokers),
vuexMutation: globalVuex.mutation,
vuexGetter: globalVuex.getter,
vuexAction: globalVuex.action,
vuexQuery: globalVuex.query
};