@@ -71,7 +71,6 @@ const P_R = Promise.resolve()
71
71
*
72
72
*/
73
73
export class BaseComponent {
74
-
75
74
getTopDiuu ( ) {
76
75
let diuu = null
77
76
@@ -88,9 +87,41 @@ export class BaseComponent {
88
87
return diuu
89
88
}
90
89
91
- getHocTop ( ) {
92
- const diuu = this . getTopDiuu ( )
93
- return instanceManager . getCompInstByUUID ( diuu )
90
+ /**
91
+ * 当组件新产生的时候,获取刷数据的 目标小程序实例,数据路径
92
+ 需要考虑 自定义组件render返回null的时候,需要往回追溯
93
+ *
94
+ * @returns {* }
95
+ */
96
+ getTopWx ( ) {
97
+ if ( this . isPageComp ) {
98
+ const diuu = this . getTopDiuu ( )
99
+ compInst = instanceManager . getCompInstByUUID ( diuu )
100
+ return {
101
+ comp : compInst ,
102
+ wx : this . getWxInst ( ) ,
103
+ key : '_r'
104
+ }
105
+ }
106
+
107
+ let compInst = this
108
+ let wxParent = null
109
+ let key = '_r'
110
+
111
+ while ( ! wxParent ) {
112
+ const diuu = compInst . getTopDiuu ( )
113
+ compInst = instanceManager . getCompInstByUUID ( diuu )
114
+
115
+ wxParent = compInst . _p . getWxInst ( )
116
+ key = key . replace ( '_r' , compInst . _keyPath + 'R' )
117
+ compInst = compInst . _p
118
+ }
119
+
120
+ return {
121
+ comp : compInst ,
122
+ wx : wxParent ,
123
+ key : key ,
124
+ }
94
125
}
95
126
96
127
@@ -112,32 +143,21 @@ export class BaseComponent {
112
143
} else {
113
144
const start = Date . now ( )
114
145
wxInst . setData ( { _r : allData } , ( ) => {
115
- console . log ( 'first duration:' , Date . now ( ) - start )
116
- // this也会被收集进来,pop 把this移除掉
117
146
const firstReplaceRAllList = getRAllList ( this )
147
+ console . log ( 'first duration:' , Date . now ( ) - start , this , allData )
118
148
119
- let hasAddCb = false
120
149
if ( firstReplaceRAllList . length > 0 ) {
121
150
const start = Date . now ( )
122
151
wxInst . groupSetData ( ( ) => {
123
- for ( let i = firstReplaceRAllList . length - 1 ; i >= 0 ; i -- ) {
124
- const inst = firstReplaceRAllList [ i ]
125
- const wxItem = inst . getWxInst ( )
126
-
127
- if ( wxItem . data . _r ) {
128
- continue
129
- }
130
-
131
- if ( ! hasAddCb ) {
132
- hasAddCb = true
133
- wxItem . setData ( { _r : inst . _r } , ( ) => {
134
- console . log ( 'first replace duration:' , Date . now ( ) - start )
135
- recursionMount ( this )
136
- } )
137
- } else {
138
- wxItem . setData ( { _r : inst . _r } )
139
- }
152
+ for ( let i = 0 ; i < firstReplaceRAllList . length ; i ++ ) {
153
+ const { inst, data} = firstReplaceRAllList [ i ]
154
+ inst . setData ( data )
140
155
}
156
+
157
+ wxInst . setData ( { } , ( ) => {
158
+ console . log ( 'first replace duration:' , Date . now ( ) - start , firstReplaceRAllList )
159
+ recursionMount ( this )
160
+ } )
141
161
} )
142
162
} else {
143
163
recursionMount ( this )
@@ -148,13 +168,15 @@ export class BaseComponent {
148
168
149
169
150
170
/**
151
- * 刷新数据到小程序
171
+ * 刷新数据到小程序,使用 groupSetData 来优化多次setData
152
172
* @param cb
153
173
* @param styleUpdater 上报样式的updater
154
174
*/
155
175
updateWX ( cb , styleUpdater ) {
156
- const updaterList = [ ]
157
- const firstReplaceRList = [ ]
176
+ let updaterList = [ ]
177
+
178
+ // 可能会收集出重复的,所以使用set结构
179
+ const firstReplaceRList = new Set ( )
158
180
159
181
let gpr = null
160
182
const groupPromise = new Promise ( ( resolve ) => {
@@ -177,56 +199,40 @@ export class BaseComponent {
177
199
gpr ( )
178
200
return
179
201
}
180
- /// groupSetData 来优化多次setData
181
202
182
203
const topWX = styleUpdater ? styleUpdater . inst : this . getWxInst ( )
183
204
184
- if ( firstReplaceRList . length > 0 ) {
205
+ if ( firstReplaceRList . size > 0 ) {
185
206
groupPromise . then ( ( ) => {
186
207
const start = Date . now ( )
187
- console . log ( 'update Replace R:' , firstReplaceRList )
188
-
189
- let hasAddCb = false
190
208
topWX . groupSetData ( ( ) => {
191
- for ( let i = firstReplaceRList . length - 1 ; i >= 0 ; i -- ) {
192
- const inst = firstReplaceRList [ i ]
209
+ firstReplaceRList . forEach ( ( { inst , data} ) => {
210
+ inst . setData ( data )
211
+ } )
193
212
194
- const wxItem = inst . getWxInst ( )
195
- if ( wxItem . data . _r ) {
196
- continue
197
- }
198
-
199
- if ( ! hasAddCb ) {
200
- hasAddCb = true
201
- wxItem . setData ( { _r : inst . _r } , ( ) => {
202
- console . log ( 'update Replace duration:' , Date . now ( ) - start )
203
- frp ( )
204
- } )
205
- } else {
206
- wxItem . setData ( { _r : inst . _r } )
207
- }
208
- }
213
+ topWX . setData ( { } , ( ) => {
214
+ console . log ( 'update Replace duration:' , Date . now ( ) - start , firstReplaceRList )
215
+ frp ( )
216
+ } )
209
217
} )
210
218
} )
211
219
} else {
212
220
frp ( )
213
221
}
214
222
215
223
topWX . groupSetData ( ( ) => {
216
- console . log ( 'update wow:' , updaterList )
224
+ updaterList = simpleUpdaterList ( updaterList )
217
225
const start = Date . now ( )
218
226
219
- for ( let i = updaterList . length - 1 ; i >= 0 ; i -- ) {
227
+ for ( let i = 0 ; i < updaterList . length ; i ++ ) {
220
228
const { inst, data} = updaterList [ i ]
221
- if ( i === 0 ) {
222
- inst . setData ( data , ( ) => {
223
- console . log ( 'update duration:' , Date . now ( ) - start )
224
- gpr ( )
225
- } )
226
- } else {
227
- inst . setData ( data )
228
- }
229
+ inst . setData ( data )
229
230
}
231
+
232
+ topWX . setData ( { } , ( ) => {
233
+ console . log ( 'update duration:' , Date . now ( ) - start , updaterList )
234
+ gpr ( )
235
+ } )
230
236
} )
231
237
}
232
238
@@ -257,29 +263,19 @@ export class BaseComponent {
257
263
recursionMount ( child )
258
264
updatePros . push ( P_R )
259
265
} else {
260
- const top = child . getHocTop ( )
261
- if ( ! top . _p ) {
262
- updaterList . push ( {
263
- inst : top . getWxInst ( ) ,
264
- data : {
265
- _r : allSubData
266
- }
267
- } )
268
- } else {
269
- const topParent = top . _p
270
-
271
- updaterList . push ( {
272
- inst : topParent . getWxInst ( ) ,
273
- data : {
274
- [ `${ top . _keyPath } R` ] : allSubData
275
- }
276
- } )
277
- }
266
+ const { wx, comp, key} = child . getTopWx ( )
267
+ updaterList . push ( {
268
+ inst : wx ,
269
+ data : {
270
+ [ key ] : allSubData
271
+ }
272
+ } )
278
273
279
- firstReplaceRList . push ( ... getRAllList ( child ) )
274
+ addAllReplaceR ( firstReplaceRList , getRAllList ( comp ) )
280
275
281
276
const p = new Promise ( ( resolve ) => {
282
277
firstReplacePromise . then ( ( ) => {
278
+ console . log ( 'recursionMount:' , child )
283
279
recursionMount ( child )
284
280
resolve ( )
285
281
} )
@@ -342,29 +338,17 @@ export class BaseComponent {
342
338
updatePros . push ( groupPromise )
343
339
}
344
340
} else {
345
- // 自定义组件 render null 的情况
346
- const top = this . getHocTop ( )
347
- const topParent = top . _p
348
-
341
+ // 自定义组件在上一次的render中,返回了null
342
+ const { wx, key, comp} = this . getTopWx ( )
349
343
updaterList . push ( {
350
- inst : topParent . getWxInst ( ) ,
344
+ inst : wx ,
351
345
data : {
352
- [ ` ${ top . _keyPath } R` ] : this . _r
346
+ [ key ] : { ... this . _r } // 需要展开,以免_r 被污染
353
347
}
354
348
} )
355
- firstReplaceRList . push ( ...getRAllList ( this ) )
356
- const p = new Promise ( ( resolve ) => {
357
- firstReplacePromise . then ( ( ) => {
358
- for ( let i = 0 ; i < this . _c . length ; i ++ ) {
359
- const inst = instanceManager . getCompInstByUUID ( this . _c [ i ] )
360
- recursionMount ( inst )
361
- }
362
-
363
- resolve ( )
364
- } )
365
- } )
366
349
367
- updatePros . push ( p )
350
+ addAllReplaceR ( firstReplaceRList , getRAllList ( comp ) )
351
+ updatePros . push ( groupPromise )
368
352
}
369
353
370
354
this . _or = { }
@@ -620,6 +604,52 @@ function recursionCollectChild(inst, descendantList) {
620
604
return
621
605
}
622
606
623
- descendantList . push ( inst )
607
+ if ( inst . _myOutStyle === false ) {
608
+ return
609
+ }
610
+
611
+ const wxInst = inst . getWxInst ( )
612
+ if ( wxInst . data . _r && ! inst . isPageComp ) {
613
+ return
614
+ }
615
+
616
+ descendantList . unshift ( {
617
+ inst : wxInst ,
618
+ data : {
619
+ ...inst . _r
620
+ }
621
+ } )
624
622
}
625
623
624
+
625
+ function simpleUpdaterList ( list ) {
626
+
627
+ const instMap = new Map ( )
628
+ const simpleList = [ ]
629
+
630
+ for ( let i = list . length - 1 ; i >= 0 ; i -- ) {
631
+ const item = list [ i ]
632
+ const { inst, data} = item
633
+
634
+ if ( instMap . has ( inst ) ) {
635
+ Object . assign ( simpleList [ instMap . get ( inst ) ] . data , data )
636
+ } else {
637
+ simpleList . push ( {
638
+ inst,
639
+ data
640
+ } )
641
+ instMap . set ( inst , simpleList . length - 1 )
642
+ }
643
+ }
644
+
645
+ return simpleList
646
+ }
647
+
648
+
649
+ function addAllReplaceR ( rSet , nrList ) {
650
+ nrList . forEach ( item => {
651
+ rSet . add ( item )
652
+ } )
653
+ }
654
+
655
+
0 commit comments