@mpxjs/core 2.8.0-beta.5 → 2.8.1

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.
package/@types/index.d.ts CHANGED
@@ -107,7 +107,6 @@ interface Mixin<D, P, C, M> {
107
107
  properties?: P
108
108
  computed?: C
109
109
  methods?: M
110
-
111
110
  [index: string]: any
112
111
  }
113
112
 
@@ -119,9 +118,8 @@ type UnboxMixinsField<Mi extends Array<any>, F> =
119
118
  interface Context {
120
119
  triggerEvent: WechatMiniprogram.Component.InstanceMethods<Record<string, any>>['triggerEvent']
121
120
  refs: ObjectOf<WechatMiniprogram.NodesRef & ComponentIns<{}, {}, {}, {}, []>>
122
-
121
+ asyncRefs: ObjectOf<Promise<WechatMiniprogram.NodesRef & ComponentIns<{}, {}, {}, {}, []>>>
123
122
  forceUpdate (params?: object, callback?: () => void): void
124
-
125
123
  selectComponent: ReplaceWxComponentIns['selectComponent']
126
124
  selectAllComponents: ReplaceWxComponentIns['selectAllComponents']
127
125
  createSelectorQuery: WechatMiniprogram.Component.InstanceMethods<Record<string, any>>['createSelectorQuery']
@@ -194,29 +192,18 @@ export function observable<T extends object> (obj: T): T
194
192
  type MpxComProps<O> = { $rawOptions: O }
195
193
 
196
194
  export interface MpxComponentIns {
197
-
198
195
  $refs: ObjectOf<WechatMiniprogram.NodesRef & ComponentIns<{}, {}, {}, {}, []>>
199
-
196
+ $asyncRefs : ObjectOf<Promise<WechatMiniprogram.NodesRef & ComponentIns<{}, {}, {}, {}, []>>>
200
197
  $set: typeof set
201
-
202
198
  $remove: typeof del
203
-
204
199
  $delete: typeof del
205
-
206
200
  $watch (expr: string | (() => any), handler: WatchHandler | WatchOptWithHandler, options?: WatchOpt): () => void
207
-
208
201
  $forceUpdate (params?: object, callback?: () => void): void
209
-
210
202
  $nextTick (fn: () => void): void
211
-
212
203
  $t: typeof t
213
-
214
204
  $tc: typeof tc
215
-
216
205
  $te: typeof te
217
-
218
206
  $tm: typeof tm
219
-
220
207
  [k: string]: any
221
208
  }
222
209
 
@@ -516,11 +503,6 @@ export function reactive<T extends object> (target: T): Reactive<T>
516
503
 
517
504
  export function isReactive (value: unknown): boolean
518
505
 
519
- /**
520
- * Return a shallowly-reactive copy of the original object, where only the root
521
- * level properties are reactive. It also does not auto-unwrap refs (even at the
522
- * root level).
523
- */
524
506
  export function shallowReactive<T extends object> (target: T): ShallowReactive<T>
525
507
 
526
508
  export function computed<T> (
@@ -618,7 +600,7 @@ export function onTabItemTap (callback: () => void): void
618
600
  export function onSaveExitState (callback: () => void): void
619
601
 
620
602
  // get instance
621
- export function getCurrentInstance<T extends object> (): T
603
+ export function getCurrentInstance<T extends MpxComponentIns> (): T
622
604
 
623
605
  // I18n
624
606
  export function useI18n<Options extends {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/core",
3
- "version": "2.8.0-beta.5",
3
+ "version": "2.8.1",
4
4
  "description": "mpx runtime core",
5
5
  "keywords": [
6
6
  "miniprogram",
@@ -19,7 +19,7 @@
19
19
  ],
20
20
  "main": "src/index.js",
21
21
  "dependencies": {
22
- "@mpxjs/utils": "^2.8.0-beta.4",
22
+ "@mpxjs/utils": "^2.8.1",
23
23
  "lodash": "^4.1.1",
24
24
  "miniprogram-api-typings": "^3.0.2"
25
25
  },
@@ -47,5 +47,5 @@
47
47
  "url": "https://github.com/didi/mpx/issues"
48
48
  },
49
49
  "sideEffects": false,
50
- "gitHead": "4e1d6b16c406d3caac0dabb4fae9bc16ac330755"
50
+ "gitHead": "19f21ed57a671536eb1b2e059758118b026bab25"
51
51
  }
package/src/core/proxy.js CHANGED
@@ -215,8 +215,6 @@ export default class MpxProxy {
215
215
  // 强制执行render
216
216
  this.target.$forceUpdate = this.forceUpdate.bind(this)
217
217
  this.target.$nextTick = nextTick
218
- // 挂载$refs对象
219
- this.target.$refs = {}
220
218
  }
221
219
  }
222
220
 
