From 5a07c883c76a2230c6d9f220c2cfca025c3ddd46 Mon Sep 17 00:00:00 2001 From: zombieJ Date: Mon, 14 Aug 2017 12:31:03 +0800 Subject: [PATCH 1/4] remove tuple --- .../shared/fiber/ReactFiberUpdateQueue.js | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/renderers/shared/fiber/ReactFiberUpdateQueue.js b/src/renderers/shared/fiber/ReactFiberUpdateQueue.js index e58ce420f37c4..cc99283ca87c9 100644 --- a/src/renderers/shared/fiber/ReactFiberUpdateQueue.js +++ b/src/renderers/shared/fiber/ReactFiberUpdateQueue.js @@ -160,7 +160,7 @@ function findInsertionPosition(queue, update): Update | null { return insertAfter; } -function ensureUpdateQueues(fiber: Fiber): [UpdateQueue, UpdateQueue | null] { +function ensureUpdateQueues(fiber: Fiber) { const alternateFiber = fiber.alternate; let queue1 = fiber.updateQueue; @@ -177,13 +177,6 @@ function ensureUpdateQueues(fiber: Fiber): [UpdateQueue, UpdateQueue | null] { } else { queue2 = null; } - - // TODO: Refactor to avoid returning a tuple. - return [ - queue1, - // Return null if there is no alternate queue, or if its queue is the same. - queue2 !== queue1 ? queue2 : null, - ]; } // The work-in-progress queue is a subset of the current queue (if it exists). @@ -217,7 +210,12 @@ function ensureUpdateQueues(fiber: Fiber): [UpdateQueue, UpdateQueue | null] { // If the update is cloned, it returns the cloned update. function insertUpdate(fiber: Fiber, update: Update): Update | null { // We'll have at least one and at most two distinct update queues. - const [queue1, queue2] = ensureUpdateQueues(fiber); + ensureUpdateQueues(fiber); + const queue1: UpdateQueue = (fiber.updateQueue: any); + const queue2: UpdateQueue | null = fiber.alternate && + fiber.alternate.updateQueue !== queue1 + ? fiber.alternate.updateQueue + : null; // Warn if an update is scheduled from inside an updater function. if (__DEV__) { @@ -356,7 +354,7 @@ function addTopLevelUpdate( ): void { const isTopLevelUnmount = partialState.element === null; - const update = { + const update: Update = { priorityLevel, partialState, callback, @@ -370,7 +368,11 @@ function addTopLevelUpdate( if (isTopLevelUnmount) { // TODO: Redesign the top-level mount/update/unmount API to avoid this // special case. - const [queue1, queue2] = ensureUpdateQueues(fiber); + const queue1: UpdateQueue | null = fiber.updateQueue; + const queue2: UpdateQueue | null = fiber.alternate && + fiber.alternate.updateQueue !== queue1 + ? fiber.alternate.updateQueue + : null; // Drop all updates that are lower-priority, so that the tree is not // remounted. We need to do this for both queues. From 5215475aeb76d6761ef1873691cd00d0650939c1 Mon Sep 17 00:00:00 2001 From: zombieJ Date: Mon, 14 Aug 2017 12:50:20 +0800 Subject: [PATCH 2/4] clean up --- .../shared/fiber/ReactFiberUpdateQueue.js | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/renderers/shared/fiber/ReactFiberUpdateQueue.js b/src/renderers/shared/fiber/ReactFiberUpdateQueue.js index cc99283ca87c9..e3f1cc9265fac 100644 --- a/src/renderers/shared/fiber/ReactFiberUpdateQueue.js +++ b/src/renderers/shared/fiber/ReactFiberUpdateQueue.js @@ -163,19 +163,14 @@ function findInsertionPosition(queue, update): Update | null { function ensureUpdateQueues(fiber: Fiber) { const alternateFiber = fiber.alternate; - let queue1 = fiber.updateQueue; - if (queue1 === null) { - queue1 = fiber.updateQueue = createUpdateQueue(); + if (fiber.updateQueue === null) { + fiber.updateQueue = createUpdateQueue(); } - let queue2; if (alternateFiber !== null) { - queue2 = alternateFiber.updateQueue; - if (queue2 === null) { - queue2 = alternateFiber.updateQueue = createUpdateQueue(); + if (alternateFiber.updateQueue === null) { + alternateFiber.updateQueue = createUpdateQueue(); } - } else { - queue2 = null; } } @@ -369,9 +364,7 @@ function addTopLevelUpdate( // TODO: Redesign the top-level mount/update/unmount API to avoid this // special case. const queue1: UpdateQueue | null = fiber.updateQueue; - const queue2: UpdateQueue | null = fiber.alternate && - fiber.alternate.updateQueue !== queue1 - ? fiber.alternate.updateQueue + const queue2: UpdateQueue | null = fiber.alternate ? fiber.alternate.updateQueue : null; // Drop all updates that are lower-priority, so that the tree is not From 4690e50c7d1d51761e5d128b9c446f0f2c1493e8 Mon Sep 17 00:00:00 2001 From: zombieJ Date: Mon, 14 Aug 2017 13:31:09 +0800 Subject: [PATCH 3/4] prettier code --- src/renderers/shared/fiber/ReactFiberUpdateQueue.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/renderers/shared/fiber/ReactFiberUpdateQueue.js b/src/renderers/shared/fiber/ReactFiberUpdateQueue.js index e3f1cc9265fac..657673ddd5f76 100644 --- a/src/renderers/shared/fiber/ReactFiberUpdateQueue.js +++ b/src/renderers/shared/fiber/ReactFiberUpdateQueue.js @@ -364,7 +364,8 @@ function addTopLevelUpdate( // TODO: Redesign the top-level mount/update/unmount API to avoid this // special case. const queue1: UpdateQueue | null = fiber.updateQueue; - const queue2: UpdateQueue | null = fiber.alternate ? fiber.alternate.updateQueue + const queue2: UpdateQueue | null = fiber.alternate + ? fiber.alternate.updateQueue : null; // Drop all updates that are lower-priority, so that the tree is not From 787edfdeb752bcbdb6b879fe48216a289b4e604d Mon Sep 17 00:00:00 2001 From: zombieJ Date: Mon, 28 Aug 2017 14:48:51 +0800 Subject: [PATCH 4/4] Use module-level varibles --- .../shared/fiber/ReactFiberUpdateQueue.js | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/renderers/shared/fiber/ReactFiberUpdateQueue.js b/src/renderers/shared/fiber/ReactFiberUpdateQueue.js index 657673ddd5f76..0aed19f69ae05 100644 --- a/src/renderers/shared/fiber/ReactFiberUpdateQueue.js +++ b/src/renderers/shared/fiber/ReactFiberUpdateQueue.js @@ -68,6 +68,9 @@ export type UpdateQueue = { isProcessing?: boolean, }; +let _queue1; +let _queue2; + function comparePriority(a: PriorityLevel, b: PriorityLevel): number { // When comparing update priorities, treat sync and Task work as equal. // TODO: Could we avoid the need for this by always coercing sync priority @@ -163,15 +166,24 @@ function findInsertionPosition(queue, update): Update | null { function ensureUpdateQueues(fiber: Fiber) { const alternateFiber = fiber.alternate; - if (fiber.updateQueue === null) { - fiber.updateQueue = createUpdateQueue(); + let queue1 = fiber.updateQueue; + if (queue1 === null) { + queue1 = fiber.updateQueue = createUpdateQueue(); } + let queue2; if (alternateFiber !== null) { - if (alternateFiber.updateQueue === null) { - alternateFiber.updateQueue = createUpdateQueue(); + queue2 = alternateFiber.updateQueue; + if (queue2 === null) { + queue2 = alternateFiber.updateQueue = createUpdateQueue(); } + } else { + queue2 = null; } + + _queue1 = queue1; + // Return null if there is no alternate queue, or if its queue is the same. + _queue2 = queue2 !== queue1 ? queue2 : null; } // The work-in-progress queue is a subset of the current queue (if it exists). @@ -206,11 +218,8 @@ function ensureUpdateQueues(fiber: Fiber) { function insertUpdate(fiber: Fiber, update: Update): Update | null { // We'll have at least one and at most two distinct update queues. ensureUpdateQueues(fiber); - const queue1: UpdateQueue = (fiber.updateQueue: any); - const queue2: UpdateQueue | null = fiber.alternate && - fiber.alternate.updateQueue !== queue1 - ? fiber.alternate.updateQueue - : null; + const queue1 = _queue1; + const queue2 = _queue2; // Warn if an update is scheduled from inside an updater function. if (__DEV__) { @@ -349,7 +358,7 @@ function addTopLevelUpdate( ): void { const isTopLevelUnmount = partialState.element === null; - const update: Update = { + const update = { priorityLevel, partialState, callback, @@ -363,10 +372,8 @@ function addTopLevelUpdate( if (isTopLevelUnmount) { // TODO: Redesign the top-level mount/update/unmount API to avoid this // special case. - const queue1: UpdateQueue | null = fiber.updateQueue; - const queue2: UpdateQueue | null = fiber.alternate - ? fiber.alternate.updateQueue - : null; + const queue1 = _queue1; + const queue2 = _queue2; // Drop all updates that are lower-priority, so that the tree is not // remounted. We need to do this for both queues.