Use different default priority depending on the AsyncUpdate context flag
- A priority context of NoWork represents the default priority - Changed the signature of getPriorityContext to accept the fiber that is about to be updated.
Andrew Clark committed
Apr 17, 2017 at 12:39 UTC
6f8fec4b8015e1d95c177521ae9c2903713ceb16
4 files changed
+34
-16
src/renderers/shared/fiber/ReactFiberBeginWork.js
+1
-1
@@ -66,7 +66,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
66
config: HostConfig<T, P, I, TI, PI, C, CX, PL>,
67
hostContext: HostContext<C, CX>,
68
scheduleUpdate: (fiber: Fiber, priorityLevel: PriorityLevel) => void,
69
- getPriorityContext: () => PriorityLevel,
69
+ getPriorityContext: (fiber: Fiber) => PriorityLevel,
70
) {
71
const {
72
shouldSetTextContent,
src/renderers/shared/fiber/ReactFiberClassComponent.js
+8
-5
@@ -15,7 +15,10 @@
15
import type {Fiber} from 'ReactFiber';
16
import type {PriorityLevel} from 'ReactPriorityLevel';
17
18
-var {Update} = require('ReactTypeOfSideEffect');
18
+var {
19
+ Update,
20
+} = require('ReactTypeOfSideEffect');
21
+
22
var {
23
cacheContext,
24
getMaskedContext,
@@ -54,7 +57,7 @@ if (__DEV__) {
57
58
module.exports = function(
59
scheduleUpdate: (fiber: Fiber, priorityLevel: PriorityLevel) => void,
57
- getPriorityContext: () => PriorityLevel,
60
+ getPriorityContext: (fiber: Fiber) => PriorityLevel,
61
memoizeProps: (workInProgress: Fiber, props: any) => void,
62
memoizeState: (workInProgress: Fiber, state: any) => void,
63
) {
@@ -63,7 +66,7 @@ module.exports = function(
66
isMounted,
67
enqueueSetState(instance, partialState, callback) {
68
const fiber = ReactInstanceMap.get(instance);
66
- const priorityLevel = getPriorityContext();
69
+ const priorityLevel = getPriorityContext(fiber);
70
callback = callback === undefined ? null : callback;
71
if (__DEV__) {
72
warnOnInvalidCallback(callback, 'setState');
@@ -73,7 +76,7 @@ module.exports = function(
76
},
77
enqueueReplaceState(instance, state, callback) {
78
const fiber = ReactInstanceMap.get(instance);
76
- const priorityLevel = getPriorityContext();
79
+ const priorityLevel = getPriorityContext(fiber);
80
callback = callback === undefined ? null : callback;
81
if (__DEV__) {
82
warnOnInvalidCallback(callback, 'replaceState');
@@ -83,7 +86,7 @@ module.exports = function(
86
},
87
enqueueForceUpdate(instance, callback) {
88
const fiber = ReactInstanceMap.get(instance);
86
- const priorityLevel = getPriorityContext();
89
+ const priorityLevel = getPriorityContext(fiber);
90
callback = callback === undefined ? null : callback;
91
if (__DEV__) {
92
warnOnInvalidCallback(callback, 'forceUpdate');
src/renderers/shared/fiber/ReactFiberReconciler.js
+4
-2
@@ -17,7 +17,9 @@ import type {FiberRoot} from 'ReactFiberRoot';
17
import type {PriorityLevel} from 'ReactPriorityLevel';
18
import type {ReactNodeList} from 'ReactTypes';
19
20
-var {addTopLevelUpdate} = require('ReactFiberUpdateQueue');
20
+var {
21
+ addTopLevelUpdate,
22
+} = require('ReactFiberUpdateQueue');
23
24
var {
25
findCurrentUnmaskedContext,
@@ -178,7 +180,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
180
}
181
}
182
181
- const priorityLevel = getPriorityContext();
183
+ const priorityLevel = getPriorityContext(current);
184
const nextState = {element};
185
callback = callback === undefined ? null : callback;
186
if (__DEV__) {
src/renderers/shared/fiber/ReactFiberScheduler.js
+21
-8
@@ -60,6 +60,10 @@ var {
60
OffscreenPriority,
61
} = require('ReactPriorityLevel');
62
63
+var {
64
+ AsyncUpdates,
65
+} = require('ReactTypeOfContext');
66
+
67
var {
68
NoEffect,
69
Placement,
@@ -167,11 +171,11 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
171
resetAfterCommit,
172
} = config;
173
170
- // The priority level to use when scheduling an update.
171
- // TODO: Should we change this to an array? Might be less confusing.
172
- let priorityContext: PriorityLevel = useSyncScheduling
173
- ? SynchronousPriority
174
- : LowPriority;
174
+ // The priority level to use when scheduling an update. We use NoWork to
175
+ // represent the default priority.
176
+ // TODO: Should we change this to an array instead of using the call stack?
177
+ // Might be less confusing.
178
+ let priorityContext: PriorityLevel = NoWork;
179
180
// Keep track of this so we can reset the priority context if an error
181
// is thrown during reconciliation.
@@ -1340,16 +1344,25 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
1344
}
1345
}
1346
1343
- function getPriorityContext(): PriorityLevel {
1347
+ function getPriorityContext(fiber: Fiber): PriorityLevel {
1348
+ let priorityLevel = priorityContext;
1349
+ if (priorityLevel === NoWork) {
1350
+ if (!useSyncScheduling || fiber.contextTag & AsyncUpdates) {
1351
+ priorityLevel = LowPriority;
1352
+ } else {
1353
+ priorityLevel = SynchronousPriority;
1354
+ }
1355
+ }
1356
+
1357
// If we're in a batch, or if we're already performing work, downgrade sync
1358
// priority to task priority
1359
if (
1347
- priorityContext === SynchronousPriority &&
1360
+ priorityLevel === SynchronousPriority &&
1361
(isPerformingWork || isBatchingUpdates)
1362
) {
1363
return TaskPriority;
1364
}
1352
- return priorityContext;
1365
+ return priorityLevel;
1366
}
1367
1368
function scheduleErrorRecovery(fiber: Fiber) {