Skip to content

Commit 2665772

Browse files
author
profreelancer222
committed
Refactor usability tests.
Co-authored-by: name <[email protected]> Co-authored-by: another-name <[email protected]>
1 parent d331e56 commit 2665772

File tree

1 file changed

+42
-44
lines changed

1 file changed

+42
-44
lines changed

src/helpers.js

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,5 @@
11
import { isObject } from './util'
22

3-
/**
4-
* Reduce the code which written in Vue.js for committing the mutation
5-
* @param {String} [namespace] - Module's namespace
6-
* @param {Object|Array} mutations # Object's item can be a function which accept `commit` function as the first param, it can accept another params. You can commit mutation and do any other things in this function. specially, You need to pass anthor params from the mapped function.
7-
* @return {Object}
8-
*/
9-
export const mapMutations = normalizeNamespace((namespace, mutations) => {
10-
const res = {}
11-
if (__DEV__ && !isValidMap(mutations)) {
12-
console.error('[vuex] mapMutations: mapper parameter must be either an Array or an Object')
13-
}
14-
normalizeMap(mutations).forEach(({ key, val }) => {
15-
res[key] = function mappedMutation (...args) {
16-
// Get the commit method from store
17-
let commit = this.$store.commit
18-
if (namespace) {
19-
const module = getModuleByNamespace(this.$store, 'mapMutations', namespace)
20-
if (!module) {
21-
return
22-
}
23-
commit = module.context.commit
24-
}
25-
return typeof val === 'function'
26-
? val.apply(this, [commit].concat(args))
27-
: commit.apply(this.$store, [val].concat(args))
28-
}
29-
})
30-
return res
31-
})
32-
333
/**
344
* Reduce the code which written in Vue.js for getting the state.
355
* @param {String} [namespace] - Module's namespace
@@ -64,30 +34,30 @@ export const mapState = normalizeNamespace((namespace, states) => {
6434
})
6535

6636
/**
67-
* Reduce the code which written in Vue.js for dispatch the action
37+
* Reduce the code which written in Vue.js for committing the mutation
6838
* @param {String} [namespace] - Module's namespace
69-
* @param {Object|Array} actions # Object's item can be a function which accept `dispatch` function as the first param, it can accept anthor params. You can dispatch action and do any other things in this function. specially, You need to pass anthor params from the mapped function.
39+
* @param {Object|Array} mutations # Object's item can be a function which accept `commit` function as the first param, it can accept another params. You can commit mutation and do any other things in this function. specially, You need to pass anthor params from the mapped function.
7040
* @return {Object}
7141
*/
72-
export const mapActions = normalizeNamespace((namespace, actions) => {
42+
export const mapMutations = normalizeNamespace((namespace, mutations) => {
7343
const res = {}
74-
if (__DEV__ && !isValidMap(actions)) {
75-
console.error('[vuex] mapActions: mapper parameter must be either an Array or an Object')
44+
if (__DEV__ && !isValidMap(mutations)) {
45+
console.error('[vuex] mapMutations: mapper parameter must be either an Array or an Object')
7646
}
77-
normalizeMap(actions).forEach(({ key, val }) => {
78-
res[key] = function mappedAction (...args) {
79-
// get dispatch function from store
80-
let dispatch = this.$store.dispatch
47+
normalizeMap(mutations).forEach(({ key, val }) => {
48+
res[key] = function mappedMutation (...args) {
49+
// Get the commit method from store
50+
let commit = this.$store.commit
8151
if (namespace) {
82-
const module = getModuleByNamespace(this.$store, 'mapActions', namespace)
52+
const module = getModuleByNamespace(this.$store, 'mapMutations', namespace)
8353
if (!module) {
8454
return
8555
}
86-
dispatch = module.context.dispatch
56+
commit = module.context.commit
8757
}
8858
return typeof val === 'function'
89-
? val.apply(this, [dispatch].concat(args))
90-
: dispatch.apply(this.$store, [val].concat(args))
59+
? val.apply(this, [commit].concat(args))
60+
: commit.apply(this.$store, [val].concat(args))
9161
}
9262
})
9363
return res
@@ -123,7 +93,35 @@ export const mapGetters = normalizeNamespace((namespace, getters) => {
12393
return res
12494
})
12595

126-
96+
/**
97+
* Reduce the code which written in Vue.js for dispatch the action
98+
* @param {String} [namespace] - Module's namespace
99+
* @param {Object|Array} actions # Object's item can be a function which accept `dispatch` function as the first param, it can accept anthor params. You can dispatch action and do any other things in this function. specially, You need to pass anthor params from the mapped function.
100+
* @return {Object}
101+
*/
102+
export const mapActions = normalizeNamespace((namespace, actions) => {
103+
const res = {}
104+
if (__DEV__ && !isValidMap(actions)) {
105+
console.error('[vuex] mapActions: mapper parameter must be either an Array or an Object')
106+
}
107+
normalizeMap(actions).forEach(({ key, val }) => {
108+
res[key] = function mappedAction (...args) {
109+
// get dispatch function from store
110+
let dispatch = this.$store.dispatch
111+
if (namespace) {
112+
const module = getModuleByNamespace(this.$store, 'mapActions', namespace)
113+
if (!module) {
114+
return
115+
}
116+
dispatch = module.context.dispatch
117+
}
118+
return typeof val === 'function'
119+
? val.apply(this, [dispatch].concat(args))
120+
: dispatch.apply(this.$store, [val].concat(args))
121+
}
122+
})
123+
return res
124+
})
127125

128126
/**
129127
* Rebinding namespace param for mapXXX function in special scoped, and return them by simple object

0 commit comments

Comments
 (0)