Use a union type for the head of StateQueue.
This commit is contained in:
Andrew Clark
2016-09-13 15:26:47 -07:00
committed by Sebastian Markbage
parent 691e053650
commit d218158d22
3 changed files with 32 additions and 27 deletions
@@ -119,20 +119,18 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>, 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<T, P, I, C>(config : HostConfig<T, P, I, C>, 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) {
@@ -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;
};
@@ -648,8 +648,8 @@ describe('ReactIncremental', () => {
instance.setState(updater);
ReactNoop.flush();
expect(instance.state.num).toEqual(2);
ReactNoop.render(<Foo multiplier={3} />);
instance.setState(updater);
ReactNoop.render(<Foo multiplier={3} />);
ReactNoop.flush();
expect(instance.state.num).toEqual(6);
});