@samitouri / QOS-React / commits / d218158d22

Clean up

Use a union type for the head of StateQueue.

Andrew Clark committed Jul 26, 2016 at 10:28 UTC d218158d229ee505a2901184fb2602e08c5645c2
3 files changed +32 -27
src/renderers/shared/fiber/ReactFiberBeginWork.js
+9 -14
@@ -119,20 +119,18 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>, getSchedu
119 fiber.stateQueue = stateQueue;
120 // Schedule update on the alternate as well, since we don't know which tree
121 // is current.
122 - // $FlowFixMe: Intersection issue. Don't know why it's only happening here.
123 - const { alternate } = fiber;
124 - if (alternate !== null) {
125 - alternate.stateQueue = stateQueue;
122 + if (fiber.alternate !== null) {
123 + fiber.alternate.stateQueue = stateQueue;
124 }
125 while (true) {
126 if (fiber.pendingWorkPriority === NoWork ||
127 fiber.pendingWorkPriority >= priorityLevel) {
128 fiber.pendingWorkPriority = priorityLevel;
129 }
132 - if (alternate !== null) {
133 - if (alternate.pendingWorkPriority === NoWork ||
134 - alternate.pendingWorkPriority >= priorityLevel) {
135 - alternate.pendingWorkPriority = priorityLevel;
130 + if (fiber.alternate !== null) {
131 + if (fiber.alternate.pendingWorkPriority === NoWork ||
132 + fiber.alternate.pendingWorkPriority >= priorityLevel) {
133 + fiber.alternate.pendingWorkPriority = priorityLevel;
134 }
135 }
136 // Duck type root
@@ -169,12 +167,9 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>, getSchedu
167 }
168 // Compute the state using the memoized state and the pending state queue.
169 var stateQueue = workInProgress.stateQueue;
172 - var state;
173 - if (!current) {
174 - state = mergeStateQueue(null, props, stateQueue);
175 - } else {
176 - state = mergeStateQueue(current.memoizedState, props, stateQueue);
177 - }
170 + var state = current ?
171 + mergeStateQueue(stateQueue, current.memoizedState, props) :
172 + mergeStateQueue(stateQueue, null, props);
173
174 var instance = workInProgress.stateNode;
175 if (!instance) {
src/renderers/shared/fiber/ReactFiberStateQueue.js
+22 -12
@@ -12,23 +12,32 @@
12
13 'use strict';
14
15 -export type StateQueue = {
15 +type StateQueueNode = {
16 partialState: any,
17 - next: StateQueue | null,
18 - tail: StateQueue | null
17 + callback: ?Function,
18 + next: ?StateQueueNode,
19 +};
20 +
21 +export type StateQueue = StateQueueNode & {
22 + tail: ?StateQueueNode
23 };
24
25 exports.createStateQueue = function(partialState : mixed) : StateQueue {
26 return {
27 partialState,
28 + callback: null,
29 next: null,
30 tail: null,
31 };
32 };
33
29 -exports.addToQueue = function(queue : StateQueue, partialState : mixed): StateQueue {
30 - const node = exports.createStateQueue(partialState);
31 - if (queue.tail === null) {
34 +exports.addToQueue = function(queue : StateQueue, partialState : mixed) : StateQueue {
35 + const node = {
36 + partialState,
37 + callback: null,
38 + next: null,
39 + };
40 + if (!queue.tail) {
41 queue.next = node;
42 } else {
43 queue.tail.next = node;
@@ -37,16 +46,17 @@ exports.addToQueue = function(queue : StateQueue, partialState : mixed): StateQu
46 return queue;
47 };
48
40 -exports.mergeStateQueue = function(prevState : any, props : any, queue : ?StateQueue) : any {
41 - if (!queue) {
49 +exports.mergeStateQueue = function(queue : ?StateQueue, prevState : any, props : any) : any {
50 + let node : ?StateQueueNode = queue;
51 + if (!node) {
52 return prevState;
53 }
54 let state = Object.assign({}, prevState);
55 do {
46 - const partialState = typeof queue.partialState === 'function' ?
47 - queue.partialState(state, props) :
48 - queue.partialState;
56 + const partialState = typeof node.partialState === 'function' ?
57 + node.partialState(state, props) :
58 + node.partialState;
59 state = Object.assign(state, partialState);
50 - } while (queue = queue.next);
60 + } while (node = node.next);
61 return state;
62 };
src/renderers/shared/fiber/__tests__/ReactIncremental-test.js
+1 -1
@@ -648,8 +648,8 @@ describe('ReactIncremental', () => {
648 instance.setState(updater);
649 ReactNoop.flush();
650 expect(instance.state.num).toEqual(2);
651 - ReactNoop.render(<Foo multiplier={3} />);
651 instance.setState(updater);
652 + ReactNoop.render(<Foo multiplier={3} />);
653 ReactNoop.flush();
654 expect(instance.state.num).toEqual(6);
655 });