Skip to content

Commit 11565a2

Browse files
author
Brian Vaughn
committed
Revert "Scheduler.unstable_next (#14756)"
This reverts commit bc9818f.
1 parent f2e2637 commit 11565a2

File tree

8 files changed

+78
-166
lines changed

8 files changed

+78
-166
lines changed

packages/react-cache/src/__tests__/ReactCache-test.internal.js

-22
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ describe('ReactCache', () => {
2424
beforeEach(() => {
2525
jest.resetModules();
2626

27-
let currentPriorityLevel = 3;
28-
2927
jest.mock('scheduler', () => {
3028
let callbacks = [];
3129
return {
@@ -40,26 +38,6 @@ describe('ReactCache', () => {
4038
callback();
4139
}
4240
},
43-
44-
unstable_ImmediatePriority: 1,
45-
unstable_UserBlockingPriority: 2,
46-
unstable_NormalPriority: 3,
47-
unstable_LowPriority: 4,
48-
unstable_IdlePriority: 5,
49-
50-
unstable_runWithPriority(priorityLevel, fn) {
51-
const prevPriorityLevel = currentPriorityLevel;
52-
currentPriorityLevel = priorityLevel;
53-
try {
54-
return fn();
55-
} finally {
56-
currentPriorityLevel = prevPriorityLevel;
57-
}
58-
},
59-
60-
unstable_getCurrentPriorityLevel() {
61-
return currentPriorityLevel;
62-
},
6341
};
6442
});
6543

packages/react-reconciler/src/ReactFiberScheduler.js

+78-70
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,8 @@ import type {Interaction} from 'scheduler/src/Tracing';
1515
import {
1616
__interactionsRef,
1717
__subscriberRef,
18-
unstable_wrap as Scheduler_tracing_wrap,
18+
unstable_wrap as Schedule_tracing_wrap,
1919
} from 'scheduler/tracing';
20-
import {
21-
unstable_next as Scheduler_next,
22-
unstable_getCurrentPriorityLevel as getCurrentPriorityLevel,
23-
unstable_runWithPriority as runWithPriority,
24-
unstable_ImmediatePriority as ImmediatePriority,
25-
unstable_UserBlockingPriority as UserBlockingPriority,
26-
unstable_NormalPriority as NormalPriority,
27-
unstable_LowPriority as LowPriority,
28-
unstable_IdlePriority as IdlePriority,
29-
} from 'scheduler';
3020
import {
3121
invokeGuardedCallback,
3222
hasCaughtError,
@@ -132,7 +122,7 @@ import {
132122
computeAsyncExpiration,
133123
computeInteractiveExpiration,
134124
} from './ReactFiberExpirationTime';
135-
import {ConcurrentMode, ProfileMode, NoContext} from './ReactTypeOfMode';
125+
import {ConcurrentMode, ProfileMode} from './ReactTypeOfMode';
136126
import {enqueueUpdate, resetCurrentlyProcessingQueue} from './ReactUpdateQueue';
137127
import {createCapturedValue} from './ReactCapturedValue';
138128
import {
@@ -252,6 +242,11 @@ if (__DEV__) {
252242
// Used to ensure computeUniqueAsyncExpiration is monotonically decreasing.
253243
let lastUniqueAsyncExpiration: number = Sync - 1;
254244

245+
// Represents the expiration time that incoming updates should use. (If this
246+
// is NoWork, use the default strategy: async updates in async mode, sync
247+
// updates in sync mode.)
248+
let expirationContext: ExpirationTime = NoWork;
249+
255250
let isWorking: boolean = false;
256251

257252
// The next work in progress fiber that we're currently working on.
@@ -798,11 +793,9 @@ function commitRoot(root: FiberRoot, finishedWork: Fiber): void {
798793
// TODO: Avoid this extra callback by mutating the tracing ref directly,
799794
// like we do at the beginning of commitRoot. I've opted not to do that
800795
// here because that code is still in flux.
801-
callback = Scheduler_tracing_wrap(callback);
796+
callback = Schedule_tracing_wrap(callback);
802797
}
803-
passiveEffectCallbackHandle = runWithPriority(NormalPriority, () => {
804-
return schedulePassiveEffects(callback);
805-
});
798+
passiveEffectCallbackHandle = schedulePassiveEffects(callback);
806799
passiveEffectCallback = callback;
807800
}
808801

@@ -1586,58 +1579,52 @@ function computeUniqueAsyncExpiration(): ExpirationTime {
15861579
}
15871580

15881581
function computeExpirationForFiber(currentTime: ExpirationTime, fiber: Fiber) {
1589-
const priorityLevel = getCurrentPriorityLevel();
1590-
15911582
let expirationTime;
1592-
if ((fiber.mode & ConcurrentMode) === NoContext) {
1593-
// Outside of concurrent mode, updates are always synchronous.
1594-
expirationTime = Sync;
1595-
} else if (isWorking && !isCommitting) {
1596-
// During render phase, updates expire during as the current render.
1597-
expirationTime = nextRenderExpirationTime;
1583+
if (expirationContext !== NoWork) {
1584+
// An explicit expiration context was set;
1585+
expirationTime = expirationContext;
1586+
} else if (isWorking) {
1587+
if (isCommitting) {
1588+
// Updates that occur during the commit phase should have sync priority
1589+
// by default.
1590+
expirationTime = Sync;
1591+
} else {
1592+
// Updates during the render phase should expire at the same time as
1593+
// the work that is being rendered.
1594+
expirationTime = nextRenderExpirationTime;
1595+
}
15981596
} else {
1599-
switch (priorityLevel) {
1600-
case ImmediatePriority:
1601-
expirationTime = Sync;
1602-
break;
1603-
case UserBlockingPriority:
1597+
// No explicit expiration context was set, and we're not currently
1598+
// performing work. Calculate a new expiration time.
1599+
if (fiber.mode & ConcurrentMode) {
1600+
if (isBatchingInteractiveUpdates) {
1601+
// This is an interactive update
16041602
expirationTime = computeInteractiveExpiration(currentTime);
1605-
break;
1606-
case NormalPriority:
1607-
// This is a normal, concurrent update
1603+
} else {
1604+
// This is an async update
16081605
expirationTime = computeAsyncExpiration(currentTime);
1609-
break;
1610-
case LowPriority:
1611-
case IdlePriority:
1612-
expirationTime = Never;
1613-
break;
1614-
default:
1615-
invariant(
1616-
false,
1617-
'Unknown priority level. This error is likely caused by a bug in ' +
1618-
'React. Please file an issue.',
1619-
);
1620-
}
1621-
1622-
// If we're in the middle of rendering a tree, do not update at the same
1623-
// expiration time that is already rendering.
1624-
if (nextRoot !== null && expirationTime === nextRenderExpirationTime) {
1625-
expirationTime -= 1;
1606+
}
1607+
// If we're in the middle of rendering a tree, do not update at the same
1608+
// expiration time that is already rendering.
1609+
if (nextRoot !== null && expirationTime === nextRenderExpirationTime) {
1610+
expirationTime -= 1;
1611+
}
1612+
} else {
1613+
// This is a sync update
1614+
expirationTime = Sync;
16261615
}
16271616
}
1628-
1629-
// Keep track of the lowest pending interactive expiration time. This
1630-
// allows us to synchronously flush all interactive updates
1631-
// when needed.
1632-
// TODO: Move this to renderer?
1633-
if (
1634-
priorityLevel === UserBlockingPriority &&
1635-
(lowestPriorityPendingInteractiveExpirationTime === NoWork ||
1636-
expirationTime < lowestPriorityPendingInteractiveExpirationTime)
1637-
) {
1638-
lowestPriorityPendingInteractiveExpirationTime = expirationTime;
1617+
if (isBatchingInteractiveUpdates) {
1618+
// This is an interactive update. Keep track of the lowest pending
1619+
// interactive expiration time. This allows us to synchronously flush
1620+
// all interactive updates when needed.
1621+
if (
1622+
lowestPriorityPendingInteractiveExpirationTime === NoWork ||
1623+
expirationTime < lowestPriorityPendingInteractiveExpirationTime
1624+
) {
1625+
lowestPriorityPendingInteractiveExpirationTime = expirationTime;
1626+
}
16391627
}
1640-
16411628
return expirationTime;
16421629
}
16431630

@@ -1875,16 +1862,34 @@ function scheduleWork(fiber: Fiber, expirationTime: ExpirationTime) {
18751862
}
18761863
}
18771864

1865+
function deferredUpdates<A>(fn: () => A): A {
1866+
const currentTime = requestCurrentTime();
1867+
const previousExpirationContext = expirationContext;
1868+
const previousIsBatchingInteractiveUpdates = isBatchingInteractiveUpdates;
1869+
expirationContext = computeAsyncExpiration(currentTime);
1870+
isBatchingInteractiveUpdates = false;
1871+
try {
1872+
return fn();
1873+
} finally {
1874+
expirationContext = previousExpirationContext;
1875+
isBatchingInteractiveUpdates = previousIsBatchingInteractiveUpdates;
1876+
}
1877+
}
1878+
18781879
function syncUpdates<A, B, C0, D, R>(
18791880
fn: (A, B, C0, D) => R,
18801881
a: A,
18811882
b: B,
18821883
c: C0,
18831884
d: D,
18841885
): R {
1885-
return runWithPriority(ImmediatePriority, () => {
1886+
const previousExpirationContext = expirationContext;
1887+
expirationContext = Sync;
1888+
try {
18861889
return fn(a, b, c, d);
1887-
});
1890+
} finally {
1891+
expirationContext = previousExpirationContext;
1892+
}
18881893
}
18891894

18901895
// TODO: Everything below this is written as if it has been lifted to the
@@ -1905,6 +1910,7 @@ let unhandledError: mixed | null = null;
19051910

19061911
let isBatchingUpdates: boolean = false;
19071912
let isUnbatchingUpdates: boolean = false;
1913+
let isBatchingInteractiveUpdates: boolean = false;
19081914

19091915
let completedBatches: Array<Batch> | null = null;
19101916

@@ -2435,9 +2441,7 @@ function completeRoot(
24352441
lastCommittedRootDuringThisBatch = root;
24362442
nestedUpdateCount = 0;
24372443
}
2438-
runWithPriority(ImmediatePriority, () => {
2439-
commitRoot(root, finishedWork);
2440-
});
2444+
commitRoot(root, finishedWork);
24412445
}
24422446

24432447
function onUncaughtError(error: mixed) {
@@ -2503,6 +2507,9 @@ function flushSync<A, R>(fn: (a: A) => R, a: A): R {
25032507
}
25042508

25052509
function interactiveUpdates<A, B, R>(fn: (A, B) => R, a: A, b: B): R {
2510+
if (isBatchingInteractiveUpdates) {
2511+
return fn(a, b);
2512+
}
25062513
// If there are any pending interactive updates, synchronously flush them.
25072514
// This needs to happen before we read any handlers, because the effect of
25082515
// the previous event may influence which handlers are called during
@@ -2516,13 +2523,14 @@ function interactiveUpdates<A, B, R>(fn: (A, B) => R, a: A, b: B): R {
25162523
performWork(lowestPriorityPendingInteractiveExpirationTime, false);
25172524
lowestPriorityPendingInteractiveExpirationTime = NoWork;
25182525
}
2526+
const previousIsBatchingInteractiveUpdates = isBatchingInteractiveUpdates;
25192527
const previousIsBatchingUpdates = isBatchingUpdates;
2528+
isBatchingInteractiveUpdates = true;
25202529
isBatchingUpdates = true;
25212530
try {
2522-
return runWithPriority(UserBlockingPriority, () => {
2523-
return fn(a, b);
2524-
});
2531+
return fn(a, b);
25252532
} finally {
2533+
isBatchingInteractiveUpdates = previousIsBatchingInteractiveUpdates;
25262534
isBatchingUpdates = previousIsBatchingUpdates;
25272535
if (!isBatchingUpdates && !isRendering) {
25282536
performSyncWork();
@@ -2572,7 +2580,7 @@ export {
25722580
unbatchedUpdates,
25732581
flushSync,
25742582
flushControlled,
2575-
Scheduler_next as deferredUpdates,
2583+
deferredUpdates,
25762584
syncUpdates,
25772585
interactiveUpdates,
25782586
flushInteractiveUpdates,

packages/react/src/ReactSharedInternals.js

-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
unstable_now,
1313
unstable_scheduleCallback,
1414
unstable_runWithPriority,
15-
unstable_next,
1615
unstable_getFirstCallbackNode,
1716
unstable_pauseExecution,
1817
unstable_continueExecution,
@@ -54,7 +53,6 @@ if (__UMD__) {
5453
unstable_now,
5554
unstable_scheduleCallback,
5655
unstable_runWithPriority,
57-
unstable_next,
5856
unstable_wrapCallback,
5957
unstable_getFirstCallbackNode,
6058
unstable_pauseExecution,

packages/scheduler/npm/umd/scheduler.development.js

-8
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@
5454
);
5555
}
5656

57-
function unstable_next() {
58-
return global.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Scheduler.unstable_next.apply(
59-
this,
60-
arguments
61-
);
62-
}
63-
6457
function unstable_wrapCallback() {
6558
return global.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Scheduler.unstable_wrapCallback.apply(
6659
this,
@@ -102,7 +95,6 @@
10295
unstable_cancelCallback: unstable_cancelCallback,
10396
unstable_shouldYield: unstable_shouldYield,
10497
unstable_runWithPriority: unstable_runWithPriority,
105-
unstable_next: unstable_next,
10698
unstable_wrapCallback: unstable_wrapCallback,
10799
unstable_getCurrentPriorityLevel: unstable_getCurrentPriorityLevel,
108100
unstable_continueExecution: unstable_continueExecution,

packages/scheduler/npm/umd/scheduler.production.min.js

-8
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@
5454
);
5555
}
5656

57-
function unstable_next() {
58-
return global.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Scheduler.unstable_next.apply(
59-
this,
60-
arguments
61-
);
62-
}
63-
6457
function unstable_wrapCallback() {
6558
return global.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Scheduler.unstable_wrapCallback.apply(
6659
this,
@@ -96,7 +89,6 @@
9689
unstable_cancelCallback: unstable_cancelCallback,
9790
unstable_shouldYield: unstable_shouldYield,
9891
unstable_runWithPriority: unstable_runWithPriority,
99-
unstable_next: unstable_next,
10092
unstable_wrapCallback: unstable_wrapCallback,
10193
unstable_getCurrentPriorityLevel: unstable_getCurrentPriorityLevel,
10294
unstable_continueExecution: unstable_continueExecution,

packages/scheduler/npm/umd/scheduler.profiling.min.js

-8
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@
5454
);
5555
}
5656

57-
function unstable_next() {
58-
return global.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Scheduler.unstable_next.apply(
59-
this,
60-
arguments
61-
);
62-
}
63-
6457
function unstable_wrapCallback() {
6558
return global.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Scheduler.unstable_wrapCallback.apply(
6659
this,
@@ -96,7 +89,6 @@
9689
unstable_cancelCallback: unstable_cancelCallback,
9790
unstable_shouldYield: unstable_shouldYield,
9891
unstable_runWithPriority: unstable_runWithPriority,
99-
unstable_next: unstable_next,
10092
unstable_wrapCallback: unstable_wrapCallback,
10193
unstable_getCurrentPriorityLevel: unstable_getCurrentPriorityLevel,
10294
unstable_continueExecution: unstable_continueExecution,

0 commit comments

Comments
 (0)