|
@@ -0,0 +1,449 @@
|
|
1
|
+import diff from './diff'
|
|
2
|
+
|
|
3
|
+let originData = null
|
|
4
|
+let globalStore = null
|
|
5
|
+let fnMapping = {}
|
|
6
|
+
|
|
7
|
+const ARRAYTYPE = '[object Array]'
|
|
8
|
+const OBJECTTYPE = '[object Object]'
|
|
9
|
+const FUNCTIONTYPE = '[object Function]'
|
|
10
|
+
|
|
11
|
+export default function create(store, option) {
|
|
12
|
+ let updatePath = null
|
|
13
|
+ if (arguments.length === 2) {
|
|
14
|
+ if (!originData) {
|
|
15
|
+ originData = JSON.parse(JSON.stringify(store.data))
|
|
16
|
+ globalStore = store
|
|
17
|
+ store.instances = {}
|
|
18
|
+ store.update = update
|
|
19
|
+ store.push = push
|
|
20
|
+ store.pull = pull
|
|
21
|
+ store.add = add
|
|
22
|
+ store.remove = remove
|
|
23
|
+ store.originData = originData
|
|
24
|
+ store.env && initCloud(store.env)
|
|
25
|
+ extendStoreMethod(store)
|
|
26
|
+ }
|
|
27
|
+ getApp().globalData && (getApp().globalData.store = store)
|
|
28
|
+ //option.data = store.data
|
|
29
|
+ const onLoad = option.onLoad
|
|
30
|
+ walk(store.data)
|
|
31
|
+ // 解决函数属性初始化不能显示的问题,要求必须在data中声明使用
|
|
32
|
+ // 这段代码是同步store.data到option.data,只有经过walk方法后store.data中的函数才能变成属性,才能被小程序page方法渲染
|
|
33
|
+ if (option.data && Object.keys(option.data).length > 0) {
|
|
34
|
+ updatePath = getUpdatePath(option.data)
|
|
35
|
+ syncValues(store.data, option.data)
|
|
36
|
+ }
|
|
37
|
+ option.onLoad = function (e) {
|
|
38
|
+ this.store = store
|
|
39
|
+ this._updatePath = updatePath
|
|
40
|
+ rewriteUpdate(this)
|
|
41
|
+ store.instances[this.route] = []
|
|
42
|
+ store.instances[this.route].push(this)
|
|
43
|
+ onLoad && onLoad.call(this, e)
|
|
44
|
+ syncValues(store.data, this.data)
|
|
45
|
+ this.setData(this.data)
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ // 解决执行navigateBack或reLaunch时清除store.instances对应页面的实例
|
|
49
|
+ const onUnload = option.onUnload
|
|
50
|
+ option.onUnload = function () {
|
|
51
|
+ onUnload && onUnload.call(this)
|
|
52
|
+ store.instances[this.route] = []
|
|
53
|
+ }
|
|
54
|
+
|
|
55
|
+ Page(option)
|
|
56
|
+ } else {
|
|
57
|
+ const ready = store.ready
|
|
58
|
+ const pure = store.pure
|
|
59
|
+ const componentUpdatePath = getUpdatePath(store.data)
|
|
60
|
+ store.ready = function () {
|
|
61
|
+ if (pure) {
|
|
62
|
+ this.store = { data: store.data || {} }
|
|
63
|
+ this.store.originData = store.data ? JSON.parse(JSON.stringify(store.data)) : {}
|
|
64
|
+ walk(store.data || {})
|
|
65
|
+ rewritePureUpdate(this)
|
|
66
|
+ } else {
|
|
67
|
+ this.page = getCurrentPages()[getCurrentPages().length - 1]
|
|
68
|
+ this.store = this.page.store
|
|
69
|
+ this._updatePath = componentUpdatePath
|
|
70
|
+ syncValues(this.store.data, store.data)
|
|
71
|
+ walk(store.data || {})
|
|
72
|
+ this.setData.call(this, this.store.data)
|
|
73
|
+ rewriteUpdate(this)
|
|
74
|
+ this.store.instances[this.page.route].push(this)
|
|
75
|
+ }
|
|
76
|
+ ready && ready.call(this)
|
|
77
|
+ }
|
|
78
|
+ Component(store)
|
|
79
|
+ }
|
|
80
|
+}
|
|
81
|
+
|
|
82
|
+function syncValues(from, to){
|
|
83
|
+ Object.keys(to).forEach(key=>{
|
|
84
|
+ if(from.hasOwnProperty(key)){
|
|
85
|
+ to[key] = from[key]
|
|
86
|
+ }
|
|
87
|
+ })
|
|
88
|
+}
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+function getUpdatePath(data) {
|
|
92
|
+ const result = {}
|
|
93
|
+ dataToPath(data, result)
|
|
94
|
+ return result
|
|
95
|
+}
|
|
96
|
+
|
|
97
|
+function dataToPath(data, result) {
|
|
98
|
+ Object.keys(data).forEach(key => {
|
|
99
|
+ result[key] = true
|
|
100
|
+ const type = Object.prototype.toString.call(data[key])
|
|
101
|
+ if (type === OBJECTTYPE) {
|
|
102
|
+ _objToPath(data[key], key, result)
|
|
103
|
+ } else if (type === ARRAYTYPE) {
|
|
104
|
+ _arrayToPath(data[key], key, result)
|
|
105
|
+ }
|
|
106
|
+ })
|
|
107
|
+}
|
|
108
|
+
|
|
109
|
+function _objToPath(data, path, result) {
|
|
110
|
+ Object.keys(data).forEach(key => {
|
|
111
|
+ result[path + '.' + key] = true
|
|
112
|
+ delete result[path]
|
|
113
|
+ const type = Object.prototype.toString.call(data[key])
|
|
114
|
+ if (type === OBJECTTYPE) {
|
|
115
|
+ _objToPath(data[key], path + '.' + key, result)
|
|
116
|
+ } else if (type === ARRAYTYPE) {
|
|
117
|
+ _arrayToPath(data[key], path + '.' + key, result)
|
|
118
|
+ }
|
|
119
|
+ })
|
|
120
|
+}
|
|
121
|
+
|
|
122
|
+function _arrayToPath(data, path, result) {
|
|
123
|
+ data.forEach((item, index) => {
|
|
124
|
+ result[path + '[' + index + ']'] = true
|
|
125
|
+ delete result[path]
|
|
126
|
+ const type = Object.prototype.toString.call(item)
|
|
127
|
+ if (type === OBJECTTYPE) {
|
|
128
|
+ _objToPath(item, path + '[' + index + ']', result)
|
|
129
|
+ } else if (type === ARRAYTYPE) {
|
|
130
|
+ _arrayToPath(item, path + '[' + index + ']', result)
|
|
131
|
+ }
|
|
132
|
+ })
|
|
133
|
+}
|
|
134
|
+
|
|
135
|
+function rewritePureUpdate(ctx) {
|
|
136
|
+ ctx.update = function (patch) {
|
|
137
|
+ const store = this.store
|
|
138
|
+ const that = this
|
|
139
|
+ return new Promise(resolve => {
|
|
140
|
+ //defineFnProp(store.data)
|
|
141
|
+ if (patch) {
|
|
142
|
+ for (let key in patch) {
|
|
143
|
+ updateByPath(store.data, key, patch[key])
|
|
144
|
+ }
|
|
145
|
+ }
|
|
146
|
+ let diffResult = diff(store.data, store.originData)
|
|
147
|
+ let array = []
|
|
148
|
+ if (Object.keys(diffResult)[0] == '') {
|
|
149
|
+ diffResult = diffResult['']
|
|
150
|
+ }
|
|
151
|
+ if (Object.keys(diffResult).length > 0) {
|
|
152
|
+ array.push( new Promise( cb => that.setData(diffResult, cb) ) )
|
|
153
|
+ store.onChange && store.onChange(diffResult)
|
|
154
|
+ for (let key in diffResult) {
|
|
155
|
+ updateByPath(store.originData, key, typeof diffResult[key] === 'object' ? JSON.parse(JSON.stringify(diffResult[key])) : diffResult[key])
|
|
156
|
+ }
|
|
157
|
+ }
|
|
158
|
+ Promise.all(array).then( e => resolve(diffResult) )
|
|
159
|
+ })
|
|
160
|
+ }
|
|
161
|
+}
|
|
162
|
+
|
|
163
|
+function initCloud(env) {
|
|
164
|
+ wx.cloud.init()
|
|
165
|
+ globalStore.db = wx.cloud.database({
|
|
166
|
+ env: env
|
|
167
|
+ })
|
|
168
|
+}
|
|
169
|
+
|
|
170
|
+function push(patch) {
|
|
171
|
+ return new Promise(function (resolve, reject) {
|
|
172
|
+ _push(update(patch), resolve, reject)
|
|
173
|
+ })
|
|
174
|
+}
|
|
175
|
+
|
|
176
|
+function _push(diffResult, resolve) {
|
|
177
|
+ const objs = diffToPushObj(diffResult)
|
|
178
|
+ Object.keys(objs).forEach((path) => {
|
|
179
|
+ const arr = path.split('-')
|
|
180
|
+ const id = globalStore.data[arr[0]][parseInt(arr[1])]._id
|
|
181
|
+ const obj = objs[path]
|
|
182
|
+ if (globalStore.methods && globalStore.methods[arr[0]]) {
|
|
183
|
+ Object.keys(globalStore.methods[arr[0]]).forEach(key => {
|
|
184
|
+ if (obj.hasOwnProperty(key)) {
|
|
185
|
+ delete obj[key]
|
|
186
|
+ }
|
|
187
|
+ })
|
|
188
|
+ }
|
|
189
|
+ globalStore.db.collection(arr[0]).doc(id).update({
|
|
190
|
+ data: obj
|
|
191
|
+ }).then((res) => {
|
|
192
|
+ resolve(res)
|
|
193
|
+ })
|
|
194
|
+ })
|
|
195
|
+}
|
|
196
|
+
|
|
197
|
+function update(patch) {
|
|
198
|
+ return new Promise(resolve => {
|
|
199
|
+ //defineFnProp(globalStore.data)
|
|
200
|
+ if (patch) {
|
|
201
|
+ for (let key in patch) {
|
|
202
|
+ updateByPath(globalStore.data, key, patch[key])
|
|
203
|
+ }
|
|
204
|
+ }
|
|
205
|
+ let diffResult = diff(globalStore.data, originData)
|
|
206
|
+ if (Object.keys(diffResult)[0] == '') {
|
|
207
|
+ diffResult = diffResult['']
|
|
208
|
+ }
|
|
209
|
+ const updateAll = matchGlobalData(diffResult)
|
|
210
|
+ let array = []
|
|
211
|
+ if (Object.keys(diffResult).length > 0) {
|
|
212
|
+ for (let key in globalStore.instances) {
|
|
213
|
+ globalStore.instances[key].forEach(ins => {
|
|
214
|
+ if(updateAll || globalStore.updateAll || ins._updatePath){
|
|
215
|
+ // 获取需要更新的字段
|
|
216
|
+ const needUpdatePathList = getNeedUpdatePathList(diffResult, ins._updatePath)
|
|
217
|
+ if (needUpdatePathList.length) {
|
|
218
|
+ const _diffResult = {}
|
|
219
|
+ for (let _path in diffResult) {
|
|
220
|
+ if (needUpdatePathList.includes(_path)) {
|
|
221
|
+ _diffResult[_path] = diffResult[_path]
|
|
222
|
+ }
|
|
223
|
+ }
|
|
224
|
+ array.push( new Promise(cb => {
|
|
225
|
+ ins.setData.call(ins, _diffResult, cb)
|
|
226
|
+ }) )
|
|
227
|
+ }
|
|
228
|
+ }
|
|
229
|
+ })
|
|
230
|
+ }
|
|
231
|
+ globalStore.onChange && globalStore.onChange(diffResult)
|
|
232
|
+ for (let key in diffResult) {
|
|
233
|
+ updateByPath(originData, key, typeof diffResult[key] === 'object' ? JSON.parse(JSON.stringify(diffResult[key])) : diffResult[key])
|
|
234
|
+ }
|
|
235
|
+ }
|
|
236
|
+ Promise.all(array).then(e=>{
|
|
237
|
+ resolve(diffResult)
|
|
238
|
+ })
|
|
239
|
+ })
|
|
240
|
+}
|
|
241
|
+
|
|
242
|
+function matchGlobalData(diffResult) {
|
|
243
|
+ if(!globalStore.globalData) return false
|
|
244
|
+ for (let keyA in diffResult) {
|
|
245
|
+ if (globalStore.globalData.indexOf(keyA) > -1) {
|
|
246
|
+ return true
|
|
247
|
+ }
|
|
248
|
+ for (let i = 0, len = globalStore.globalData.length; i < len; i++) {
|
|
249
|
+ if (includePath(keyA, globalStore.globalData[i])) {
|
|
250
|
+ return true
|
|
251
|
+ }
|
|
252
|
+ }
|
|
253
|
+ }
|
|
254
|
+ return false
|
|
255
|
+}
|
|
256
|
+
|
|
257
|
+function getNeedUpdatePathList(diffResult, updatePath){
|
|
258
|
+ const paths = []
|
|
259
|
+ for(let keyA in diffResult){
|
|
260
|
+ if(updatePath[keyA]){
|
|
261
|
+ paths.push(keyA)
|
|
262
|
+ }
|
|
263
|
+ for(let keyB in updatePath){
|
|
264
|
+ if(includePath(keyA, keyB)){
|
|
265
|
+ paths.push(keyA)
|
|
266
|
+ }
|
|
267
|
+ }
|
|
268
|
+ }
|
|
269
|
+ return paths
|
|
270
|
+}
|
|
271
|
+
|
|
272
|
+function includePath(pathA, pathB){
|
|
273
|
+ if(pathA.indexOf(pathB)===0){
|
|
274
|
+ const next = pathA.substr(pathB.length, 1)
|
|
275
|
+ if(next === '['||next === '.'){
|
|
276
|
+ return true
|
|
277
|
+ }
|
|
278
|
+ }
|
|
279
|
+ return false
|
|
280
|
+}
|
|
281
|
+
|
|
282
|
+function rewriteUpdate(ctx) {
|
|
283
|
+ ctx.update = update
|
|
284
|
+}
|
|
285
|
+
|
|
286
|
+function updateByPath(origin, path, value) {
|
|
287
|
+ const arr = path.replace(/]/g, '').replace(/\[/g, '.').split('.')
|
|
288
|
+ let current = origin
|
|
289
|
+ for (let i = 0, len = arr.length; i < len; i++) {
|
|
290
|
+ if (i === len - 1) {
|
|
291
|
+ current[arr[i]] = value
|
|
292
|
+ } else {
|
|
293
|
+ current = current[arr[i]]
|
|
294
|
+ }
|
|
295
|
+ }
|
|
296
|
+}
|
|
297
|
+
|
|
298
|
+function pull(cn, where) {
|
|
299
|
+ return new Promise(function (resolve) {
|
|
300
|
+ globalStore.db.collection(cn).where(where || {}).get().then(res => {
|
|
301
|
+ extend(res, cn)
|
|
302
|
+ resolve(res)
|
|
303
|
+ })
|
|
304
|
+ })
|
|
305
|
+}
|
|
306
|
+
|
|
307
|
+function extend(res, cn) {
|
|
308
|
+ res.data.forEach(item => {
|
|
309
|
+ const mds = globalStore.methods[cn]
|
|
310
|
+ mds && Object.keys(mds).forEach(key => {
|
|
311
|
+ Object.defineProperty(item, key, {
|
|
312
|
+ enumerable: true,
|
|
313
|
+ get: () => {
|
|
314
|
+ return mds[key].call(item)
|
|
315
|
+ },
|
|
316
|
+ set: () => {
|
|
317
|
+ //方法不能改写
|
|
318
|
+ }
|
|
319
|
+ })
|
|
320
|
+ })
|
|
321
|
+ })
|
|
322
|
+}
|
|
323
|
+
|
|
324
|
+function add(cn, data) {
|
|
325
|
+ return globalStore.db.collection(cn).add({ data })
|
|
326
|
+}
|
|
327
|
+
|
|
328
|
+function remove(cn, id) {
|
|
329
|
+ return globalStore.db.collection(cn).doc(id).remove()
|
|
330
|
+}
|
|
331
|
+
|
|
332
|
+function diffToPushObj(diffResult) {
|
|
333
|
+ const result = {}
|
|
334
|
+ Object.keys(diffResult).forEach(key => {
|
|
335
|
+ diffItemToObj(key, diffResult[key], result)
|
|
336
|
+ })
|
|
337
|
+ return result
|
|
338
|
+}
|
|
339
|
+
|
|
340
|
+function diffItemToObj(path, value, result) {
|
|
341
|
+ const arr = path.replace(/]/g, '').replace(/\[/g, '.').split('.')
|
|
342
|
+ const obj = {}
|
|
343
|
+ let current = null
|
|
344
|
+ const len = arr.length
|
|
345
|
+ for (let i = 2; i < len; i++) {
|
|
346
|
+ if (len === 3) {
|
|
347
|
+ obj[arr[i]] = value
|
|
348
|
+ } else {
|
|
349
|
+ if (i === len - 1) {
|
|
350
|
+ current[arr[i]] = value
|
|
351
|
+ } else {
|
|
352
|
+ const pre = current
|
|
353
|
+ current = {}
|
|
354
|
+ if (i === 2) {
|
|
355
|
+ obj[arr[i]] = current
|
|
356
|
+ } else {
|
|
357
|
+ pre[arr[i]] = current
|
|
358
|
+ }
|
|
359
|
+ }
|
|
360
|
+ }
|
|
361
|
+ }
|
|
362
|
+ const key = arr[0] + '-' + arr[1]
|
|
363
|
+ result[key] = Object.assign(result[key] || {}, obj)
|
|
364
|
+}
|
|
365
|
+
|
|
366
|
+function extendStoreMethod() {
|
|
367
|
+ globalStore.method = function (path, fn) {
|
|
368
|
+ fnMapping[path] = fn
|
|
369
|
+ let ok = getObjByPath(path)
|
|
370
|
+ Object.defineProperty(ok.obj, ok.key, {
|
|
371
|
+ enumerable: true,
|
|
372
|
+ get: () => {
|
|
373
|
+ return fnMapping[path].call(globalStore.data)
|
|
374
|
+ },
|
|
375
|
+ set: () => {
|
|
376
|
+ console.warn('Please using store.method to set method prop of data!')
|
|
377
|
+ }
|
|
378
|
+ })
|
|
379
|
+ }
|
|
380
|
+}
|
|
381
|
+
|
|
382
|
+function getObjByPath(path) {
|
|
383
|
+ const arr = path.replace(/]/g, '').replace(/\[/g, '.').split('.')
|
|
384
|
+ const len = arr.length
|
|
385
|
+ if (len > 1) {
|
|
386
|
+ let current = globalStore.data[arr[0]]
|
|
387
|
+ for (let i = 1; i < len - 1; i++) {
|
|
388
|
+ current = current[arr[i]]
|
|
389
|
+ }
|
|
390
|
+ return { obj: current, key: arr[len - 1] }
|
|
391
|
+ } else {
|
|
392
|
+ return { obj: globalStore.data, key: arr[0] }
|
|
393
|
+ }
|
|
394
|
+}
|
|
395
|
+
|
|
396
|
+function walk(data) {
|
|
397
|
+ Object.keys(data).forEach(key => {
|
|
398
|
+ const obj = data[key]
|
|
399
|
+ const tp = type(obj)
|
|
400
|
+ if (tp == FUNCTIONTYPE) {
|
|
401
|
+ setProp(key, obj)
|
|
402
|
+ } else if (tp == OBJECTTYPE) {
|
|
403
|
+ Object.keys(obj).forEach(subKey => {
|
|
404
|
+ _walk(obj[subKey], key + '.' + subKey)
|
|
405
|
+ })
|
|
406
|
+
|
|
407
|
+ } else if (tp == ARRAYTYPE) {
|
|
408
|
+ obj.forEach((item, index) => {
|
|
409
|
+ _walk(item, key + '[' + index + ']')
|
|
410
|
+ })
|
|
411
|
+
|
|
412
|
+ }
|
|
413
|
+ })
|
|
414
|
+}
|
|
415
|
+
|
|
416
|
+function _walk(obj, path) {
|
|
417
|
+ const tp = type(obj)
|
|
418
|
+ if (tp == FUNCTIONTYPE) {
|
|
419
|
+ setProp(path, obj)
|
|
420
|
+ } else if (tp == OBJECTTYPE) {
|
|
421
|
+ Object.keys(obj).forEach(subKey => {
|
|
422
|
+ _walk(obj[subKey], path + '.' + subKey)
|
|
423
|
+ })
|
|
424
|
+
|
|
425
|
+ } else if (tp == ARRAYTYPE) {
|
|
426
|
+ obj.forEach((item, index) => {
|
|
427
|
+ _walk(item, path + '[' + index + ']')
|
|
428
|
+ })
|
|
429
|
+
|
|
430
|
+ }
|
|
431
|
+}
|
|
432
|
+
|
|
433
|
+function setProp(path, fn) {
|
|
434
|
+ const ok = getObjByPath(path)
|
|
435
|
+ fnMapping[path] = fn
|
|
436
|
+ Object.defineProperty(ok.obj, ok.key, {
|
|
437
|
+ enumerable: true,
|
|
438
|
+ get: () => {
|
|
439
|
+ return fnMapping[path].call(globalStore.data)
|
|
440
|
+ },
|
|
441
|
+ set: () => {
|
|
442
|
+ console.warn('Please using store.method to set method prop of data!')
|
|
443
|
+ }
|
|
444
|
+ })
|
|
445
|
+}
|
|
446
|
+
|
|
447
|
+function type(obj) {
|
|
448
|
+ return Object.prototype.toString.call(obj)
|
|
449
|
+}
|