@@ -19,6 +19,7 @@ import {
19
19
getRealOc ,
20
20
invokeWillUnmount ,
21
21
recursionFirstFlushWX ,
22
+ recursionFirstFlushWXSync ,
22
23
getShowUpdaterMap ,
23
24
HIDDEN_STYLE ,
24
25
recursionMountOrUpdate
@@ -111,6 +112,21 @@ import shallowEqual from './shallowEqual'
111
112
*
112
113
* 特别的,当Father是页面组件,且是第一次初始化的时候,每一层级的节点将依次产生,一共会发生n(组件树层级)次groupSetData
113
114
*
115
+ *
116
+ * 另外微信小程序自定义组件的 setData行为是这样的(groupSetData同setData),当执行setData以后,新产生A组件的时候,调用如下:
117
+ *
118
+ * A created
119
+ * A attached
120
+ * setData 调用返回
121
+ * A ready
122
+ * setData callback 调用
123
+ *
124
+ * 所以这里的setData/groupSetData 调用结束之后,A组件attached的生命周期已经执行,这个时候InstanceManager已经管理A,可以不用等到
125
+ * callback之后去执行下一次groupSetData
126
+ *
127
+ * 但是其他版本的小程序【百度,支付宝】是否如此,暂无测试,所以保留 sync /async 两个版本的 firstUpdateWX updateWX recursionFirstFlushWX
128
+ *
129
+ *
114
130
*/
115
131
export class BaseComponent {
116
132
@@ -227,6 +243,57 @@ export class BaseComponent {
227
243
}
228
244
}
229
245
246
+ firstUpdateWXSync ( ) {
247
+ const deepComp = this . getDeepComp ( )
248
+ if ( ! deepComp || Object . keys ( deepComp . _r ) . length === 0 ) {
249
+ // 页面组件render null
250
+ recursionMountOrUpdate ( this )
251
+ return
252
+ }
253
+
254
+
255
+ const pageWxInst = this . getWxInst ( )
256
+ const comps = [ ]
257
+ // 收集下一次groupSetData的实例
258
+ deepComp . _c . forEach ( child => {
259
+ if ( child . _myOutStyle ) {
260
+ const childComp = child . getDeepComp ( )
261
+ comps . push ( childComp )
262
+ }
263
+ } )
264
+
265
+ if ( comps . length === 0 ) {
266
+ pageWxInst . setData ( {
267
+ _r : deepComp . _r
268
+ } , ( ) => {
269
+ recursionMountOrUpdate ( this )
270
+ } )
271
+ } else {
272
+ const styleKey = deepComp . firstStyleKey
273
+ const styleValue = deepComp . _r [ styleKey ]
274
+ const pageShowUpdater = {
275
+ inst : pageWxInst ,
276
+ data : {
277
+ [ `_r.${ styleKey } ` ] : styleValue
278
+ }
279
+ }
280
+
281
+ pageWxInst . groupSetData ( ( ) => {
282
+ pageWxInst . setData ( {
283
+ _r : {
284
+ ...deepComp . _r ,
285
+ [ deepComp . firstStyleKey ] : `${ styleValue } ${ HIDDEN_STYLE } `
286
+ }
287
+ } )
288
+
289
+ //pageWxInst.setData 之后 已经可以获取子组件实例
290
+ recursionFirstFlushWXSync ( this , pageWxInst , comps , [ pageShowUpdater ] , ( ) => {
291
+ recursionMountOrUpdate ( this )
292
+ } )
293
+ } )
294
+ }
295
+ }
296
+
230
297
/**
231
298
* 刷新数据到小程序
232
299
* @param cb
@@ -276,6 +343,65 @@ export class BaseComponent {
276
343
} )
277
344
}
278
345
346
+ /**
347
+ * 刷新数据到小程序
348
+ * @param cb
349
+ * @param styleUpdater 上报样式的updater
350
+ */
351
+ updateWXSync ( cb , styleUpdater ) {
352
+ const flushList = [ ]
353
+ const firstFlushList = [ ]
354
+
355
+ this . updateWXInner ( flushList , firstFlushList )
356
+
357
+ if ( styleUpdater ) {
358
+ flushList . push ( styleUpdater )
359
+ }
360
+
361
+ if ( flushList . length === 0 ) {
362
+ recursionMountOrUpdate ( this )
363
+ cb && cb ( )
364
+ return
365
+ }
366
+
367
+ const showUpdaterMap = getShowUpdaterMap ( firstFlushList )
368
+ const showUpdaterList = Array . from ( showUpdaterMap . values ( ) )
369
+
370
+ /// groupSetData 来优化多次setData
371
+
372
+ const topWX = styleUpdater ? styleUpdater . inst : this . getWxInst ( )
373
+ topWX . groupSetData ( ( ) => {
374
+ for ( let i = 0 ; i < flushList . length ; i ++ ) {
375
+ const { inst, data} = flushList [ i ]
376
+
377
+ const updater = showUpdaterMap . get ( inst )
378
+ if ( updater ) {
379
+ Object . assign ( data , updater . hiddenData )
380
+ }
381
+
382
+
383
+ if ( showUpdaterList . length === 0 ) {
384
+ if ( i === 0 ) {
385
+ inst . setData ( data , ( ) => {
386
+ recursionMountOrUpdate ( this )
387
+ cb && cb ( )
388
+ } )
389
+ } else {
390
+ inst . setData ( data )
391
+ }
392
+ } else {
393
+ inst . setData ( data )
394
+ }
395
+ }
396
+
397
+ if ( showUpdaterList . length !== 0 ) {
398
+ recursionFirstFlushWXSync ( this , topWX , firstFlushList , showUpdaterList , ( ) => {
399
+ recursionMountOrUpdate ( this )
400
+ cb && cb ( )
401
+ } )
402
+ }
403
+ } )
404
+ }
279
405
280
406
/**
281
407
* 递归程序。 主要做两个事情
@@ -491,7 +617,7 @@ export class Component extends BaseComponent {
491
617
const oldOutStyle = this . _myOutStyle
492
618
493
619
if ( this . isPageComp ) {
494
- this . updateWX ( finalCb )
620
+ this . updateWXSync ( finalCb )
495
621
return
496
622
}
497
623
@@ -523,7 +649,7 @@ export class Component extends BaseComponent {
523
649
524
650
const wxInst = pp . getWxInst ( )
525
651
526
- this . updateWX ( finalCb , {
652
+ this . updateWXSync ( finalCb , {
527
653
inst : wxInst ,
528
654
data : {
529
655
[ stylePath ] : newOutStyle
@@ -535,7 +661,7 @@ export class Component extends BaseComponent {
535
661
p = p . _p
536
662
}
537
663
} else {
538
- this . updateWX ( finalCb )
664
+ this . updateWXSync ( finalCb )
539
665
}
540
666
}
541
667
}
0 commit comments