@@ -234,6 +232,7 @@ export default class MpxProxy {
234
232
  {
235
233
  triggerEvent: this.target.triggerEvent.bind(this.target),
236
234
  refs: this.target.$refs,
235
+ asyncRefs: this.target.$asyncRefs,
237
236
  forceUpdate: this.forceUpdate.bind(this),
238
237
  selectComponent: this.target.selectComponent.bind(this.target),
239
238
  selectAllComponents: this.target.selectAllComponents.bind(this.target),
@@ -482,10 +481,8 @@ export default class MpxProxy {
482
481
  this.forceUpdateAll = false
483
482
  }
484
483
 
485
- /**
486
- * mounted之后才接收回调来触发updated钩子,换言之mounted之前修改数据是不会触发updated的
487
- */
488
484
  let callback = cb
485
+ // mounted之后才会触发BEFOREUPDATE/UPDATED
489
486
  if (this.isMounted()) {
490
487
  this.callHook(BEFOREUPDATE)
491
488
  callback = () => {
@@ -494,6 +491,7 @@ export default class MpxProxy {
494
491
  renderTask && renderTask.resolve()
495
492
  }
496
493
  }
494
+
497
495
  data = processUndefined(data)
498
496
  if (typeof Mpx.config.setDataHandler === 'function') {
499
497
  try {
@@ -501,6 +499,7 @@ export default class MpxProxy {
501
499
  } catch (e) {
502
500
  }
503
501
  }
502
+
504
503
  this.target.__render(data, callback)
505
504
  }
506
505
 
@@ -1,4 +1,4 @@
1
- import { CREATED, BEFOREMOUNT, BEFOREUPDATE, UNMOUNTED } from '../../core/innerLifecycle'
1
+ import { CREATED, BEFORECREATE, BEFOREUPDATE, UNMOUNTED } from '../../core/innerLifecycle'
2
2
  import { noop, error } from '@mpxjs/utils'
3
3
  import { getEnvObj } from '@mpxjs/utils/src/env'
4
4
 
@@ -16,38 +16,42 @@ const setNodeRef = function (target, ref) {
16
16
  }
17
17
 
18
18
  const setComponentRef = function (target, ref, isAsync) {
19
- let cacheRef = null
20
- const targetRefs = isAsync ? (target.$asyncRefs || (target.$asyncRefs = {})) : target.$refs
21
- Object.defineProperty(targetRefs, ref.key, {
19
+ const targetRefs = isAsync ? target.$asyncRefs : target.$refs
20
+ const cacheMap = isAsync ? target.__asyncRefCacheMap : target.__refCacheMap
21
+ const key = ref.key
22
+ Object.defineProperty(targetRefs, key, {
22
23
  enumerable: true,
23
24
  configurable: true,
24
25
  get () {
25
26
  // wx由于分包异步化的存在,每次访问refs都需要重新执行selectComponent,避免一直拿到缓存中的placeholder
26
- if (__mpx_mode__ === 'wx' || !cacheRef) {
27
- return (cacheRef = target.__getRefNode(ref, isAsync))
27
+ if (__mpx_mode__ === 'wx' || !cacheMap.get(key)) {
28
+ cacheMap.set(key, target.__getRefNode(ref, isAsync))
28
29
  }
29
- return cacheRef
30
+ return cacheMap.get(key)
30
31
  }
31
32
  })
32
33
  }
33
34
 
34
35
  export default function getRefsMixin () {
35
36
  const refsMixin = {
36
- [BEFOREMOUNT] () {
37
+ [BEFORECREATE] () {
38
+ this.$refs = {}
39
+ this.$asyncRefs = {}
40
+ this.__refCacheMap = new Map()
41
+ this.__asyncRefCacheMap = new Map()
37
42
  this.__getRefs()
38
43
  },
39
44
  [BEFOREUPDATE] () {
40
- this.__getRefs()
45
+ this.__refCacheMap.clear()
46
+ this.__asyncRefCacheMap.clear()
41
47
  },
42
48
  methods: {
43
49
  __getRefs () {
44
50
  if (this.__getRefsData) {
45
51
  const refs = this.__getRefsData()
46
-
47
52
  refs.forEach(ref => {
48
53
  const setRef = ref.type === 'node' ? setNodeRef : setComponentRef
49
54
  setRef(this, ref)
50
-
51
55
  if (__mpx_mode__ === 'tt' && ref.type === 'component') {
52
56
  setComponentRef(this, ref, true)
53
57
  }
@@ -108,7 +112,7 @@ export default function getRefsMixin () {
108
112
  selectorQuery.exec = function (originalCb = noop) {
109
113
  const cb = function (results) {
110
114
  results.forEach((item, index) => {
111
- cbs[index](item)
115
+ cbs[index] && cbs[index](item)
112
116
  })
113
117
  originalCb(results)
114
118
  }