@mpxjs/core 2.7.52 → 2.8.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/@types/index.d.ts +342 -27
  2. package/package.json +10 -5
  3. package/src/convertor/convertor.js +2 -2
  4. package/src/convertor/mergeLifecycle.js +4 -4
  5. package/src/convertor/wxToAli.js +3 -4
  6. package/src/convertor/wxToSwan.js +2 -2
  7. package/src/convertor/wxToTt.js +1 -10
  8. package/src/convertor/wxToWeb.js +14 -7
  9. package/src/core/implement.js +2 -2
  10. package/src/core/injectMixins.js +1 -1
  11. package/src/core/innerLifecycle.js +15 -2
  12. package/src/core/mergeOptions.js +11 -5
  13. package/src/core/proxy.js +343 -229
  14. package/src/core/transferOptions.js +5 -2
  15. package/src/helper/const.js +10 -0
  16. package/src/index.js +73 -147
  17. package/src/observer/array.js +12 -17
  18. package/src/observer/computed.js +27 -56
  19. package/src/observer/dep.js +1 -1
  20. package/src/observer/effect.js +113 -0
  21. package/src/observer/effectScope.js +109 -0
  22. package/src/observer/{index.js → reactive.js} +74 -70
  23. package/src/observer/ref.js +97 -0
  24. package/src/observer/scheduler.js +171 -56
  25. package/src/observer/watch.js +163 -39
  26. package/src/platform/builtInMixins/i18nMixin.js +238 -31
  27. package/src/platform/builtInMixins/pageScrollMixin.web.js +4 -5
  28. package/src/platform/builtInMixins/pageStatusMixin.js +76 -54
  29. package/src/platform/builtInMixins/pageStatusMixin.web.js +35 -22
  30. package/src/platform/builtInMixins/proxyEventMixin.js +40 -22
  31. package/src/platform/builtInMixins/proxyEventMixin.web.js +16 -24
  32. package/src/platform/builtInMixins/refsMixin.js +82 -73
  33. package/src/platform/builtInMixins/refsMixin.web.js +0 -47
  34. package/src/platform/builtInMixins/relationsMixin.js +10 -9
  35. package/src/platform/builtInMixins/renderHelperMixin.js +1 -1
  36. package/src/platform/builtInMixins/showMixin.js +1 -1
  37. package/src/platform/createApp.js +5 -5
  38. package/src/platform/export/api.js +23 -0
  39. package/src/platform/export/api.web.js +26 -0
  40. package/src/platform/export/index.js +45 -0
  41. package/src/platform/export/index.web.js +36 -0
  42. package/src/platform/index.js +1 -5
  43. package/src/platform/patch/ali/getDefaultOptions.js +33 -31
  44. package/src/platform/patch/ali/lifecycle.js +21 -13
  45. package/src/platform/patch/builtInKeysMap.js +2 -1
  46. package/src/platform/patch/index.js +4 -9
  47. package/src/platform/patch/swan/getDefaultOptions.js +3 -3
  48. package/src/platform/patch/swan/lifecycle.js +17 -14
  49. package/src/platform/patch/web/getDefaultOptions.js +40 -16
  50. package/src/platform/patch/web/lifecycle.js +6 -3
  51. package/src/platform/patch/wx/getDefaultOptions.js +38 -31
  52. package/src/platform/patch/wx/lifecycle.js +18 -11
  53. package/src/runtime/createFactory.js +6 -2
  54. package/src/vue.web.js +3 -0
  55. package/src/vuePlugin.js +31 -0
  56. package/src/core/createStore.js +0 -241
  57. package/src/core/mapStore.js +0 -94
  58. package/src/helper/env.js +0 -20
  59. package/src/helper/getByPath.js +0 -127
  60. package/src/helper/log.js +0 -31
  61. package/src/helper/utils.js +0 -652
  62. package/src/observer/watcher.js +0 -244
