Rename stateQueue -> updateQueue
Also cleans up some types.
Andrew Clark committed
Aug 4, 2016 at 14:27 UTC
c6db7f73bebd135dcb2dce2eaebed64c54627727
4 files changed
+54
-47
src/renderers/shared/fiber/ReactFiber.js
+5
-5
@@ -15,7 +15,7 @@
15
import type { ReactCoroutine, ReactYield } from 'ReactCoroutine';
16
import type { TypeOfWork } from 'ReactTypeOfWork';
17
import type { PriorityLevel } from 'ReactPriorityLevel';
18
-import type { StateQueue } from 'ReactFiberStateQueue';
18
+import type { UpdateQueue } from 'ReactFiberUpdateQueue';
19
20
var ReactTypeOfWork = require('ReactTypeOfWork');
21
var {
@@ -78,7 +78,7 @@ export type Fiber = Instance & {
78
// TODO: I think that there is a way to merge pendingProps and memoizedProps.
79
memoizedProps: any, // The props used to create the output.
80
// A queue of local state updates.
81
- stateQueue: ?StateQueue,
81
+ updateQueue: ?UpdateQueue,
82
// The state used to create the output. This is a full state object.
83
memoizedState: any,
84
// Output is the return value of this fiber, or a linked list of return values
@@ -156,7 +156,7 @@ var createFiber = function(tag : TypeOfWork, key : null | string) : Fiber {
156
157
pendingProps: null,
158
memoizedProps: null,
159
- stateQueue: null,
159
+ updateQueue: null,
160
memoizedState: null,
161
output: null,
162
@@ -199,7 +199,7 @@ exports.cloneFiber = function(fiber : Fiber, priorityLevel : PriorityLevel) : Fi
199
alt.sibling = fiber.sibling; // This should always be overridden. TODO: null
200
alt.ref = fiber.ref;
201
alt.pendingProps = fiber.pendingProps; // TODO: Pass as argument.
202
- alt.stateQueue = fiber.stateQueue;
202
+ alt.updateQueue = fiber.updateQueue;
203
alt.pendingWorkPriority = priorityLevel;
204
205
alt.child = fiber.child;
@@ -225,7 +225,7 @@ exports.cloneFiber = function(fiber : Fiber, priorityLevel : PriorityLevel) : Fi
225
// pendingProps is here for symmetry but is unnecessary in practice for now.
226
// TODO: Pass in the new pendingProps as an argument maybe?
227
alt.pendingProps = fiber.pendingProps;
228
- alt.stateQueue = fiber.stateQueue;
228
+ alt.updateQueue = fiber.updateQueue;
229
alt.pendingWorkPriority = priorityLevel;
230
231
alt.memoizedProps = fiber.memoizedProps;
src/renderers/shared/fiber/ReactFiberBeginWork.js
+20
-20
@@ -18,7 +18,7 @@ import type { FiberRoot } from 'ReactFiberRoot';
18
import type { HostConfig } from 'ReactFiberReconciler';
19
import type { Scheduler } from 'ReactFiberScheduler';
20
import type { PriorityLevel } from 'ReactPriorityLevel';
21
-import type { StateQueue } from 'ReactFiberStateQueue';
21
+import type { UpdateQueue } from 'ReactFiberUpdateQueue';
22
23
var {
24
reconcileChildFibers,
@@ -42,10 +42,10 @@ var {
42
OffscreenPriority,
43
} = require('ReactPriorityLevel');
44
var {
45
- createStateQueue,
45
+ createUpdateQueue,
46
addToQueue,
47
- mergeStateQueue,
48
-} = require('ReactFiberStateQueue');
47
+ mergeUpdateQueue,
48
+} = require('ReactFiberUpdateQueue');
49
var ReactInstanceMap = require('ReactInstanceMap');
50
51
module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>, getScheduler : () => Scheduler) {
@@ -115,13 +115,13 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>, getSchedu
115
return workInProgress.child;
116
}
117
118
- function scheduleUpdate(fiber: Fiber, stateQueue: StateQueue, priorityLevel : PriorityLevel): void {
118
+ function scheduleUpdate(fiber: Fiber, updateQueue: UpdateQueue, priorityLevel : PriorityLevel): void {
119
const { scheduleLowPriWork } = getScheduler();
120
- fiber.stateQueue = stateQueue;
120
+ fiber.updateQueue = updateQueue;
121
// Schedule update on the alternate as well, since we don't know which tree
122
// is current.
123
if (fiber.alternate !== null) {
124
- fiber.alternate.stateQueue = stateQueue;
124
+ fiber.alternate.updateQueue = updateQueue;
125
}
126
while (true) {
127
if (fiber.pendingWorkPriority === NoWork ||
@@ -151,10 +151,10 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>, getSchedu
151
const updater = {
152
enqueueSetState(instance, partialState) {
153
const fiber = ReactInstanceMap.get(instance);
154
- const stateQueue = fiber.stateQueue ?
155
- addToQueue(fiber.stateQueue, partialState) :
156
- createStateQueue(partialState);
157
- scheduleUpdate(fiber, stateQueue, LowPriority);
154
+ const updateQueue = fiber.updateQueue ?
155
+ addToQueue(fiber.updateQueue, partialState) :
156
+ createUpdateQueue(partialState);
157
+ scheduleUpdate(fiber, updateQueue, LowPriority);
158
},
159
};
160
@@ -166,21 +166,22 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>, getSchedu
166
if (!props && current) {
167
props = current.memoizedProps;
168
}
169
- // Compute the state using the memoized state and the pending state queue.
170
- var stateQueue = workInProgress.stateQueue;
171
- var state = current ?
172
- mergeStateQueue(stateQueue, current.memoizedState, props) :
173
- mergeStateQueue(stateQueue, null, props);
169
+ // Compute the state using the memoized state and the update queue.
170
+ var updateQueue = workInProgress.updateQueue;
171
+ var previousState = current ? current.memoizedState : null;
172
+ var state = updateQueue ?
173
+ mergeUpdateQueue(updateQueue, previousState, props) :
174
+ previousState;
175
176
var instance = workInProgress.stateNode;
177
if (!instance) {
178
var ctor = workInProgress.type;
179
workInProgress.stateNode = instance = new ctor(props);
180
state = instance.state || null;
180
- // The initial state must be added to the pending state queue in case
181
+ // The initial state must be added to the update queue in case
182
// setState is called before the initial render.
183
if (state !== null) {
183
- workInProgress.stateQueue = createStateQueue(state);
184
+ workInProgress.updateQueue = createUpdateQueue(state);
185
}
186
// The instance needs access to the fiber so that it can schedule updates
187
ReactInstanceMap.set(instance, workInProgress);
@@ -331,8 +332,7 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>, getSchedu
332
workInProgress.memoizedProps !== null &&
333
workInProgress.pendingProps === workInProgress.memoizedProps
334
)) &&
334
- workInProgress.stateQueue === null
335
- ) {
335
+ workInProgress.updateQueue === null) {
336
return bailoutOnAlreadyFinishedWork(current, workInProgress);
337
}
338
src/renderers/shared/fiber/ReactFiberScheduler.js
+2
-2
@@ -143,9 +143,9 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>) {
143
// The work is now done. We don't need this anymore. This flags
144
// to the system not to redo any work here.
145
workInProgress.pendingProps = null;
146
- workInProgress.stateQueue = null;
146
+ workInProgress.updateQueue = null;
147
if (current) {
148
- current.stateQueue = null;
148
+ current.updateQueue = null;
149
}
150
151
const returnFiber = workInProgress.return;
src/renderers/shared/fiber/ReactFiberUpdateQueue.js
renamed
+27
-20
@@ -6,53 +6,59 @@
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
- * @providesModule ReactFiberStateQueue
9
+ * @providesModule ReactFiberUpdateQueue
10
* @flow
11
*/
12
13
'use strict';
14
15
-type StateQueueNode = {
15
+type UpdateQueueNode = {
16
partialState: any,
17
callback: ?Function,
18
- next: ?StateQueueNode,
18
+ next: ?UpdateQueueNode,
19
};
20
21
-export type StateQueue = StateQueueNode & {
22
- tail: ?StateQueueNode
21
+export type UpdateQueue = UpdateQueueNode & {
22
+ tail: UpdateQueueNode
23
};
24
25
-exports.createStateQueue = function(partialState : mixed) : StateQueue {
26
- return {
25
+exports.createUpdateQueue = function(partialState : mixed) : UpdateQueue {
26
+ const queue = {
27
partialState,
28
callback: null,
29
next: null,
30
- tail: null,
30
+ tail: (null : any),
31
};
32
+ queue.tail = queue;
33
+ return queue;
34
};
35
34
-exports.addToQueue = function(queue : StateQueue, partialState : mixed) : StateQueue {
36
+exports.addToQueue = function(queue : UpdateQueue, partialState : mixed) : UpdateQueue {
37
const node = {
38
partialState,
39
callback: null,
40
next: null,
41
};
40
- if (!queue.tail) {
41
- queue.next = node;
42
- } else {
43
- queue.tail.next = node;
44
- }
42
+ queue.tail.next = node;
43
queue.tail = node;
44
return queue;
45
};
46
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;
55
}
56
+};
57
+
58
+exports.mergeUpdateQueue = function(queue : UpdateQueue, prevState : any, props : any) : any {
59
+ let node : ?UpdateQueueNode = queue;
60
let state = Object.assign({}, prevState);
55
- do {
61
+ while (node) {
62
let partialState;
63
if (typeof node.partialState === 'function') {
64
const updateFn = node.partialState;
@@ -61,6 +67,7 @@ exports.mergeStateQueue = function(queue : ?StateQueue, prevState : any, props :
67
partialState = node.partialState;
68
}
69
state = Object.assign(state, partialState);
64
- } while (node = node.next);
70
+ node = node.next;
71
+ }
72
return state;
73
};