Skip to content

Commit 50b7091

Browse files
committed
Work-in-progress
1 parent b1a56ab commit 50b7091

19 files changed

+2425
-534
lines changed

packages/react-cache/src/LRU.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import * as Scheduler from 'scheduler';
1111

1212
// Intentionally not named imports because Rollup would
1313
// use dynamic dispatch for CommonJS interop named imports.
14-
const {unstable_scheduleCallback: scheduleCallback} = Scheduler;
14+
const {
15+
unstable_scheduleCallback: scheduleCallback,
16+
unstable_IdlePriority: IdlePriority,
17+
} = Scheduler;
1518

1619
type Entry<T> = {|
1720
value: T,
@@ -34,7 +37,7 @@ export function createLRU<T>(limit: number) {
3437
// The cache size exceeds the limit. Schedule a callback to delete the
3538
// least recently used entries.
3639
cleanUpIsScheduled = true;
37-
scheduleCallback(cleanUp);
40+
scheduleCallback(IdlePriority, cleanUp);
3841
}
3942
}
4043

packages/react-dom/src/__tests__/ReactDOMSuspensePlaceholder-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ let ReactDOM;
1414
let Suspense;
1515
let ReactCache;
1616
let ReactTestUtils;
17+
let Scheduler;
1718
let TextResource;
1819
let act;
1920

