Clean up
Rather than bubble up both trees, bubble up once and assign to the alternate at each level. Extract logic for adding to the queue to the StateQueue module.
Andrew Clark committed
Jul 25, 2016 at 09:27 UTC
909caccebd7d17851b28d343169c2b5a6d2819f8
2 files changed
+20
-16
src/renderers/shared/fiber/ReactFiberBeginWork.js
+14
-14
@@ -117,11 +117,24 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>, getSchedu
117
function scheduleUpdate(fiber: Fiber, stateQueue: StateQueue, priorityLevel : PriorityLevel): void {
118
const { scheduleLowPriWork } = getScheduler();
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;
126
+ }
127
while (true) {
128
if (fiber.pendingWorkPriority === NoWork ||
129
fiber.pendingWorkPriority >= priorityLevel) {
130
fiber.pendingWorkPriority = priorityLevel;
131
}
132
+ if (alternate !== null) {
133
+ if (alternate.pendingWorkPriority === NoWork ||
134
+ alternate.pendingWorkPriority >= priorityLevel) {
135
+ alternate.pendingWorkPriority = priorityLevel;
136
+ }
137
+ }
138
// Duck type root
139
if (fiber.stateNode && fiber.stateNode.containerInfo) {
140
const root : FiberRoot = (fiber.stateNode : any);
@@ -139,21 +152,8 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>, getSchedu
152
const updater = {
153
enqueueSetState(instance, partialState) {
154
const fiber = instance._fiber;
142
- let stateQueue = fiber.stateQueue;
143
-
144
- // Append to pending state queue
145
- if (stateQueue === null) {
146
- stateQueue = createStateQueue(partialState);
147
- } else {
148
- addToQueue(stateQueue, partialState);
149
- }
150
-
151
- // Must schedule an update on both alternates, because we don't know tree
152
- // is current.
155
+ const stateQueue = addToQueue(fiber.stateQueue, partialState);
156
scheduleUpdate(fiber, stateQueue, LowPriority);
154
- if (fiber.alternate) {
155
- scheduleUpdate(fiber.alternate, stateQueue, LowPriority);
156
- }
157
},
158
};
159
src/renderers/shared/fiber/ReactFiberStateQueue.js
+6
-2
@@ -26,15 +26,19 @@ exports.createStateQueue = function(partialState : mixed) : StateQueue {
26
};
27
};
28
29
-exports.addToQueue = function(queue : StateQueue, partialState : mixed) {
29
+exports.addToQueue = function(queue : StateQueue, partialState : mixed): StateQueue {
30
const node = exports.createStateQueue(partialState);
31
+ if (queue === null) {
32
+ return node;
33
+ }
34
if (queue.tail === null) {
35
queue.next = node;
36
} else {
37
queue.tail.next = node;
38
}
39
queue.tail = node;
37
-}
40
+ return queue;
41
+};
42
43
exports.mergeStateQueue = function(prevState : any, props : any, queue : StateQueue) : any {
44
if (queue === null) {