@samitouri / QOS-React / commits / 21e0e3711b

Use explicit null checks in ReactFiberUpdateQueue

Follow-up to #8978

Andrew Clark committed Mar 9, 2017 at 15:51 UTC 21e0e3711b6a3e59c6e6052332c2d63c08b54dc3
1 file changed +42 -24
src/renderers/shared/fiber/ReactFiberUpdateQueue.js
+42 -24
@@ -90,7 +90,7 @@ function comparePriority(a: PriorityLevel, b: PriorityLevel): number {
90 // Ensures that a fiber has an update queue, creating a new one if needed.
91 // Returns the new or existing queue.
92 function ensureUpdateQueue(fiber: Fiber): UpdateQueue {
93 - if (fiber.updateQueue) {
93 + if (fiber.updateQueue !== null) {
94 // We already have an update queue.
95 return fiber.updateQueue;
96 }
@@ -123,13 +123,15 @@ function cloneUpdateQueue(
123 workInProgress: Fiber,
124 ): UpdateQueue | null {
125 const currentQueue = current.updateQueue;
126 - if (!currentQueue) {
126 + if (currentQueue === null) {
127 // The source fiber does not have an update queue.
128 workInProgress.updateQueue = null;
129 return null;
130 }
131 // 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 + : {};
135 altQueue.first = currentQueue.first;
136 altQueue.last = currentQueue.last;
137
@@ -156,8 +158,13 @@ function cloneUpdate(update: Update): Update {
158 };
159 }
160
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) {
168 insertAfter.next = update;
169 } else {
170 // This is the first item in the queue.
@@ -165,7 +172,7 @@ function insertUpdateIntoQueue(queue, update, insertAfter, insertBefore) {
172 queue.first = update;
173 }
174
168 - if (insertBefore) {
175 + if (insertBefore !== null) {
176 update.next = insertBefore;
177 } else {
178 // This is the last item in the queue.
@@ -180,7 +187,8 @@ function findInsertionPosition(queue, update): Update | null {
187 let insertAfter = null;
188 let insertBefore = null;
189 if (
183 - queue.last && comparePriority(queue.last.priorityLevel, priorityLevel) <= 0
190 + queue.last !== null &&
191 + comparePriority(queue.last.priorityLevel, priorityLevel) <= 0
192 ) {
193 // Fast path for the common case where the update should be inserted at
194 // the end of the queue.
@@ -188,7 +196,7 @@ function findInsertionPosition(queue, update): Update | null {
196 } else {
197 insertBefore = queue.first;
198 while (
191 - insertBefore &&
199 + insertBefore !== null &&
200 comparePriority(insertBefore.priorityLevel, priorityLevel) <= 0
201 ) {
202 insertAfter = insertBefore;
@@ -229,11 +237,13 @@ function findInsertionPosition(queue, update): Update | null {
237 // If the update is cloned, it returns the cloned update.
238 function insertUpdate(fiber: Fiber, update: Update): Update | null {
239 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;
243
244 // Warn if an update is scheduled from inside an updater function.
245 if (__DEV__) {
236 - if (queue1.isProcessing || (queue2 && queue2.isProcessing)) {
246 + if (queue1.isProcessing || (queue2 !== null && queue2.isProcessing)) {
247 warning(
248 false,
249 'An update (setState, replaceState, or forceUpdate) was scheduled ' +
@@ -246,9 +256,11 @@ function insertUpdate(fiber: Fiber, update: Update): Update | null {
256
257 // Find the insertion position in the first queue.
258 const insertAfter1 = findInsertionPosition(queue1, update);
249 - const insertBefore1 = insertAfter1 ? insertAfter1.next : queue1.first;
259 + const insertBefore1 = insertAfter1 !== null
260 + ? insertAfter1.next
261 + : queue1.first;
262
251 - if (!queue2) {
263 + if (queue2 === null) {
264 // If there's no alternate queue, there's nothing else to do but insert.
265 insertUpdateIntoQueue(queue1, update, insertAfter1, insertBefore1);
266 return null;
@@ -256,7 +268,9 @@ function insertUpdate(fiber: Fiber, update: Update): Update | null {
268
269 // If there is an alternate queue, find the insertion position.
270 const insertAfter2 = findInsertionPosition(queue2, update);
259 - const insertBefore2 = insertAfter2 ? insertAfter2.next : queue2.first;
271 + const insertBefore2 = insertAfter2 !== null
272 + ? insertAfter2.next
273 + : queue2.first;
274
275 // Now we can insert into the first queue. This must come after finding both
276 // insertion positions because it mutates the list.
@@ -273,10 +287,10 @@ function insertUpdate(fiber: Fiber, update: Update): Update | null {
287 // queue, it also inserted into the alternate. All we need to do is update
288 // the alternate queue's `first` and `last` pointers, in case they
289 // have changed.
276 - if (!insertAfter2) {
290 + if (insertAfter2 === null) {
291 queue2.first = update;
292 }
279 - if (!insertBefore2) {
293 + if (insertBefore2 === null) {
294 queue2.last = null;
295 }
296 }
@@ -341,7 +355,7 @@ function addForceUpdate(
355 exports.addForceUpdate = addForceUpdate;
356
357 function getPendingPriority(queue: UpdateQueue): PriorityLevel {
344 - return queue.first ? queue.first.priorityLevel : NoWork;
358 + return queue.first !== null ? queue.first.priorityLevel : NoWork;
359 }
360 exports.getPendingPriority = getPendingPriority;
361
@@ -351,7 +365,7 @@ function addTopLevelUpdate(
365 callback: Callback | null,
366 priorityLevel: PriorityLevel,
367 ): void {
354 - const isTopLevelUnmount = !!(partialState && partialState.element === null);
368 + const isTopLevelUnmount = partialState.element === null;
369
370 const update = {
371 priorityLevel,
@@ -368,13 +382,15 @@ function addTopLevelUpdate(
382 // Drop all updates that are lower-priority, so that the tree is not
383 // remounted. We need to do this for both queues.
384 const queue1 = fiber.updateQueue;
371 - const queue2 = fiber.alternate && fiber.alternate.updateQueue;
385 + const queue2 = fiber.alternate !== null
386 + ? fiber.alternate.updateQueue
387 + : null;
388
373 - if (queue1 && update.next) {
389 + if (queue1 !== null && update.next !== null) {
390 update.next = null;
391 queue1.last = update;
392 }
377 - if (queue2 && update2 && update2.next) {
393 + if (queue2 !== null && update2 !== null && update2.next !== null) {
394 update2.next = null;
395 queue2.last = update;
396 }
@@ -414,12 +430,14 @@ function beginUpdateQueue(
430 let dontMutatePrevState = true;
431 let callbackList = null;
432 let update = queue.first;
417 - while (update && comparePriority(update.priorityLevel, priorityLevel) <= 0) {
433 + while (
434 + update !== null && comparePriority(update.priorityLevel, priorityLevel) <= 0
435 + ) {
436 // Remove each update from the queue right before it is processed. That way
437 // if setState is called from inside an updater function, the new update
438 // will be inserted in the correct position.
439 queue.first = update.next;
422 - if (!queue.first) {
440 + if (queue.first === null) {
441 queue.last = null;
442 }
443
@@ -456,7 +474,7 @@ function beginUpdateQueue(
474
475 queue.callbackList = callbackList;
476
459 - if (!queue.first && !callbackList && !queue.hasForceUpdate) {
477 + if (queue.first === null && callbackList === null && !queue.hasForceUpdate) {
478 // The queue is empty and there are no callbacks. We can reset it.
479 workInProgress.updateQueue = null;
480 }
@@ -475,7 +493,7 @@ function commitCallbacks(
493 context: mixed,
494 ) {
495 const callbackList = queue.callbackList;
478 - if (!callbackList) {
496 + if (callbackList === null) {
497 return;
498 }
499 for (let i = 0; i < callbackList.length; i++) {