Skip to content

Commit f167826

Browse files
committed
Rename stateQueue -> updateQueue
Also cleans up some types.
1 parent 8b18725 commit f167826

File tree

5 files changed

+61
-53
lines changed

5 files changed

+61
-53
lines changed

src/renderers/shared/fiber/ReactFiber.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import type { ReactCoroutine, ReactYield } from 'ReactCoroutine';
1616
import type { TypeOfWork } from 'ReactTypeOfWork';
1717
import type { PriorityLevel } from 'ReactPriorityLevel';
18-
import type { StateQueue } from 'ReactFiberStateQueue';
18+
import type { UpdateQueue } from 'ReactFiberUpdateQueue';
1919

2020
var ReactTypeOfWork = require('ReactTypeOfWork');
2121
var {
@@ -80,7 +80,7 @@ export type Fiber = Instance & {
8080
// TODO: I think that there is a way to merge pendingProps and memoizedProps.
8181
memoizedProps: any, // The props used to create the output.
8282
// A queue of local state updates.
83-
stateQueue: ?StateQueue,
83+
updateQueue: ?UpdateQueue,
8484
// The state used to create the output. This is a full state object.
8585
memoizedState: any,
8686
// Output is the return value of this fiber, or a linked list of return values
@@ -139,7 +139,7 @@ var createFiber = function(tag : TypeOfWork, key : null | string) : Fiber {
139139

140140
pendingProps: null,
141141
memoizedProps: null,
142-
stateQueue: null,
142+
updateQueue: null,
143143
memoizedState: null,
144144
output: null,
145145

@@ -175,7 +175,7 @@ exports.cloneFiber = function(fiber : Fiber, priorityLevel : PriorityLevel) : Fi
175175
alt.sibling = fiber.sibling;
176176
alt.ref = alt.ref;
177177
alt.pendingProps = fiber.pendingProps;
178-
alt.stateQueue = fiber.stateQueue;
178+
alt.updateQueue = fiber.updateQueue;
179179
alt.pendingWorkPriority = priorityLevel;
180180

181181
// Whenever we clone, we do so to get a new work in progress.
@@ -197,7 +197,7 @@ exports.cloneFiber = function(fiber : Fiber, priorityLevel : PriorityLevel) : Fi
197197
alt.ref = alt.ref;
198198
// pendingProps is here for symmetry but is unnecessary in practice for now.
199199
alt.pendingProps = fiber.pendingProps;
200-
alt.stateQueue = fiber.stateQueue;
200+
alt.updateQueue = fiber.updateQueue;
201201
alt.pendingWorkPriority = priorityLevel;
202202

203203
alt.alternate = fiber;

src/renderers/shared/fiber/ReactFiberBeginWork.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type { FiberRoot } from 'ReactFiberRoot';
1818
import type { HostConfig } from 'ReactFiberReconciler';
1919
import type { Scheduler } from 'ReactFiberScheduler';
2020
import type { PriorityLevel } from 'ReactPriorityLevel';
21-
import type { StateQueue } from 'ReactFiberStateQueue';
21+
import type { UpdateQueue } from 'ReactFiberUpdateQueue';
2222

2323
var {
2424
reconcileChildFibers,
@@ -42,10 +42,10 @@ var {
4242
} = require('ReactPriorityLevel');
4343
var { findNextUnitOfWorkAtPriority } = require('ReactFiberPendingWork');
4444
var {
45-
createStateQueue,
45+
createUpdateQueue,
4646
addToQueue,
47-
mergeStateQueue,
48-
} = require('ReactFiberStateQueue');
47+
mergeUpdateQueue,
48+
} = require('ReactFiberUpdateQueue');
4949
var ReactInstanceMap = require('ReactInstanceMap');
5050

5151
module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>, getScheduler : () => Scheduler) {
@@ -89,15 +89,15 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>, getSchedu
8989
workInProgress.pendingWorkPriority = NoWork;
9090
}
9191

92-
function scheduleUpdate(fiber: Fiber, stateQueue: StateQueue, priorityLevel : PriorityLevel): void {
92+
function scheduleUpdate(fiber: Fiber, updateQueue: UpdateQueue, priorityLevel : PriorityLevel): void {
9393
const { scheduleLowPriWork } = getScheduler();
94-
fiber.stateQueue = stateQueue;
94+
fiber.updateQueue = updateQueue;
9595
// Schedule update on the alternate as well, since we don't know which tree
9696
// is current.
9797
// $FlowFixMe: Intersection issue. Don't know why it's only happening here.
9898
const { alternate } = fiber;
9999
if (alternate !== null) {
100-
alternate.stateQueue = stateQueue;
100+
alternate.updateQueue = updateQueue;
101101
}
102102
while (true) {
103103
if (fiber.pendingWorkPriority === NoWork ||
@@ -127,10 +127,10 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>, getSchedu
127127
const updater = {
128128
enqueueSetState(instance, partialState) {
129129
const fiber = ReactInstanceMap.get(instance);
130-
const stateQueue = fiber.stateQueue ?
131-
addToQueue(fiber.stateQueue, partialState) :
132-
createStateQueue(partialState);
133-
scheduleUpdate(fiber, stateQueue, LowPriority);
130+
const updateQueue = fiber.updateQueue ?
131+
addToQueue(fiber.updateQueue, partialState) :
132+
createUpdateQueue(partialState);
133+
scheduleUpdate(fiber, updateQueue, LowPriority);
134134
},
135135
};
136136

@@ -142,21 +142,22 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>, getSchedu
142142
if (!props && current) {
143143
props = current.memoizedProps;
144144
}
145-
// Compute the state using the memoized state and the pending state queue.
146-
var stateQueue = workInProgress.stateQueue;
147-
var state = current ?
148-
mergeStateQueue(stateQueue, current.memoizedState, props) :
149-
mergeStateQueue(stateQueue, null, props);
145+
// Compute the state using the memoized state and the update queue.
146+
var updateQueue = workInProgress.updateQueue;
147+
var previousState = current ? current.memoizedState : null;
148+
var state = updateQueue ?
149+
mergeUpdateQueue(updateQueue, previousState, props) :
150+
previousState;
150151

151152
var instance = workInProgress.stateNode;
152153
if (!instance) {
153154
var ctor = workInProgress.type;
154155
workInProgress.stateNode = instance = new ctor(props);
155156
state = instance.state || null;
156-
// The initial state must be added to the pending state queue in case
157+
// The initial state must be added to the update queue in case
157158
// setState is called before the initial render.
158159
if (state !== null) {
159-
workInProgress.stateQueue = createStateQueue(state);
160+
workInProgress.updateQueue = createUpdateQueue(state);
160161
}
161162
// The instance needs access to the fiber so that it can schedule updates
162163
ReactInstanceMap.set(instance, workInProgress);
@@ -290,7 +291,7 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>, getSchedu
290291
workInProgress.output = current.output;
291292
const priorityLevel = workInProgress.pendingWorkPriority;
292293
workInProgress.pendingProps = null;
293-
workInProgress.stateQueue = current.stateQueue = null;
294+
workInProgress.updateQueue = current.updateQueue = null;
294295
workInProgress.pendingWorkPriority = NoWork;
295296
workInProgress.stateNode = current.stateNode;
296297
workInProgress.childInProgress = current.childInProgress;
@@ -320,9 +321,9 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>, getSchedu
320321
// looking for. In that case, we should be able to just bail out.
321322
const priorityLevel = workInProgress.pendingWorkPriority;
322323
workInProgress.pendingProps = null;
323-
workInProgress.stateQueue = null;
324+
workInProgress.updateQueue = null;
324325
if (workInProgress.alternate) {
325-
workInProgress.alternate.stateQueue = null;
326+
workInProgress.alternate.updateQueue = null;
326327
}
327328
workInProgress.pendingWorkPriority = NoWork;
328329

@@ -365,14 +366,14 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>, getSchedu
365366
// progress.
366367
if (current &&
367368
workInProgress.pendingProps === current.memoizedProps &&
368-
workInProgress.stateQueue === null
369+
workInProgress.updateQueue === null
369370
) {
370371
return bailoutOnCurrent(current, workInProgress, null);
371372
}
372373

373374
if (!workInProgress.childInProgress &&
374375
workInProgress.pendingProps === workInProgress.memoizedProps &&
375-
workInProgress.stateQueue === null
376+
workInProgress.updateQueue === null
376377
) {
377378
return bailoutOnAlreadyFinishedWork(current, workInProgress);
378379
}

src/renderers/shared/fiber/ReactFiberPendingWork.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ exports.findNextUnitOfWorkAtPriority = function(currentRoot : Fiber, priorityLev
4040
if (current.pendingWorkPriority !== NoWork &&
4141
current.pendingWorkPriority <= priorityLevel) {
4242
// This node has work to do that fits our priority level criteria.
43-
if (current.pendingProps !== null || current.stateQueue !== null) {
43+
if (current.pendingProps !== null || current.updateQueue !== null) {
4444
// We found some work to do. We need to return the "work in progress"
4545
// of this node which will be the alternate.
4646
const workInProgress = current.alternate;
4747
if (!workInProgress) {
4848
throw new Error('Should have wip now');
4949
}
50-
// If current has stateQueue, it's already set on the workInProgress.
50+
// If current has updateQueue, it's already set on the workInProgress.
5151
workInProgress.pendingProps = current.pendingProps;
5252
return workInProgress;
5353
}

src/renderers/shared/fiber/ReactFiberScheduler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>) {
136136
// The work is now done. We don't need this anymore. This flags
137137
// to the system not to redo any work here.
138138
workInProgress.pendingProps = null;
139-
workInProgress.stateQueue = null;
139+
workInProgress.updateQueue = null;
140140
if (current) {
141-
current.stateQueue = null;
141+
current.updateQueue = null;
142142
}
143143

144144
const returnFiber = workInProgress.return;
@@ -206,7 +206,7 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>) {
206206

207207
function performUnitOfWork(workInProgress : Fiber) : ?Fiber {
208208
// Ignore work if there is nothing to do.
209-
if (workInProgress.pendingProps === null && workInProgress.stateQueue === null) {
209+
if (workInProgress.pendingProps === null && workInProgress.updateQueue === null) {
210210
return completeUnitOfWork(workInProgress);
211211
}
212212
// The current, flushed, state of this fiber is the alternate.

src/renderers/shared/fiber/ReactFiberStateQueue.js renamed to src/renderers/shared/fiber/ReactFiberUpdateQueue.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,59 @@
66
* LICENSE file in the root directory of this source tree. An additional grant
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*
9-
* @providesModule ReactFiberStateQueue
9+
* @providesModule ReactFiberUpdateQueue
1010
* @flow
1111
*/
1212

1313
'use strict';
1414

15-
type StateQueueNode = {
15+
type UpdateQueueNode = {
1616
partialState: any,
1717
callback: ?Function,
18-
next: ?StateQueueNode,
18+
next: ?UpdateQueueNode,
1919
};
2020

21-
export type StateQueue = StateQueueNode & {
22-
tail: ?StateQueueNode
21+
export type UpdateQueue = UpdateQueueNode & {
22+
tail: UpdateQueueNode
2323
};
2424

25-
exports.createStateQueue = function(partialState : mixed) : StateQueue {
26-
return {
25+
exports.createUpdateQueue = function(partialState : mixed) : UpdateQueue {
26+
const queue = {
2727
partialState,
2828
callback: null,
2929
next: null,
30-
tail: null,
30+
tail: (null : any),
3131
};
32+
queue.tail = queue;
33+
return queue;
3234
};
3335

34-
exports.addToQueue = function(queue : StateQueue, partialState : mixed) : StateQueue {
36+
exports.addToQueue = function(queue : UpdateQueue, partialState : mixed) : UpdateQueue {
3537
const node = {
3638
partialState,
3739
callback: null,
3840
next: null,
3941
};
40-
if (!queue.tail) {
41-
queue.next = node;
42-
} else {
43-
queue.tail.next = node;
44-
}
42+
queue.tail.next = node;
4543
queue.tail = node;
4644
return queue;
4745
};
4846

49-
exports.mergeStateQueue = function(queue : ?StateQueue, prevState : any, props : any) : any {
50-
let node : ?StateQueueNode = queue;
51-
if (!node) {
52-
return prevState;
47+
exports.callCallbacks = function(queue : UpdateQueue, partialState : mixed) {
48+
let node : ?UpdateQueueNode = queue;
49+
while (node) {
50+
if (node.callback) {
51+
const { callback } = node;
52+
callback();
53+
}
54+
node = node.next;
5355
}
56+
};
57+
58+
exports.mergeUpdateQueue = function(queue : UpdateQueue, prevState : any, props : any) : any {
59+
let node : ?UpdateQueueNode = queue;
5460
let state = Object.assign({}, prevState);
55-
do {
61+
while (node) {
5662
let partialState;
5763
if (typeof node.partialState === 'function') {
5864
const updateFn = node.partialState;
@@ -61,6 +67,7 @@ exports.mergeStateQueue = function(queue : ?StateQueue, prevState : any, props :
6167
partialState = node.partialState;
6268
}
6369
state = Object.assign(state, partialState);
64-
} while (node = node.next);
70+
node = node.next;
71+
}
6572
return state;
6673
};

0 commit comments

Comments
 (0)