Skip to content

Commit b9b83ed

Browse files
committed
fix(wx-react): 修复_r 替换bug, 修复生命周期等bug
1 parent fb9fb20 commit b9b83ed

File tree

3 files changed

+130
-105
lines changed

3 files changed

+130
-105
lines changed

packages/wx-react/miniprogram_dist/AllComponent.js

+127-97
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ const P_R = Promise.resolve()
7171
*
7272
*/
7373
export class BaseComponent {
74-
7574
getTopDiuu() {
7675
let diuu = null
7776

@@ -88,9 +87,41 @@ export class BaseComponent {
8887
return diuu
8988
}
9089

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+
}
94125
}
95126

96127

@@ -112,32 +143,21 @@ export class BaseComponent {
112143
} else {
113144
const start = Date.now()
114145
wxInst.setData({_r: allData}, () => {
115-
console.log('first duration:', Date.now() - start)
116-
// this也会被收集进来,pop 把this移除掉
117146
const firstReplaceRAllList = getRAllList(this)
147+
console.log('first duration:', Date.now() - start, this, allData)
118148

119-
let hasAddCb = false
120149
if (firstReplaceRAllList.length > 0) {
121150
const start = Date.now()
122151
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)
140155
}
156+
157+
wxInst.setData({}, () => {
158+
console.log('first replace duration:', Date.now() - start, firstReplaceRAllList)
159+
recursionMount(this)
160+
})
141161
})
142162
} else {
143163
recursionMount(this)
@@ -148,13 +168,15 @@ export class BaseComponent {
148168

149169

150170
/**
151-
* 刷新数据到小程序
171+
* 刷新数据到小程序,使用 groupSetData 来优化多次setData
152172
* @param cb
153173
* @param styleUpdater 上报样式的updater
154174
*/
155175
updateWX(cb, styleUpdater) {
156-
const updaterList = []
157-
const firstReplaceRList = []
176+
let updaterList = []
177+
178+
// 可能会收集出重复的,所以使用set结构
179+
const firstReplaceRList = new Set()
158180

159181
let gpr = null
160182
const groupPromise = new Promise((resolve) => {
@@ -177,56 +199,40 @@ export class BaseComponent {
177199
gpr()
178200
return
179201
}
180-
/// groupSetData 来优化多次setData
181202

182203
const topWX = styleUpdater ? styleUpdater.inst : this.getWxInst()
183204

184-
if (firstReplaceRList.length > 0) {
205+
if (firstReplaceRList.size > 0) {
185206
groupPromise.then(() => {
186207
const start = Date.now()
187-
console.log('update Replace R:', firstReplaceRList)
188-
189-
let hasAddCb = false
190208
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+
})
193212

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+
})
209217
})
210218
})
211219
} else {
212220
frp()
213221
}
214222

215223
topWX.groupSetData(() => {
216-
console.log('update wow:', updaterList)
224+
updaterList = simpleUpdaterList(updaterList)
217225
const start = Date.now()
218226

219-
for(let i = updaterList.length - 1; i >= 0; i --) {
227+
for(let i = 0; i < updaterList.length; i ++) {
220228
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)
229230
}
231+
232+
topWX.setData({}, () => {
233+
console.log('update duration:', Date.now() - start, updaterList)
234+
gpr()
235+
})
230236
})
231237
}
232238

@@ -257,29 +263,19 @@ export class BaseComponent {
257263
recursionMount(child)
258264
updatePros.push(P_R)
259265
} 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+
})
278273

279-
firstReplaceRList.push(...getRAllList(child))
274+
addAllReplaceR(firstReplaceRList, getRAllList(comp))
280275

281276
const p = new Promise((resolve) => {
282277
firstReplacePromise.then(() => {
278+
console.log('recursionMount:', child)
283279
recursionMount(child)
284280
resolve()
285281
})
@@ -342,29 +338,17 @@ export class BaseComponent {
342338
updatePros.push(groupPromise)
343339
}
344340
} 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()
349343
updaterList.push({
350-
inst: topParent.getWxInst(),
344+
inst: wx,
351345
data: {
352-
[`${top._keyPath}R`]: this._r
346+
[key]: {...this._r} // 需要展开,以免_r 被污染
353347
}
354348
})
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-
})
366349

367-
updatePros.push(p)
350+
addAllReplaceR(firstReplaceRList, getRAllList(comp))
351+
updatePros.push(groupPromise)
368352
}
369353

370354
this._or = {}
@@ -620,6 +604,52 @@ function recursionCollectChild(inst, descendantList) {
620604
return
621605
}
622606

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+
})
624622
}
625623

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+

packages/wx-react/miniprogram_dist/InstanceManager.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,6 @@ class InstanceManager {
7676
}
7777
}
7878

79-
export default new InstanceManager()
79+
wx.__III = new InstanceManager()
80+
81+
export default wx.__III

packages/wx-react/miniprogram_dist/createElement.js

-7
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@ export default function createElement(comp, props, ...args) {
1717
return
1818
}
1919

20-
if (props && typeof comp === 'string' && comp.endsWith('CPT') && !props.CPTVnode) {
21-
//TODO 这里假定对于this.props.xComponent的使用 只有两种情况1. 渲染为组件 2.逻辑代码判断 比如if(this.props.xComponent)
22-
//TODO 情况1, CPTVnode 等于null就是不渲染,这里返回null 也是不渲染。 情况2 这里返回null表示false, 否则返回了下面的ReactElemnt对象 表示true
23-
return props.CPTVnode
24-
}
25-
26-
2720
let children = []
2821
for (let i = 0; i < args.length; i++) {
2922
if (args[i] instanceof Array) {

0 commit comments

Comments
 (0)