From d218158d229ee505a2901184fb2602e08c5645c2 Mon Sep 17 00:00:00 2001 From: Andrew Clark Date: Tue, 26 Jul 2016 10:28:45 -0700 Subject: [PATCH] Clean up Use a union type for the head of StateQueue. --- .../shared/fiber/ReactFiberBeginWork.js | 23 +++++-------- .../shared/fiber/ReactFiberStateQueue.js | 34 ++++++++++++------- .../fiber/__tests__/ReactIncremental-test.js | 2 +- 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/renderers/shared/fiber/ReactFiberBeginWork.js b/src/renderers/shared/fiber/ReactFiberBeginWork.js index a8e97032d2..c1ad706b5b 100644 --- a/src/renderers/shared/fiber/ReactFiberBeginWork.js +++ b/src/renderers/shared/fiber/ReactFiberBeginWork.js @@ -119,20 +119,18 @@ module.exports = function(config : HostConfig, getSchedu fiber.stateQueue = stateQueue; // Schedule update on the alternate as well, since we don't know which tree // is current. - // $FlowFixMe: Intersection issue. Don't know why it's only happening here. - const { alternate } = fiber; - if (alternate !== null) { - alternate.stateQueue = stateQueue; + if (fiber.alternate !== null) { + fiber.alternate.stateQueue = stateQueue; } while (true) { if (fiber.pendingWorkPriority === NoWork || fiber.pendingWorkPriority >= priorityLevel) { fiber.pendingWorkPriority = priorityLevel; } - if (alternate !== null) { - if (alternate.pendingWorkPriority === NoWork || - alternate.pendingWorkPriority >= priorityLevel) { - alternate.pendingWorkPriority = priorityLevel; + if (fiber.alternate !== null) { + if (fiber.alternate.pendingWorkPriority === NoWork || + fiber.alternate.pendingWorkPriority >= priorityLevel) { + fiber.alternate.pendingWorkPriority = priorityLevel; } } // Duck type root @@ -169,12 +167,9 @@ module.exports = function(config : HostConfig, getSchedu } // Compute the state using the memoized state and the pending state queue. var stateQueue = workInProgress.stateQueue; - var state; - if (!current) { - state = mergeStateQueue(null, props, stateQueue); - } else { - state = mergeStateQueue(current.memoizedState, props, stateQueue); - } + var state = current ? + mergeStateQueue(stateQueue, current.memoizedState, props) : + mergeStateQueue(stateQueue, null, props); var instance = workInProgress.stateNode; if (!instance) { diff --git a/src/renderers/shared/fiber/ReactFiberStateQueue.js b/src/renderers/shared/fiber/ReactFiberStateQueue.js index 5932002810..beceeb07dd 100644 --- a/src/renderers/shared/fiber/ReactFiberStateQueue.js +++ b/src/renderers/shared/fiber/ReactFiberStateQueue.js @@ -12,23 +12,32 @@ 'use strict'; -export type StateQueue = { +type StateQueueNode = { partialState: any, - next: StateQueue | null, - tail: StateQueue | null + callback: ?Function, + next: ?StateQueueNode, +}; + +export type StateQueue = StateQueueNode & { + tail: ?StateQueueNode }; exports.createStateQueue = function(partialState : mixed) : StateQueue { return { partialState, + callback: null, next: null, tail: null, }; }; -exports.addToQueue = function(queue : StateQueue, partialState : mixed): StateQueue { - const node = exports.createStateQueue(partialState); - if (queue.tail === null) { +exports.addToQueue = function(queue : StateQueue, partialState : mixed) : StateQueue { + const node = { + partialState, + callback: null, + next: null, + }; + if (!queue.tail) { queue.next = node; } else { queue.tail.next = node; @@ -37,16 +46,17 @@ exports.addToQueue = function(queue : StateQueue, partialState : mixed): StateQu return queue; }; -exports.mergeStateQueue = function(prevState : any, props : any, queue : ?StateQueue) : any { - if (!queue) { +exports.mergeStateQueue = function(queue : ?StateQueue, prevState : any, props : any) : any { + let node : ?StateQueueNode = queue; + if (!node) { return prevState; } let state = Object.assign({}, prevState); do { - const partialState = typeof queue.partialState === 'function' ? - queue.partialState(state, props) : - queue.partialState; + const partialState = typeof node.partialState === 'function' ? + node.partialState(state, props) : + node.partialState; state = Object.assign(state, partialState); - } while (queue = queue.next); + } while (node = node.next); return state; }; diff --git a/src/renderers/shared/fiber/__tests__/ReactIncremental-test.js b/src/renderers/shared/fiber/__tests__/ReactIncremental-test.js index e7dccc7603..5179acec51 100644 --- a/src/renderers/shared/fiber/__tests__/ReactIncremental-test.js +++ b/src/renderers/shared/fiber/__tests__/ReactIncremental-test.js @@ -648,8 +648,8 @@ describe('ReactIncremental', () => { instance.setState(updater); ReactNoop.flush(); expect(instance.state.num).toEqual(2); - ReactNoop.render(); instance.setState(updater); + ReactNoop.render(); ReactNoop.flush(); expect(instance.state.num).toEqual(6); });