@@ -1,652 +0,0 @@
1
- import Vue from '../vue'
2
-
3
- import _getByPath from './getByPath'
4
-
5
- import { error } from './log'
6
-
7
- import { set } from '../observer/index'
8
-
9
- import EXPORT_MPX from '../index'
10
-
11
- // type在支付宝环境下不一定准确,判断是普通对象优先使用isPlainObject(新版支付宝不复现,issue #644 修改isPlainObject实现与type等价)
12
- export function type (n) {
13
- return Object.prototype.toString.call(n).slice(8, -1)
14
- }
15
-
16
- /**
17
- * 判断当前环境是否是浏览器环境
18
- */
19
- export const inBrowser = typeof window !== 'undefined'
20
-
21
- export function asyncLock () {
22
- let lock = false
23
- return (fn, onerror) => {
24
- if (!lock) {
25
- lock = true
26
- Promise.resolve().then(() => {
27
- lock = false
28
- typeof fn === 'function' && fn()
29
- }).catch(e => {
30
- lock = false
31
- error('Something wrong in mpx asyncLock func execution, please check.', undefined, e)
32
- typeof onerror === 'function' && onerror()
33
- })
34
- }
35
- }
36
- }
37
-
38
- export function aliasReplace (options = {}, alias, target) {
39
- if (options[alias]) {
40
- if (Array.isArray(options[alias])) {
41
- options[target] = options[alias].concat(options[target] || [])
42
- } else if (isObject(options[alias])) {
43
- options[target] = Object.assign({}, options[alias], options[target])
44
- } else {
45
- options[target] = options[alias]
46
- }
47
- delete options[alias]
48
- }
49
- return options
50
- }
51
-
52
- export function findItem (arr = [], key) {
53
- for (const item of arr) {
54
- if ((key instanceof RegExp && key.test(item)) || item === key) {
55
- return true
56
- }
57
- }
58
- return false
59
- }
60
-
61
- export function normalizeMap (prefix, arr) {
62
- if (typeof prefix !== 'string') {
63
- arr = prefix
64
- prefix = ''
65
- }
66
- if (Array.isArray(arr)) {
67
- const map = {}
68
- arr.forEach(value => {
69
- map[value] = prefix ? `${prefix}.${value}` : value
70
- })
71
- return map
72
- }
73
- if (prefix && isObject(arr)) {
74
- arr = Object.assign({}, arr)
75
- Object.keys(arr).forEach(key => {
76
- if (typeof arr[key] === 'string') {
77
- arr[key] = `${prefix}.${arr[key]}`
78
- }
79
- })
80
- }
81
- return arr
82
- }
83
-
84
- export function isExistAttr (obj, attr) {
85
- const type = typeof obj
86
- const isNullOrUndefined = obj === null || obj === undefined
87
- if (isNullOrUndefined) {
88
- return false
89
- } else if (type === 'object' || type === 'function') {
90
- return attr in obj
91
- } else {
92
- return obj[attr] !== undefined
93
- }
94
- }
95
-
96
- export function setByPath (data, pathStrOrArr, value) {
97
- _getByPath(data, pathStrOrArr, (current, key, meta) => {
98
- if (meta.isEnd) {
99
- if (__mpx_mode__ === 'web') {
100
- Vue.set(current, key, value)
101
- } else {
102
- set(current, key, value)
103
- }
104
- } else if (!current[key]) {
105
- current[key] = {}
106
- }
107
- return current[key]
108
- })
109
- }
110
-
111
- export function getByPath (data, pathStrOrArr, defaultVal, errTip) {
112
- const results = []
113
- let normalizedArr = []
114
- if (Array.isArray(pathStrOrArr)) {
115
- normalizedArr = [pathStrOrArr]
116
- } else if (typeof pathStrOrArr === 'string') {
117
- normalizedArr = pathStrOrArr.split(',').map(str => str.trim())
118
- }
119
-
120
- normalizedArr.forEach(path => {
121
- if (!path) return
122
- const result = _getByPath(data, path, (value, key) => {
123
- let newValue
124
- if (isExistAttr(value, key)) {
125
- newValue = value[key]
126
- } else {
127
- newValue = errTip
128
- }
129
- return newValue
130
- })
131
- // 小程序setData时不允许undefined数据
132
- results.push(result === undefined ? defaultVal : result)
133
- })
134
- return results.length > 1 ? results : results[0]
135
- }
136
-
137
- export function defineGetterSetter (target, key, getValue, setValue, context) {
138
- let get
139
- let set
140
- if (typeof getValue === 'function') {
141
- get = context ? getValue.bind(context) : getValue
142
- } else {
143
- get = function () {
144
- return getValue
145
- }
146
- }
147
- if (typeof setValue === 'function') {
148
- set = context ? setValue.bind(context) : setValue
149
- }
150
- let descriptor = {
151
- get,
152
- configurable: true,
153
- enumerable: true
154
- }
155
- if (set) descriptor.set = set
156
- Object.defineProperty(target, key, descriptor)
157
- }
158
-
159
- export function proxy (target, source, keys, readonly, onConflict) {
160
- keys = keys || Object.keys(source)
161
- keys.forEach((key) => {
162
- const descriptor = {
163
- get () {
164
- return source[key]
165
- },
166
- configurable: true,
167
- enumerable: true
168
- }
169
- !readonly && (descriptor.set = function (val) {
170
- source[key] = val
171
- })
172
- if (onConflict) {
173
- if (key in target) {
174
- if (onConflict(key) === false) return
175
- }
176
- }
177
- Object.defineProperty(target, key, descriptor)
178
- })
179
- return target
180
- }
181
-
182
- // 包含原型链上属性keys
183
- export function enumerableKeys (obj) {
184
- const keys = []
185
- for (let key in obj) {
186
- keys.push(key)
187
- }
188
- return keys
189
- }
190
-
191
- // 此函数用于合并mpx插件挂载到mpx.prototype中的实例属性,因此需要进行原型链属性的合并
192
- export function extend (target, ...sources) {
193
- for (const source of sources) {
194
- if (isObject(source)) {
195
- // 合并原型链属性
196
- for (const key in source) {
197
- target[key] = source[key]
198
- }
199
- }
200
- }
201
- return target
202
- }
203
-
204
- // deepMerge 用于合并i18n语言集
205
- export function merge (target, ...sources) {
206
- if (isObject(target)) {
207
- for (const source of sources) {
208
- if (isObject(source)) {
209
- Object.keys(source).forEach((key) => {
210
- if (isObject(source[key]) && isObject(target[key])) {
211
- merge(target[key], source[key])
212
- } else {
213
- target[key] = source[key]
214
- }
215
- })
216
- }
217
- }
218
- }
219
- return target
220
- }
221
-
222
- export function isObject (obj) {
223
- return obj !== null && typeof obj === 'object'
224
- }
225
-
226
- export function isPlainObject (value) {
227
- if (value === null || typeof value !== 'object' || type(value) !== 'Object') return false
228
- const proto = Object.getPrototypeOf(value)
229
- if (proto === null) return true
230
- // 处理支付宝接口返回数据对象的__proto__与js中创建对象的__proto__不一致的问题,判断value.__proto__.__proto__ === null时也认为是plainObject
231
- const innerProto = Object.getPrototypeOf(proto)
232
- if (proto === Object.prototype || innerProto === null) return true
233
- // issue #644
234
- if (EXPORT_MPX.config.observeClassInstance) {
235
- if (Array.isArray(EXPORT_MPX.config.observeClassInstance)) {
236
- for (let i = 0; i < EXPORT_MPX.config.observeClassInstance.length; i++) {
237
- if (proto === EXPORT_MPX.config.observeClassInstance[i].prototype) return true
238
- }
239
- } else {
240
- return true
241
- }
242
- }
243
- return false
244
- }
245
-
246
- const hasOwnProperty = Object.prototype.hasOwnProperty
247
-
248
- export function hasOwn (obj, key) {
249
- return hasOwnProperty.call(obj, key)
250
- }
251
-
252
- export const hasProto = '__proto__' in {}
253
-
254
- // 微信小程序插件环境2.8.3以下基础库protoAugment会失败,对环境进行测试按需降级为copyAugment
255
- function testArrayProtoAugment () {
256
- const arr = []
257
- /* eslint-disable no-proto, camelcase */
258
- arr.__proto__ = { __array_proto_test__: '__array_proto_test__' }
259
- return arr.__array_proto_test__ === '__array_proto_test__'
260
- }
261
-
262
- export const arrayProtoAugment = testArrayProtoAugment()
263
-
264
- export function isValidArrayIndex (val) {
265
- const n = parseFloat(String(val))
266
- return n >= 0 && Math.floor(n) === n && isFinite(val)
267
- }
268
-
269
- export function remove (arr, item) {
270
- if (arr.length) {
271
- const index = arr.indexOf(item)
272
- if (index > -1) {
273
- return arr.splice(index, 1)
274
- }
275
- }
276
- }
277
-
278
- export function def (obj, key, val, enumerable) {
279
- Object.defineProperty(obj, key, {
280
- value: val,
281
- enumerable: !!enumerable,
282
- writable: true,
283
- configurable: true
284
- })
285
- }
286
-
287
- export function likeArray (arr) {
288
- return Array.isArray(arr)
289
- }
290
-
291
- export function isDef (v) {
292
- return v !== undefined && v !== null
293
- }
294
-
295
- export function stringifyClass (value) {
296
- if (Array.isArray(value)) {
297
- return stringifyArray(value)
298
- }
299
- if (isObject(value)) {
300
- return stringifyObject(value)
301
- }
302
- if (typeof value === 'string') {
303
- return value
304
- }
305
- return ''
306
- }
307
-
308
- function stringifyArray (value) {
309
- let res = ''
310
- let stringified
311
- for (let i = 0, l = value.length; i < l; i++) {
312
- if (isDef(stringified = stringifyClass(value[i])) && stringified !== '') {
313
- if (res) res += ' '
314
- res += stringified
315
- }
316
- }
317
- return res
318
- }
319
-
320
- function stringifyObject (value) {
321
- let res = ''
322
- for (const key in value) {
323
- if (value[key]) {
324
- if (res) res += ' '
325
- res += key
326
- }
327
- }
328
- return res
329
- }
330
-
331
- export function concat (a, b) {
332
- return a ? b ? (a + ' ' + b) : a : (b || '')
333
- }
334
-
335
- export function hump2dash (value) {
336
- return value.replace(/[A-Z]/g, function (match) {
337
- return '-' + match.toLowerCase()
338
- })
339
- }
340
-
341
- export function dash2hump (value) {
342
- return value.replace(/-([a-z])/g, function (match, p1) {
343
- return p1.toUpperCase()
344
- })
345
- }
346
-
347
- export function parseStyleText (cssText) {
348
- const res = {}
349
- const listDelimiter = /;(?![^(]*\))/g
350
- const propertyDelimiter = /:(.+)/
351
- cssText.split(listDelimiter).forEach(function (item) {
352
- if (item) {
353
- let tmp = item.split(propertyDelimiter)
354
- tmp.length > 1 && (res[dash2hump(tmp[0].trim())] = tmp[1].trim())
355
- }
356
- })
357
- return res
358
- }
359
-
360
- export function genStyleText (styleObj) {
361
- let res = ''
362
- for (let key in styleObj) {
363
- if (hasOwn(styleObj, key)) {
364
- let item = styleObj[key]
365
- res += `${hump2dash(key)}:${item};`
366
- }
367
- }
368
- return res
369
- }
370
-
371
- export function mergeObjectArray (arr) {
372
- const res = {}
373
- for (let i = 0; i < arr.length; i++) {
374
- if (arr[i]) {
375
- Object.assign(res, arr[i])
376
- }
377
- }
378
- return res
379
- }
380
-
381
- export function normalizeDynamicStyle (value) {
382
- if (Array.isArray(value)) {
383
- return mergeObjectArray(value)
384
- }
385
- if (typeof value === 'string') {
386
- return parseStyleText(value)
387
- }
388
- return value
389
- }
390
-
391
- export function isEmptyObject (obj) {
392
- if (!obj) {
393
- return true
394
- }
395
- for (let key in obj) {
396
- return false
397
- }
398
- return true
399
- }
400
-
401
- export function aIsSubPathOfB (a, b) {
402
- if (a.startsWith(b) && a !== b) {
403
- let nextChar = a[b.length]
404
- if (nextChar === '.') {
405
- return a.slice(b.length + 1)
406
- } else if (nextChar === '[') {
407
- return a.slice(b.length)
408
- }
409
- }
410
- }
411
-
412
- export function getFirstKey (path) {
413
- return /^[^[.]*/.exec(path)[0]
414
- }
415
-
416
- function doMergeData (target, source) {
417
- Object.keys(source).forEach((srcKey) => {
418
- if (hasOwn(target, srcKey)) {
419
- target[srcKey] = source[srcKey]
420
- } else {
421
- let processed = false
422
- const tarKeys = Object.keys(target)
423
- for (let i = 0; i < tarKeys.length; i++) {
424
- const tarKey = tarKeys[i]
425
- if (aIsSubPathOfB(tarKey, srcKey)) {
426
- delete target[tarKey]
427
- target[srcKey] = source[srcKey]
428
- processed = true
429
- continue
430
- }
431
- const subPath = aIsSubPathOfB(srcKey, tarKey)
432
- if (subPath) {
433
- setByPath(target[tarKey], subPath, source[srcKey])
434
- processed = true
435
- break
436
- }
437
- }
438
- if (!processed) {
439
- target[srcKey] = source[srcKey]
440
- }
441
- }
442
- })
443
- return target
444
- }
445
-
446
- export function mergeData (target, ...sources) {
447
- if (target) {
448
- sources.forEach((source) => {
449
- if (source) doMergeData(target, source)
450
- })
451
- }
452
- return target
453
- }
454
-
455
- export function processUndefined (obj) {
456
- let result = {}
457
- for (let key in obj) {
458
- if (hasOwn(obj, key)) {
459
- if (obj[key] !== undefined) {
460
- result[key] = obj[key]
461
- } else {
462
- result[key] = ''
463
- }
464
- }
465
- }
466
- return result
467
- }
468
-
469
- export function noop () {
470
-
471
- }
472
-
473
- export function diffAndCloneA (a, b) {
474
- let diffData = null
475
- let curPath = ''
476
- let diff = false
477
-
478
- function deepDiffAndCloneA (a, b, currentDiff) {
479
- const setDiff = (val) => {
480
- if (val) {
481
- currentDiff = val
482
- if (curPath) {
483
- diffData = diffData || {}
484
- diffData[curPath] = clone
485
- }
486
- }
487
- }
488
- let clone = a
489
- if (typeof a !== 'object' || a === null) {
490
- if (!currentDiff) setDiff(a !== b)
491
- } else {
492
- const toString = Object.prototype.toString
493
- const className = toString.call(a)
494
- const sameClass = className === toString.call(b)
495
- let length
496
- let lastPath
497
- if (isPlainObject(a)) {
498
- const keys = Object.keys(a)
499
- length = keys.length
500
- clone = {}
501
- if (!currentDiff) setDiff(!sameClass || length < Object.keys(b).length || !Object.keys(b).every((key) => hasOwn(a, key)))
502
- lastPath = curPath
503
- for (let i = 0; i < length; i++) {
504
- const key = keys[i]
505
- curPath += `.${key}`
506
- clone[key] = deepDiffAndCloneA(a[key], sameClass ? b[key] : undefined, currentDiff)
507
- curPath = lastPath
508
- }
509
- // 继承原始对象的freeze/seal/preventExtensions操作
510
- if (Object.isFrozen(a)) {
511
- Object.freeze(clone)
512
- } else if (Object.isSealed(a)) {
513
- Object.seal(clone)
514
- } else if (!Object.isExtensible(a)) {
515
- Object.preventExtensions(clone)
516
- }
517
- } else if (Array.isArray(a)) {
518
- length = a.length
519
- clone = []
520
- if (!currentDiff) setDiff(!sameClass || length < b.length)
521
- lastPath = curPath
522
- for (let i = 0; i < length; i++) {
523
- curPath += `[${i}]`
524
- clone[i] = deepDiffAndCloneA(a[i], sameClass ? b[i] : undefined, currentDiff)
525
- curPath = lastPath
526
- }
527
- // 继承原始数组的freeze/seal/preventExtensions操作
528
- if (Object.isFrozen(a)) {
529
- Object.freeze(clone)
530
- } else if (Object.isSealed(a)) {
531
- Object.seal(clone)
532
- } else if (!Object.isExtensible(a)) {
533
- Object.preventExtensions(clone)
534
- }
535
- } else if (a instanceof RegExp) {
536
- if (!currentDiff) setDiff(!sameClass || '' + a !== '' + b)
537
- } else if (a instanceof Date) {
538
- if (!currentDiff) setDiff(!sameClass || +a !== +b)
539
- } else {
540
- if (!currentDiff) setDiff(!sameClass || a !== b)
541
- }
542
- }
543
- if (currentDiff) {
544
- diff = currentDiff
545
- }
546
- return clone
547
- }
548
-
549
- return {
550
- clone: deepDiffAndCloneA(a, b, diff),
551
- diff,
552
- diffData
553
- }
554
- }
555
-
556
- export function isValidIdentifierStr (str) {
557
- return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(str)
558
- }
559
-
560
- export function isNumberStr (str) {
561
- return /^\d+$/.test(str)
562
- }
563
-
564
- let datasetReg = /^data-(.+)$/
565
-
566
- export function collectDataset (props) {
567
- let dataset = {}
568
- for (let key in props) {
569
- if (hasOwn(props, key)) {
570
- let matched = datasetReg.exec(key)
571
- if (matched) {
572
- dataset[matched[1]] = props[key]
573
- }
574
- }
575
- }
576
- return dataset
577
- }
578
-
579
- /**
580
- * process renderData, remove sub node if visit parent node already
581
- * @param {Object} renderData
582
- * @return {Object} processedRenderData
583
- */
584
- export function preProcessRenderData (renderData) {
585
- // method for get key path array
586
- const processKeyPathMap = (keyPathMap) => {
587
- let keyPath = Object.keys(keyPathMap)
588
- return keyPath.filter((keyA) => {
589
- return keyPath.every((keyB) => {
590
- if (keyA.startsWith(keyB) && keyA !== keyB) {
591
- let nextChar = keyA[keyB.length]
592
- if (nextChar === '.' || nextChar === '[') {
593
- return false
594
- }
595
- }
596
- return true
597
- })
598
- })
599
- }
600
-
601
- const processedRenderData = {}
602
- const renderDataFinalKey = processKeyPathMap(renderData)
603
- Object.keys(renderData).forEach(item => {
604
- if (renderDataFinalKey.indexOf(item) > -1) {
605
- processedRenderData[item] = renderData[item]
606
- }
607
- })
608
- return processedRenderData
609
- }
610
-
611
- export function makeMap (arr) {
612
- return arr.reduce((obj, item) => {
613
- obj[item] = true
614
- return obj
615
- }, {})
616
- }
617
-
618
- /**
619
- * Get object values by chaining-key
620
- * @param {Object} obj target Object
621
- * @param {String} key chaining-key, e.g.: 'a.b.c'
622
- */
623
- export function getChainKeyOfObj (obj = {}, key = '') {
624
- return key.split('.').reduce((o, k) => o && o[k], obj)
625
- }
626
-
627
- /**
628
- * Delete object values by chaining-key
629
- * @param {Object} obj target object
630
- * @param {String} key chaining-key
631
- */
632
- export function delChainKeyOfObj (obj = {}, key = '') {
633
- return key.split('.').reduce((o, k, index, arr) => {
634
- if (arr.length === index + 1) {
635
- try {
636
- return delete o[k]
637
- } catch (e) { // undefined
638
- return false
639
- }
640
- }
641
- return o && o[k]
642
- }, obj)
643
- }
644
-
645
- export function spreadProp (obj, key) {
646
- if (hasOwn(obj, key)) {
647
- const temp = obj[key]
648
- delete obj[key]
649
- Object.assign(obj, temp)
650
- }
651
- return obj
652
- }