Skip to content

Commit f89ce7a

Browse files
committed
feat(wx-react): 增加对forceUpdate的批量处理
1 parent cdf8292 commit f89ce7a

File tree

3 files changed

+38
-45
lines changed

3 files changed

+38
-45
lines changed

packages/wx-react/miniprogram_dist/AllComponent.js

+18-43
Original file line numberDiff line numberDiff line change
@@ -169,61 +169,52 @@ export class Component extends BaseComponent {
169169
}
170170

171171
forceUpdate(cb) {
172-
if (!(this instanceof HocComponent) && !this.getWxInst()) {
173-
console.warn('the component has unmount, should not invoke forceUpdate!!')
174-
return
175-
}
176-
177-
this.componentWillUpdate && this.componentWillUpdate(this.props, this.state)
178-
this.UNSAFE_componentWillUpdate && this.UNSAFE_componentWillUpdate(this.props, this.state)
179-
180-
const subVnode = this.render()
181-
this._or = this._r
182-
this._r = {}
183-
this._c = []
184-
this.__eventHanderMap = {}
185-
186-
const parentContext = this._parentContext
187-
const context = getCurrentContext(this, parentContext)
188-
189-
render(subVnode, this, context, this._r, this._or, '_r')
190-
191-
this.flushDataToWx(subVnode, cb)
172+
this.updateInner({}, cb, true)
192173
}
193174

194175
setState(newState, cb) {
195-
// 还未渲染完成调用setState,通常发生在willMount等处,此时把更新入队,在渲染结束的时候会统一检查一下这种情况的更新
176+
this.updateInner(newState, cb, false)
177+
}
178+
179+
updateInner(newState, cb, isForce) {
180+
// 还未渲染完成调用setState/forceUpdate,通常发生在willMount等处,此时把更新入队,在渲染结束的时候会统一检查一下这种情况的更新
196181
if (!(this instanceof HocComponent) && this.firstRender !== FR_DONE) {
197182
this.updateQueue.push(newState)
198183
cb && this.updateQueueCB.push(cb)
184+
if (isForce) {
185+
this.isForceUpdate = isForce
186+
}
199187

200188
return
201189
}
202190

203191
// 没有找到对应wxInst
204192
if (!(this instanceof HocComponent) && !this.getWxInst()) {
205-
console.warn('the component has unmount, should not invoke setState!!')
193+
console.warn(`the component has unmount, should not invoke ${isForce ? 'forceUpdate' : 'setState'}!!`)
206194
return
207195
}
208196

209-
210197
if (reactUpdate.inRe()) {
211198
this.updateQueue.push(newState)
212199
if (cb) {
213200
this.updateQueueCB.push(cb)
214201
}
202+
if (isForce) {
203+
this.isForceUpdate = isForce
204+
}
215205
reactUpdate.addUpdateInst(this)
216206
return
217207
}
218208

219-
220209
const nextState = {
221210
...this.state,
222211
...newState
223212
}
224213

225214
let shouldUpdate
226-
if (this.shouldComponentUpdate) {
215+
if (isForce) {
216+
shouldUpdate = true
217+
} else if (this.shouldComponentUpdate) {
227218
shouldUpdate = this.shouldComponentUpdate(this.props, nextState)
228219
} else {
229220
shouldUpdate = true
@@ -232,14 +223,12 @@ export class Component extends BaseComponent {
232223
shouldUpdate && this.componentWillUpdate && this.componentWillUpdate(this.props, nextState)
233224
shouldUpdate && this.UNSAFE_componentWillUpdate && this.UNSAFE_componentWillUpdate(this.props, nextState)
234225

235-
236226
this.state = nextState
237227

238228
if (!shouldUpdate) {
239229
return // do nothing
240230
}
241231

242-
243232
const subVnode = this.render()
244233

245234
/// 重置实例字段:_or 旧的渲染数据, _r 渲染数据, _c 所以孩子实例 , __eventHanderMap 方法回调map
@@ -255,25 +244,11 @@ export class Component extends BaseComponent {
255244

256245
render(subVnode, this, context, this._r, this._or, '_r')
257246

258-
let finalCb = null
259-
// 合并setState 回调
260-
if (this.updateQueueCB.length > 0) {
261-
const cbQueue = this.updateQueueCB
262-
finalCb = () => {
263-
for (let i = 0; i < cbQueue.length; i++) {
264-
cbQueue[i].call(this)
265-
}
266-
}
267-
this.updateQueueCB = []
268-
} else {
269-
finalCb = cb
270-
}
271-
272-
this.flushDataToWx(subVnode, finalCb)
247+
this.flushDataToWx(subVnode, cb)
273248
}
274249

275250
/**
276-
* render过程结束之后,各组件_r 数据已经生成完毕,执行updateUI递归的把数据刷新到小程序。
251+
* render过程结束之后,各组件渲染数据已经生成完毕,执行updateUI递归的把数据刷新到小程序。
277252
* 此外:由于微信小程序自定义组件会生成一个节点,需要把内部最外层样式上报到这个节点
278253
*/
279254
flushDataToWx(subVnode, finalCb) {

packages/wx-react/miniprogram_dist/ReactUpdate.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,22 @@ class ReactUpdate {
4848
Object.assign(newState, inst.updateQueue[j])
4949
}
5050
inst.updateQueue = []
51-
inst.setState(newState)
51+
52+
let finalCb = null
53+
if (inst.updateQueueCB.length > 0) {
54+
const cbQueue = inst.updateQueueCB
55+
finalCb = () => {
56+
for (let i = 0; i < cbQueue.length; i++) {
57+
cbQueue[i].call(inst)
58+
}
59+
}
60+
inst.updateQueueCB = []
61+
}
62+
63+
const isForceUpdate = inst.isForceUpdate
64+
inst.isForceUpdate = false
65+
66+
inst.updateInner(newState, finalCb, isForceUpdate)
5267
}
5368
}
5469

packages/wx-react/miniprogram_dist/util.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ export function recursionMount(comp) {
132132
comp.updateQueueCB = []
133133
}
134134

135-
comp.setState(newState, finalCb)
135+
const isForceUpdate = comp.isForceUpdate
136+
comp.isForceUpdate = false
137+
138+
comp.updateInner(newState, finalCb, isForceUpdate)
136139
}
137140

138141

0 commit comments

Comments
 (0)