Skip to content

Commit f606e0e

Browse files
authored
Merge pull request #9145 from acdlite/explicitnullcheckfiberupdatequeue
Use explicit null checks in ReactFiberUpdateQueue
2 parents ad2b654 + bc30ef9 commit f606e0e

File tree

2 files changed

+49
-25
lines changed

2 files changed

+49
-25
lines changed

src/renderers/shared/fiber/ReactFiberScheduler.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,13 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
571571
// Check for pending update priority. This is usually null so it shouldn't
572572
// be a perf issue.
573573
const queue = workInProgress.updateQueue;
574-
if (queue !== null) {
574+
const tag = workInProgress.tag;
575+
if (
576+
queue !== null &&
577+
// TODO: Revisit once updateQueue is typed properly to distinguish between
578+
// update payloads for host components and update queues for composites
579+
(tag === ClassComponent || tag === HostRoot)
580+
) {
575581
newPriority = getPendingPriority(queue);
576582
}
577583

src/renderers/shared/fiber/ReactFiberUpdateQueue.js

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function comparePriority(a: PriorityLevel, b: PriorityLevel): number {
9090
// Ensures that a fiber has an update queue, creating a new one if needed.
9191
// Returns the new or existing queue.
9292
function ensureUpdateQueue(fiber: Fiber): UpdateQueue {
93-
if (fiber.updateQueue) {
93+
if (fiber.updateQueue !== null) {
9494
// We already have an update queue.
9595
return fiber.updateQueue;
9696
}
@@ -123,13 +123,15 @@ function cloneUpdateQueue(
123123
workInProgress: Fiber,
124124
): UpdateQueue | null {
125125
const currentQueue = current.updateQueue;
126-
if (!currentQueue) {
126+
if (currentQueue === null) {
127127
// The source fiber does not have an update queue.
128128
workInProgress.updateQueue = null;
129129
return null;
130130
}
131131
// If the alternate already has a queue, reuse the previous object.
132-
const altQueue = workInProgress.updateQueue || {};
132+
const altQueue = workInProgress.updateQueue !== null
133+
? workInProgress.updateQueue
134+
: {};
133135
altQueue.first = currentQueue.first;
134136
altQueue.last = currentQueue.last;
135137

@@ -156,16 +158,21 @@ function cloneUpdate(update: Update): Update {
156158
};
157159
}
158160

159-
function insertUpdateIntoQueue(queue, update, insertAfter, insertBefore) {
160-
if (insertAfter) {
161+
function insertUpdateIntoQueue(
162+
queue: UpdateQueue,
163+
update: Update,
164+
insertAfter: Update | null,
165+
insertBefore: Update | null,
166+
) {
167+
if (insertAfter !== null) {
161168
insertAfter.next = update;
162169
} else {
163170
// This is the first item in the queue.
164171
update.next = queue.first;
165172
queue.first = update;
166173
}
167174

168-
if (insertBefore) {
175+
if (insertBefore !== null) {
169176
update.next = insertBefore;
170177
} else {
171178
// This is the last item in the queue.
@@ -180,15 +187,16 @@ function findInsertionPosition(queue, update): Update | null {
180187
let insertAfter = null;
181188
let insertBefore = null;
182189
if (
183-
queue.last && comparePriority(queue.last.priorityLevel, priorityLevel) <= 0
190+
queue.last !== null &&
191+
comparePriority(queue.last.priorityLevel, priorityLevel) <= 0
184192
) {
185193
// Fast path for the common case where the update should be inserted at
186194
// the end of the queue.
187195
insertAfter = queue.last;
188196
} else {
189197
insertBefore = queue.first;
190198
while (
191-
insertBefore &&
199+
insertBefore !== null &&
192200
comparePriority(insertBefore.priorityLevel, priorityLevel) <= 0
193201
) {
194202
insertAfter = insertBefore;
@@ -229,11 +237,13 @@ function findInsertionPosition(queue, update): Update | null {
229237
// If the update is cloned, it returns the cloned update.
230238
function insertUpdate(fiber: Fiber, update: Update): Update | null {
231239
const queue1 = ensureUpdateQueue(fiber);
232-
const queue2 = fiber.alternate ? ensureUpdateQueue(fiber.alternate) : null;
240+
const queue2 = fiber.alternate !== null
241+
? ensureUpdateQueue(fiber.alternate)
242+
: null;
233243

234244
// Warn if an update is scheduled from inside an updater function.
235245
if (__DEV__) {
236-
if (queue1.isProcessing || (queue2 && queue2.isProcessing)) {
246+
if (queue1.isProcessing || (queue2 !== null && queue2.isProcessing)) {
237247
warning(
238248
false,
239249
'An update (setState, replaceState, or forceUpdate) was scheduled ' +
@@ -246,17 +256,21 @@ function insertUpdate(fiber: Fiber, update: Update): Update | null {
246256

247257
// Find the insertion position in the first queue.
248258
const insertAfter1 = findInsertionPosition(queue1, update);
249-
const insertBefore1 = insertAfter1 ? insertAfter1.next : queue1.first;
259+
const insertBefore1 = insertAfter1 !== null
260+
? insertAfter1.next
261+
: queue1.first;
250262

251-
if (!queue2) {
263+
if (queue2 === null) {
252264
// If there's no alternate queue, there's nothing else to do but insert.
253265
insertUpdateIntoQueue(queue1, update, insertAfter1, insertBefore1);
254266
return null;
255267
}
256268

257269
// If there is an alternate queue, find the insertion position.
258270
const insertAfter2 = findInsertionPosition(queue2, update);
259-
const insertBefore2 = insertAfter2 ? insertAfter2.next : queue2.first;
271+
const insertBefore2 = insertAfter2 !== null
272+
? insertAfter2.next
273+
: queue2.first;
260274

261275
// Now we can insert into the first queue. This must come after finding both
262276
// insertion positions because it mutates the list.
@@ -273,10 +287,10 @@ function insertUpdate(fiber: Fiber, update: Update): Update | null {
273287
// queue, it also inserted into the alternate. All we need to do is update
274288
// the alternate queue's `first` and `last` pointers, in case they
275289
// have changed.
276-
if (!insertAfter2) {
290+
if (insertAfter2 === null) {
277291
queue2.first = update;
278292
}
279-
if (!insertBefore2) {
293+
if (insertBefore2 === null) {
280294
queue2.last = null;
281295
}
282296
}
@@ -341,7 +355,7 @@ function addForceUpdate(
341355
exports.addForceUpdate = addForceUpdate;
342356

343357
function getPendingPriority(queue: UpdateQueue): PriorityLevel {
344-
return queue.first ? queue.first.priorityLevel : NoWork;
358+
return queue.first !== null ? queue.first.priorityLevel : NoWork;
345359
}
346360
exports.getPendingPriority = getPendingPriority;
347361

@@ -351,7 +365,7 @@ function addTopLevelUpdate(
351365
callback: Callback | null,
352366
priorityLevel: PriorityLevel,
353367
): void {
354-
const isTopLevelUnmount = !!(partialState && partialState.element === null);
368+
const isTopLevelUnmount = partialState.element === null;
355369

356370
const update = {
357371
priorityLevel,
@@ -368,13 +382,15 @@ function addTopLevelUpdate(
368382
// Drop all updates that are lower-priority, so that the tree is not
369383
// remounted. We need to do this for both queues.
370384
const queue1 = fiber.updateQueue;
371-
const queue2 = fiber.alternate && fiber.alternate.updateQueue;
385+
const queue2 = fiber.alternate !== null
386+
? fiber.alternate.updateQueue
387+
: null;
372388

373-
if (queue1 && update.next) {
389+
if (queue1 !== null && update.next !== null) {
374390
update.next = null;
375391
queue1.last = update;
376392
}
377-
if (queue2 && update2 && update2.next) {
393+
if (queue2 !== null && update2 !== null && update2.next !== null) {
378394
update2.next = null;
379395
queue2.last = update;
380396
}
@@ -414,12 +430,14 @@ function beginUpdateQueue(
414430
let dontMutatePrevState = true;
415431
let callbackList = null;
416432
let update = queue.first;
417-
while (update && comparePriority(update.priorityLevel, priorityLevel) <= 0) {
433+
while (
434+
update !== null && comparePriority(update.priorityLevel, priorityLevel) <= 0
435+
) {
418436
// Remove each update from the queue right before it is processed. That way
419437
// if setState is called from inside an updater function, the new update
420438
// will be inserted in the correct position.
421439
queue.first = update.next;
422-
if (!queue.first) {
440+
if (queue.first === null) {
423441
queue.last = null;
424442
}
425443

@@ -456,7 +474,7 @@ function beginUpdateQueue(
456474

457475
queue.callbackList = callbackList;
458476

459-
if (!queue.first && !callbackList && !queue.hasForceUpdate) {
477+
if (queue.first === null && callbackList === null && !queue.hasForceUpdate) {
460478
// The queue is empty and there are no callbacks. We can reset it.
461479
workInProgress.updateQueue = null;
462480
}
@@ -475,7 +493,7 @@ function commitCallbacks(
475493
context: mixed,
476494
) {
477495
const callbackList = queue.callbackList;
478-
if (!callbackList) {
496+
if (callbackList === null) {
479497
return;
480498
}
481499
for (let i = 0; i < callbackList.length; i++) {

0 commit comments

Comments
 (0)