@@ -26,6 +27,7 @@ describe('ReactDOMSuspensePlaceholder', () => {
2627
ReactDOM = require('react-dom');
2728
ReactCache = require('react-cache');
2829
ReactTestUtils = require('react-dom/test-utils');
30+
Scheduler = require('scheduler');
2931
act = ReactTestUtils.act;
3032
Suspense = React.Suspense;
3133
container = document.createElement('div');
@@ -94,6 +96,8 @@ describe('ReactDOMSuspensePlaceholder', () => {
9496

9597
await advanceTimers(1000);
9698

99+
Scheduler.flushAll();
100+
97101
expect(divs[0].current.style.display).toEqual('');
98102
expect(divs[1].current.style.display).toEqual('');
99103
// This div's display was set with a prop.
@@ -115,6 +119,8 @@ describe('ReactDOMSuspensePlaceholder', () => {
115119

116120
await advanceTimers(1000);
117121

122+
Scheduler.flushAll();
123+
118124
expect(container.textContent).toEqual('ABC');
119125
});
120126

@@ -160,6 +166,8 @@ describe('ReactDOMSuspensePlaceholder', () => {
160166

161167
await advanceTimers(1000);
162168

169+
Scheduler.flushAll();
170+
163171
expect(container.innerHTML).toEqual(
164172
'<span style="display: inline;">Sibling</span><span style="">Async</span>',
165173
);

packages/react-reconciler/src/ReactDebugFiberPerf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ export function stopRequestCallbackTimer(
253253
expirationTime: number,
254254
): void {
255255
if (enableUserTimingAPI) {
256-
if (supportsUserTiming) {
256+
if (supportsUserTiming && isWaitingForCallback) {
257257
isWaitingForCallback = false;
258258
const warning = didExpire ? 'React was blocked by main thread' : null;
259259
endMark(

packages/react-reconciler/src/ReactFiberExpirationTime.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@
77
* @flow
88
*/
99

10+
import type {ReactPriorityLevel} from './SchedulerWithReactIntegration';
11+
1012
import MAX_SIGNED_31_BIT_INT from './maxSigned31BitInt';
1113

14+
import {
15+
ImmediatePriority,
16+
UserBlockingPriority,
17+
NormalPriority,
18+
IdlePriority,
19+
} from './SchedulerWithReactIntegration';
20+
1221
export type ExpirationTime = number;
1322

1423
export const NoWork = 0;
@@ -46,6 +55,8 @@ function computeExpirationBucket(
4655
);
4756
}
4857

58+
// TODO: This corresponds to Scheduler's NormalPriority, not LowPriority. Update
59+
// the names to reflect.
4960
export const LOW_PRIORITY_EXPIRATION = 5000;
5061
export const LOW_PRIORITY_BATCH_SIZE = 250;
5162

@@ -80,3 +91,31 @@ export function computeInteractiveExpiration(currentTime: ExpirationTime) {
8091
HIGH_PRIORITY_BATCH_SIZE,
8192
);
8293
}
94+
95+
export function inferPriorityFromExpirationTime(
96+
currentTime: ExpirationTime,
97+
expirationTime: ExpirationTime,
98+
): ReactPriorityLevel {
99+
if (expirationTime === Sync) {
100+
return ImmediatePriority;
101+
}
102+
if (expirationTime === Never) {
103+
return IdlePriority;
104+
}
105+
const msUntil =
106+
msToExpirationTime(expirationTime) - msToExpirationTime(currentTime);
107+
if (msUntil <= 0) {
108+
return ImmediatePriority;
109+
}
110+
if (msUntil <= HIGH_PRIORITY_EXPIRATION) {
111+
return UserBlockingPriority;
112+
}
113+
if (msUntil <= LOW_PRIORITY_EXPIRATION) {
114+
return NormalPriority;
115+
}
116+
117+
// TODO: Handle LowPriority
118+
119+
// Assume anything lower has idle priority
120+
return IdlePriority;
121+
}

packages/react-reconciler/src/ReactFiberReconciler.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import {
4343
requestCurrentTime,
4444
computeExpirationForFiber,
4545
scheduleWork,
46-
requestWork,
4746
flushRoot,
4847
batchedUpdates,
4948
unbatchedUpdates,
@@ -300,7 +299,6 @@ export function updateContainer(
300299

301300
export {
302301
flushRoot,
303-
requestWork,
304302
computeUniqueAsyncExpiration,
305303
batchedUpdates,
306304
unbatchedUpdates,

packages/react-reconciler/src/ReactFiberRoot.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ type BaseFiberRootProperties = {|
8383
firstBatch: Batch | null,
8484
// Linked-list of roots
8585
nextScheduledRoot: FiberRoot | null,
86+
87+
// New Scheduler fields
88+
callbackNode: *,
89+
callbackExpirationTime: ExpirationTime,
90+
firstPendingTime: ExpirationTime,
91+
lastPendingTime: ExpirationTime,
92+
pingTime: ExpirationTime,
8693
|};
8794

8895
// The following attributes are only used by interaction tracing builds.
@@ -145,6 +152,12 @@ export function createFiberRoot(
145152
interactionThreadID: unstable_getThreadID(),
146153
memoizedInteractions: new Set(),
147154
pendingInteractionMap: new Map(),
155+
156+
callbackNode: null,
157+
callbackExpirationTime: NoWork,
158+
firstPendingTime: NoWork,
159+
lastPendingTime: NoWork,
160+
pingTime: NoWork,
148161
}: FiberRoot);
149162
} else {
150163
root = ({
@@ -172,6 +185,12 @@ export function createFiberRoot(
172185
expirationTime: NoWork,
173186
firstBatch: null,
174187
nextScheduledRoot: null,
188+
189+
callbackNode: null,
190+
callbackExpirationTime: NoWork,
191+
firstPendingTime: NoWork,
192+
lastPendingTime: NoWork,
193+
pingTime: NoWork,
175194
}: BaseFiberRootProperties);
176195
}
177196

packages/react-reconciler/src/ReactFiberScheduler.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
markLegacyErrorBoundaryAsFailed as markLegacyErrorBoundaryAsFailed_old,
2323
isAlreadyFailedLegacyErrorBoundary as isAlreadyFailedLegacyErrorBoundary_old,
2424
scheduleWork as scheduleWork_old,
25-
requestWork as requestWork_old,
2625
flushRoot as flushRoot_old,
2726
batchedUpdates as batchedUpdates_old,
2827
unbatchedUpdates as unbatchedUpdates_old,
@@ -35,6 +34,7 @@ import {
3534
computeUniqueAsyncExpiration as computeUniqueAsyncExpiration_old,
3635
flushPassiveEffects as flushPassiveEffects_old,
3736
warnIfNotCurrentlyBatchingInDev as warnIfNotCurrentlyBatchingInDev_old,
37+
inferStartTimeFromExpirationTime as inferStartTimeFromExpirationTime_old,
3838
} from './ReactFiberScheduler.old';
3939

4040
import {
@@ -50,7 +50,6 @@ import {
5050
markLegacyErrorBoundaryAsFailed as markLegacyErrorBoundaryAsFailed_new,
5151
isAlreadyFailedLegacyErrorBoundary as isAlreadyFailedLegacyErrorBoundary_new,
5252
scheduleWork as scheduleWork_new,
53-
requestWork as requestWork_new,
5453
flushRoot as flushRoot_new,
5554
batchedUpdates as batchedUpdates_new,
5655
unbatchedUpdates as unbatchedUpdates_new,
@@ -63,6 +62,7 @@ import {
6362
computeUniqueAsyncExpiration as computeUniqueAsyncExpiration_new,
6463
flushPassiveEffects as flushPassiveEffects_new,
6564
warnIfNotCurrentlyBatchingInDev as warnIfNotCurrentlyBatchingInDev_new,
65+
inferStartTimeFromExpirationTime as inferStartTimeFromExpirationTime_new,
6666
} from './ReactFiberScheduler.new';
6767

6868
export let requestCurrentTime = requestCurrentTime_old;
@@ -77,7 +77,6 @@ export let resolveRetryThenable = resolveRetryThenable_old;
7777
export let markLegacyErrorBoundaryAsFailed = markLegacyErrorBoundaryAsFailed_old;
7878
export let isAlreadyFailedLegacyErrorBoundary = isAlreadyFailedLegacyErrorBoundary_old;
7979
export let scheduleWork = scheduleWork_old;
80-
export let requestWork = requestWork_old;
8180
export let flushRoot = flushRoot_old;
8281
export let batchedUpdates = batchedUpdates_old;
8382
export let unbatchedUpdates = unbatchedUpdates_old;
@@ -90,6 +89,7 @@ export let flushInteractiveUpdates = flushInteractiveUpdates_old;
9089
export let computeUniqueAsyncExpiration = computeUniqueAsyncExpiration_old;
9190
export let flushPassiveEffects = flushPassiveEffects_old;
9291
export let warnIfNotCurrentlyBatchingInDev = warnIfNotCurrentlyBatchingInDev_old;
92+
export let inferStartTimeFromExpirationTime = inferStartTimeFromExpirationTime_old;
9393

9494
if (enableNewScheduler) {
9595
requestCurrentTime = requestCurrentTime_new;
@@ -104,7 +104,6 @@ if (enableNewScheduler) {
104104
markLegacyErrorBoundaryAsFailed = markLegacyErrorBoundaryAsFailed_new;
105105
isAlreadyFailedLegacyErrorBoundary = isAlreadyFailedLegacyErrorBoundary_new;
106106
scheduleWork = scheduleWork_new;
107-
requestWork = requestWork_new;
108107
flushRoot = flushRoot_new;
109108
batchedUpdates = batchedUpdates_new;
110109
unbatchedUpdates = unbatchedUpdates_new;
@@ -117,6 +116,7 @@ if (enableNewScheduler) {
117116
computeUniqueAsyncExpiration = computeUniqueAsyncExpiration_new;
118117
flushPassiveEffects = flushPassiveEffects_new;
119118
warnIfNotCurrentlyBatchingInDev = warnIfNotCurrentlyBatchingInDev_new;
119+
inferStartTimeFromExpirationTime = inferStartTimeFromExpirationTime_new;
120120
}
121121

122122
export type Thenable = {

0 commit comments

Comments
 (0)