Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
245 lines (225 loc) · 6.45 KB
/
Copy pathindex.js
File metadata and controls
245 lines (225 loc) · 6.45 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import Mpx, { reactive, computed, effectScope } from '@mpxjs/core'
import {
getByPath,
warn,
proxy
} from '@mpxjs/utils'
// 兼容在web和小程序平台中创建表现一致的store
import mapStore from './mapStore'
function transformGetters (getters, module, store) {
const newGetters = {}
for (const key in getters) {
if (key in store.getters) {
warn(`Duplicate getter type: ${key}.`)
}
const getter = function () {
if (store.withThis) {
return getters[key].call({
state: module.state,
getters: store.getters,
rootState: store.state
})
}
return getters[key](module.state, store.getters, store.state)
}
newGetters[key] = getter
}
return newGetters
}
function transformMutations (mutations, module, store) {
const newMutations = {}
for (const key in mutations) {
if (store.mutations[key]) {
warn(`Duplicate mutation type: ${key}.`)
}
const context = {
state: module.state,
commit: store.commit.bind(store)
}
const mutation = function (...payload) {
if (store.withThis) return mutations[key].apply(context, payload)
return mutations[key](module.state, ...payload)
}
newMutations[key] = mutation
}
return newMutations
}
function transformActions (actions, module, store) {
const newActions = {}
for (const key in actions) {
if (store.actions[key]) {
warn(`Duplicate action type: ${key}.`)
}
newActions[key] = function (...payload) {
const context = {
rootState: store.state,
state: module.state,
getters: store.getters,
dispatch: store.dispatch.bind(store),
commit: store.commit.bind(store)
}
let result
if (store.withThis) {
result = actions[key].apply(context, payload)
} else {
result = actions[key](context, ...payload)
}
// action一定返回一个promise
if (result && typeof result.then === 'function' && typeof result.catch === 'function') {
return result
} else {
return Promise.resolve(result)
}
}
}
return newActions
}
function mergeDeps (module, deps) {
const mergeProps = ['state', 'getters', 'mutations', 'actions']
Object.keys(deps).forEach(key => {
const store = deps[key]
mergeProps.forEach(prop => {
if (module[prop] && (key in module[prop])) {
warn(`Deps's name [${key}] conflicts with ${prop}'s key in current options.`)
} else {
module[prop] = module[prop] || {}
if (prop === 'getters') {
// depsGetters单独存放,不需要重新进行初始化
module.depsGetters = module.depsGetters || {}
module.depsGetters[key] = store.getters
// module[prop][key] = () => store[prop]
} else {
module[prop][key] = store[prop]
}
}
})
})
}
class Store {
constructor (options) {
const {
plugins = []
} = options
this.withThis = options.withThis
this.__wrappedGetters = {}
this.__depsGetters = {}
this.getters = {}
this.mutations = {}
this.actions = {}
this._subscribers = []
this._scope = effectScope(true)
this.state = this.registerModule(options).state
this.resetStoreVM()
Object.assign(this, mapStore(this))
plugins.forEach(plugin => plugin(this))
}
dispatch (type, ...payload) {
const action = getByPath(this.actions, type)
if (!action) {
return Promise.reject(new Error(`unknown action type: ${type}`))
} else {
return action(...payload)
}
}
commit (type, ...payload) {
const mutation = getByPath(this.mutations, type)
if (!mutation) {
warn(`Unknown mutation type: ${type}.`)
} else {
mutation(...payload)
return this._subscribers.slice().forEach(sub => sub({ type, payload }, this.state))
}
}
subscribe (fn, options) {
return genericSubscribe(fn, this._subscribers, options)
}
registerModule (module) {
const state = module.state || {}
const reactiveModule = {
state
}
if (module.getters) {
reactiveModule.getters = transformGetters(module.getters, reactiveModule, this)
}
if (module.mutations) {
reactiveModule.mutations = transformMutations(module.mutations, reactiveModule, this)
}
if (module.actions) {
reactiveModule.actions = transformActions(module.actions, reactiveModule, this)
}
if (module.deps) {
mergeDeps(reactiveModule, module.deps)
}
Object.assign(this.__depsGetters, reactiveModule.depsGetters)
Object.assign(this.__wrappedGetters, reactiveModule.getters)
// merge mutations
Object.assign(this.mutations, reactiveModule.mutations)
// merge actions
Object.assign(this.actions, reactiveModule.actions)
// 子module
if (module.modules) {
const childs = module.modules
Object.keys(childs).forEach(key => {
reactiveModule.state[key] = this.registerModule(childs[key]).state
})
}
return reactiveModule
}
resetStoreVM () {
this._scope.run(() => {
if (__mpx_mode__ === 'web') {
const Vue = Mpx.__vue
const vm = new Vue({
data: {
__mpxState: this.state
},
computed: this.__wrappedGetters
})
const computedKeys = Object.keys(this.__wrappedGetters)
proxy(this.getters, vm, computedKeys)
proxy(this.getters, this.__depsGetters)
} else {
reactive(this.state)
const computedObj = {}
Object.entries(this.__wrappedGetters).forEach(([key, value]) => {
computedObj[key] = computed(value)
})
proxy(this.getters, computedObj)
proxy(this.getters, this.__depsGetters)
}
})
}
}
function genericSubscribe (fn, subs, options) {
if (subs.indexOf(fn) < 0) {
options && options.prepend
? subs.unshift(fn)
: subs.push(fn)
}
return () => {
const i = subs.indexOf(fn)
if (i > -1) {
subs.splice(i, 1)
}
}
}
export function createStore (options) {
return new Store(options)
}
// ts util functions
export function createStateWithThis (state) {
return state
}
export function createGettersWithThis (getters, options = {}) {
return getters
}
export function createMutationsWithThis (mutations, options = {}) {
return mutations
}
export function createActionsWithThis (actions, options = {}) {
return actions
}
export function createStoreWithThis (options) {
options.withThis = true
return new Store(options)
}