-
Notifications
You must be signed in to change notification settings - Fork 394
Expand file tree
/
Copy pathmapStore.js
More file actions
140 lines (131 loc) · 3.95 KB
/
Copy pathmapStore.js
File metadata and controls
140 lines (131 loc) · 3.95 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import {
warn,
error,
getByPath,
isObject
} from '@mpxjs/utils'
import { computed } from '@mpxjs/core'
function normalizeMap (prefix, arr) {
if (typeof prefix !== 'string') {
arr = prefix
prefix = ''
}
if (Array.isArray(arr)) {
const map = {}
arr.forEach(value => {
map[value] = prefix ? `${prefix}.${value}` : value
})
return map
}
if (prefix && isObject(arr)) {
arr = Object.assign({}, arr)
Object.keys(arr).forEach(key => {
if (typeof arr[key] === 'string') {
arr[key] = `${prefix}.${arr[key]}`
}
})
}
return arr
}
function mapFactory (type, store) {
return function (depPath, maps) {
maps = normalizeMap(depPath, maps)
const result = {}
Object.entries(maps).forEach(([key, value]) => {
result[key] = function (payload) {
switch (type) {
case 'state':
if (typeof value === 'function') {
return value.call(this, store.state, store.getters)
} else {
let stateVal = getByPath(store.state, value, '', '__NOTFOUND__')
if (stateVal === '__NOTFOUND__') {
warn(`Unknown state named [${value}].`)
stateVal = ''
}
return stateVal
}
case 'getters': {
let getterVal = getByPath(store.getters, value, '', '__NOTFOUND__')
if (getterVal === '__NOTFOUND__') {
warn(`Unknown getter named [${value}].`)
getterVal = ''
}
return getterVal
}
case 'mutations':
return store.commit(value, payload)
case 'actions':
return store.dispatch(value, payload)
}
}
})
return result
}
}
function checkMapInstance (args) {
const context = args[args.length - 1]
const isValid = context && typeof context === 'object' && context.__mpxProxy
if (!isValid) {
error('调用map**ToInstance时必须传入当前component实例this')
}
args.splice(-1)
return {
restParams: args,
context
}
}
function mapComputedToInstance (result, context) {
const options = __mpx_mode__ === 'web' ? context.$options : context.__mpxProxy.options
options.computed = options.computed || {}
Object.assign(options.computed, result)
}
export default function (store) {
const mapState = mapFactory('state', store)
const mapGetters = mapFactory('getters', store)
const mapMutations = mapFactory('mutations', store)
const mapActions = mapFactory('actions', store)
return {
mapState,
mapGetters,
mapMutations,
mapActions,
// map*ToRefs用于组合式API解构获取响应式数据
mapStateToRefs: (...args) => {
const result = {}
Object.entries(mapState(...args)).forEach(([key, value]) => {
result[key] = computed(value)
})
return result
},
mapGettersToRefs: (...args) => {
const result = {}
Object.entries(mapGetters(...args)).forEach(([key, value]) => {
result[key] = computed(value)
})
return result
},
// 以下是map*ToInstance用于异步store的,参数args:depPath, maps, context
mapStateToInstance: (...args) => {
const { context, restParams } = checkMapInstance(args)
const result = mapState(...restParams)
// 将result挂载到mpxProxy实例属性上
mapComputedToInstance(result, context)
},
mapGettersToInstance: (...args) => {
const { context, restParams } = checkMapInstance(args)
const result = mapGetters(...restParams)
mapComputedToInstance(result, context)
},
mapMutationsToInstance: (...args) => {
const { context, restParams } = checkMapInstance(args)
const result = mapMutations(...restParams)
Object.assign(context, result)
},
mapActionsToInstance: (...args) => {
const { context, restParams } = checkMapInstance(args)
const result = mapActions(...restParams)
Object.assign(context, result)
}
}
}