@samitouri / QOS-React-2 / commits / 41e62e7719

Remove runWithPriority internally

Rick Hanlon committed Mar 8, 2021 at 11:05 UTC 41e62e77191e112a7be38448e53470a6b8220611
8 files changed +45 -166
packages/react-dom/src/events/ReactDOMEventListener.js
+1 -20
@@ -12,10 +12,6 @@ import type {FiberRoot} from 'react-reconciler/src/ReactInternalTypes';
12 import type {Container, SuspenseInstance} from '../client/ReactDOMHostConfig';
13 import type {DOMEventName} from '../events/DOMEventNames';
14
15 -// Intentionally not named imports because Rollup would use dynamic dispatch for
16 -// CommonJS interop named imports.
17 -import * as Scheduler from 'scheduler';
18 -
15 import {
16 isReplayableDiscreteEvent,
17 queueDiscreteEvent,
@@ -89,11 +85,6 @@ const getCurrentPriorityLevel = enableNewReconciler
85 ? getCurrentPriorityLevel_new
86 : getCurrentPriorityLevel_old;
87
92 -const {
93 - unstable_UserBlockingPriority: UserBlockingPriority,
94 - unstable_runWithPriority: runWithPriority,
95 -} = Scheduler;
96 -
88 // TODO: can we stop exporting these?
89 export let _enabled = true;
90
@@ -178,18 +169,8 @@ function dispatchContinuousEvent(
169 ) {
170 const previousPriority = getCurrentUpdateLanePriority();
171 try {
181 - // TODO: Double wrapping is necessary while we decouple Scheduler priority.
172 setCurrentUpdateLanePriority(InputContinuousLanePriority);
183 - runWithPriority(
184 - UserBlockingPriority,
185 - dispatchEvent.bind(
186 - null,
187 - domEventName,
188 - eventSystemFlags,
189 - container,
190 - nativeEvent,
191 - ),
192 - );
173 + dispatchEvent(domEventName, eventSystemFlags, container, nativeEvent);
174 } finally {
175 setCurrentUpdateLanePriority(previousPriority);
176 }
packages/react-reconciler/src/ReactFiberHooks.new.js
+10 -27
@@ -87,12 +87,6 @@ import {
87 markWorkInProgressReceivedUpdate,
88 checkIfWorkInProgressReceivedUpdate,
89 } from './ReactFiberBeginWork.new';
90 -import {
91 - UserBlockingPriority,
92 - NormalPriority,
93 - runWithPriority,
94 - getCurrentPriorityLevel,
95 -} from './SchedulerWithReactIntegration.new';
90 import {getIsHydrating} from './ReactFiberHydrationContext.new';
91 import {
92 makeClientId,
@@ -1711,38 +1705,27 @@ function rerenderDeferredValue<T>(value: T): T {
1705 }
1706
1707 function startTransition(setPending, callback) {
1714 - const priorityLevel = getCurrentPriorityLevel();
1708 const previousLanePriority = getCurrentUpdateLanePriority();
1709 setCurrentUpdateLanePriority(
1710 higherLanePriority(previousLanePriority, InputContinuousLanePriority),
1711 );
1712
1720 - runWithPriority(
1721 - priorityLevel < UserBlockingPriority ? UserBlockingPriority : priorityLevel,
1722 - () => {
1723 - setPending(true);
1724 - },
1725 - );
1713 + setPending(true);
1714
1715 // TODO: Can remove this. Was only necessary because we used to give
1716 // different behavior to transitions without a config object. Now they are
1717 // all treated the same.
1718 setCurrentUpdateLanePriority(DefaultLanePriority);
1719
1732 - runWithPriority(
1733 - priorityLevel > NormalPriority ? NormalPriority : priorityLevel,
1734 - () => {
1735 - const prevTransition = ReactCurrentBatchConfig.transition;
1736 - ReactCurrentBatchConfig.transition = 1;
1737 - try {
1738 - setPending(false);
1739 - callback();
1740 - } finally {
1741 - setCurrentUpdateLanePriority(previousLanePriority);
1742 - ReactCurrentBatchConfig.transition = prevTransition;
1743 - }
1744 - },
1745 - );
1720 + const prevTransition = ReactCurrentBatchConfig.transition;
1721 + ReactCurrentBatchConfig.transition = 1;
1722 + try {
1723 + setPending(false);
1724 + callback();
1725 + } finally {
1726 + setCurrentUpdateLanePriority(previousLanePriority);
1727 + ReactCurrentBatchConfig.transition = prevTransition;
1728 + }
1729 }
1730
1731 function mountTransition(): [(() => void) => void, boolean] {
packages/react-reconciler/src/ReactFiberHooks.old.js
+10 -27
@@ -87,12 +87,6 @@ import {
87 markWorkInProgressReceivedUpdate,
88 checkIfWorkInProgressReceivedUpdate,
89 } from './ReactFiberBeginWork.old';
90 -import {
91 - UserBlockingPriority,
92 - NormalPriority,
93 - runWithPriority,
94 - getCurrentPriorityLevel,
95 -} from './SchedulerWithReactIntegration.old';
90 import {getIsHydrating} from './ReactFiberHydrationContext.old';
91 import {
92 makeClientId,
@@ -1711,38 +1705,27 @@ function rerenderDeferredValue<T>(value: T): T {
1705 }
1706
1707 function startTransition(setPending, callback) {
1714 - const priorityLevel = getCurrentPriorityLevel();
1708 const previousLanePriority = getCurrentUpdateLanePriority();
1709 setCurrentUpdateLanePriority(
1710 higherLanePriority(previousLanePriority, InputContinuousLanePriority),
1711 );
1712
1720 - runWithPriority(
1721 - priorityLevel < UserBlockingPriority ? UserBlockingPriority : priorityLevel,
1722 - () => {
1723 - setPending(true);
1724 - },
1725 - );
1713 + setPending(true);
1714
1715 // TODO: Can remove this. Was only necessary because we used to give
1716 // different behavior to transitions without a config object. Now they are
1717 // all treated the same.
1718 setCurrentUpdateLanePriority(DefaultLanePriority);
1719
1732 - runWithPriority(
1733 - priorityLevel > NormalPriority ? NormalPriority : priorityLevel,
1734 - () => {
1735 - const prevTransition = ReactCurrentBatchConfig.transition;
1736 - ReactCurrentBatchConfig.transition = 1;
1737 - try {
1738 - setPending(false);
1739 - callback();
1740 - } finally {
1741 - setCurrentUpdateLanePriority(previousLanePriority);
1742 - ReactCurrentBatchConfig.transition = prevTransition;
1743 - }
1744 - },
1745 - );
1720 + const prevTransition = ReactCurrentBatchConfig.transition;
1721 + ReactCurrentBatchConfig.transition = 1;
1722 + try {
1723 + setPending(false);
1724 + callback();
1725 + } finally {
1726 + setCurrentUpdateLanePriority(previousLanePriority);
1727 + ReactCurrentBatchConfig.transition = prevTransition;
1728 + }
1729 }
1730
1731 function mountTransition(): [(() => void) => void, boolean] {
packages/react-reconciler/src/ReactFiberWorkLoop.new.js
+6 -13
@@ -41,7 +41,6 @@ import {
41 scheduleCallback,
42 cancelCallback,
43 getCurrentPriorityLevel,
44 - runWithPriority,
44 shouldYield,
45 requestPaint,
46 now,
@@ -1124,7 +1123,7 @@ export function deferredUpdates<A>(fn: () => A): A {
1123 const previousLanePriority = getCurrentUpdateLanePriority();
1124 try {
1125 setCurrentUpdateLanePriority(DefaultLanePriority);
1127 - return runWithPriority(NormalSchedulerPriority, fn);
1126 + return fn();
1127 } finally {
1128 setCurrentUpdateLanePriority(previousLanePriority);
1129 }
@@ -1176,7 +1175,7 @@ export function batchedEventUpdates<A, R>(fn: A => R, a: A): R {
1175 }
1176
1177 export function discreteUpdates<A, B, C, D, R>(
1179 - fn: (A, B, C) => R,
1178 + fn: (A, B, C, D) => R,
1179 a: A,
1180 b: B,
1181 c: C,
@@ -1188,10 +1187,7 @@ export function discreteUpdates<A, B, C, D, R>(
1187 const previousLanePriority = getCurrentUpdateLanePriority();
1188 try {
1189 setCurrentUpdateLanePriority(InputDiscreteLanePriority);
1191 - return runWithPriority(
1192 - UserBlockingSchedulerPriority,
1193 - fn.bind(null, a, b, c, d),
1194 - );
1190 + return fn(a, b, c, d);
1191 } finally {
1192 setCurrentUpdateLanePriority(previousLanePriority);
1193 executionContext = prevExecutionContext;
@@ -1237,7 +1233,7 @@ export function flushSync<A, R>(fn: A => R, a: A): R {
1233 try {
1234 setCurrentUpdateLanePriority(SyncLanePriority);
1235 if (fn) {
1240 - return runWithPriority(ImmediateSchedulerPriority, fn.bind(null, a));
1236 + return fn(a);
1237 } else {
1238 return (undefined: $FlowFixMe);
1239 }
@@ -1257,7 +1253,7 @@ export function flushControlled(fn: () => mixed): void {
1253 const previousLanePriority = getCurrentUpdateLanePriority();
1254 try {
1255 setCurrentUpdateLanePriority(SyncLanePriority);
1260 - runWithPriority(ImmediateSchedulerPriority, fn);
1256 + fn();
1257 } finally {
1258 setCurrentUpdateLanePriority(previousLanePriority);
1259
@@ -1753,10 +1749,7 @@ function commitRoot(root) {
1749 const previousUpdateLanePriority = getCurrentUpdateLanePriority();
1750 try {
1751 setCurrentUpdateLanePriority(SyncLanePriority);
1756 - runWithPriority(
1757 - ImmediateSchedulerPriority,
1758 - commitRootImpl.bind(null, root, previousUpdateLanePriority),
1759 - );
1752 + commitRootImpl(root, previousUpdateLanePriority);
1753 } finally {
1754 setCurrentUpdateLanePriority(previousUpdateLanePriority);
1755 }
packages/react-reconciler/src/ReactFiberWorkLoop.old.js
+6 -13
@@ -41,7 +41,6 @@ import {
41 scheduleCallback,
42 cancelCallback,
43 getCurrentPriorityLevel,
44 - runWithPriority,
44 shouldYield,
45 requestPaint,
46 now,
@@ -1124,7 +1123,7 @@ export function deferredUpdates<A>(fn: () => A): A {
1123 const previousLanePriority = getCurrentUpdateLanePriority();
1124 try {
1125 setCurrentUpdateLanePriority(DefaultLanePriority);
1127 - return runWithPriority(NormalSchedulerPriority, fn);
1126 + return fn();
1127 } finally {
1128 setCurrentUpdateLanePriority(previousLanePriority);
1129 }
@@ -1176,7 +1175,7 @@ export function batchedEventUpdates<A, R>(fn: A => R, a: A): R {
1175 }
1176
1177 export function discreteUpdates<A, B, C, D, R>(
1179 - fn: (A, B, C) => R,
1178 + fn: (A, B, C, D) => R,
1179 a: A,
1180 b: B,
1181 c: C,
@@ -1188,10 +1187,7 @@ export function discreteUpdates<A, B, C, D, R>(
1187 const previousLanePriority = getCurrentUpdateLanePriority();
1188 try {
1189 setCurrentUpdateLanePriority(InputDiscreteLanePriority);
1191 - return runWithPriority(
1192 - UserBlockingSchedulerPriority,
1193 - fn.bind(null, a, b, c, d),
1194 - );
1190 + return fn(a, b, c, d);
1191 } finally {
1192 setCurrentUpdateLanePriority(previousLanePriority);
1193 executionContext = prevExecutionContext;
@@ -1237,7 +1233,7 @@ export function flushSync<A, R>(fn: A => R, a: A): R {
1233 try {
1234 setCurrentUpdateLanePriority(SyncLanePriority);
1235 if (fn) {
1240 - return runWithPriority(ImmediateSchedulerPriority, fn.bind(null, a));
1236 + return fn(a);
1237 } else {
1238 return (undefined: $FlowFixMe);
1239 }
@@ -1257,7 +1253,7 @@ export function flushControlled(fn: () => mixed): void {
1253 const previousLanePriority = getCurrentUpdateLanePriority();
1254 try {
1255 setCurrentUpdateLanePriority(SyncLanePriority);
1260 - runWithPriority(ImmediateSchedulerPriority, fn);
1256 + fn();
1257 } finally {
1258 setCurrentUpdateLanePriority(previousLanePriority);
1259
@@ -1753,10 +1749,7 @@ function commitRoot(root) {
1749 const previousUpdateLanePriority = getCurrentUpdateLanePriority();
1750 try {
1751 setCurrentUpdateLanePriority(SyncLanePriority);
1756 - runWithPriority(
1757 - ImmediateSchedulerPriority,
1758 - commitRootImpl.bind(null, root, previousUpdateLanePriority),
1759 - );
1752 + commitRootImpl(root, previousUpdateLanePriority);
1753 } finally {
1754 setCurrentUpdateLanePriority(previousUpdateLanePriority);
1755 }
packages/react-reconciler/src/SchedulerWithReactIntegration.new.js
+6 -17
@@ -26,7 +26,6 @@ import {
26 import {scheduleMicrotask, supportsMicrotasks} from './ReactFiberHostConfig';
27
28 const {
29 - unstable_runWithPriority: Scheduler_runWithPriority,
29 unstable_scheduleCallback: Scheduler_scheduleCallback,
30 unstable_cancelCallback: Scheduler_cancelCallback,
31 unstable_shouldYield: Scheduler_shouldYield,
@@ -123,14 +122,6 @@ function reactPriorityToSchedulerPriority(reactPriorityLevel) {
122 }
123 }
124
126 -export function runWithPriority<T>(
127 - reactPriorityLevel: ReactPriorityLevel,
128 - fn: () => T,
129 -): T {
130 - const priorityLevel = reactPriorityToSchedulerPriority(reactPriorityLevel);
131 - return Scheduler_runWithPriority(priorityLevel, fn);
132 -}
133 -
125 export function scheduleCallback(
126 reactPriorityLevel: ReactPriorityLevel,
127 callback: SchedulerCallback,
@@ -188,14 +179,12 @@ function flushSyncCallbackQueueImpl() {
179 const isSync = true;
180 const queue = syncQueue;
181 setCurrentUpdateLanePriority(SyncLanePriority);
191 - runWithPriority(ImmediatePriority, () => {
192 - for (; i < queue.length; i++) {
193 - let callback = queue[i];
194 - do {
195 - callback = callback(isSync);
196 - } while (callback !== null);
197 - }
198 - });
182 + for (; i < queue.length; i++) {
183 + let callback = queue[i];
184 + do {
185 + callback = callback(isSync);
186 + } while (callback !== null);
187 + }
188 syncQueue = null;
189 } catch (error) {
190 // If something throws, leave the remaining callbacks on the queue.
packages/react-reconciler/src/SchedulerWithReactIntegration.old.js
+6 -17
@@ -26,7 +26,6 @@ import {
26 import {scheduleMicrotask, supportsMicrotasks} from './ReactFiberHostConfig';
27
28 const {
29 - unstable_runWithPriority: Scheduler_runWithPriority,
29 unstable_scheduleCallback: Scheduler_scheduleCallback,
30 unstable_cancelCallback: Scheduler_cancelCallback,
31 unstable_shouldYield: Scheduler_shouldYield,
@@ -123,14 +122,6 @@ function reactPriorityToSchedulerPriority(reactPriorityLevel) {
122 }
123 }
124
126 -export function runWithPriority<T>(
127 - reactPriorityLevel: ReactPriorityLevel,
128 - fn: () => T,
129 -): T {
130 - const priorityLevel = reactPriorityToSchedulerPriority(reactPriorityLevel);
131 - return Scheduler_runWithPriority(priorityLevel, fn);
132 -}
133 -
125 export function scheduleCallback(
126 reactPriorityLevel: ReactPriorityLevel,
127 callback: SchedulerCallback,
@@ -188,14 +179,12 @@ function flushSyncCallbackQueueImpl() {
179 const isSync = true;
180 const queue = syncQueue;
181 setCurrentUpdateLanePriority(SyncLanePriority);
191 - runWithPriority(ImmediatePriority, () => {
192 - for (; i < queue.length; i++) {
193 - let callback = queue[i];
194 - do {
195 - callback = callback(isSync);
196 - } while (callback !== null);
197 - }
198 - });
182 + for (; i < queue.length; i++) {
183 + let callback = queue[i];
184 + do {
185 + callback = callback(isSync);
186 + } while (callback !== null);
187 + }
188 syncQueue = null;
189 } catch (error) {
190 // If something throws, leave the remaining callbacks on the queue.
packages/react-reconciler/src/__tests__/ReactSchedulerIntegration-test.js
-32
@@ -68,17 +68,6 @@ describe('ReactSchedulerIntegration', () => {
68 );
69 }
70
71 - it('flush sync has correct priority', () => {
72 - function ReadPriority() {
73 - Scheduler.unstable_yieldValue(
74 - 'Priority: ' + getCurrentPriorityAsString(),
75 - );
76 - return null;
77 - }
78 - ReactNoop.flushSync(() => ReactNoop.render(<ReadPriority />));
79 - expect(Scheduler).toHaveYielded(['Priority: Immediate']);
80 - });
81 -
71 // TODO: Figure out what to do with these tests. I don't think most of them
72 // make sense once we decouple Scheduler from React. Perhaps need similar
73 // tests for React DOM.
@@ -139,27 +128,6 @@ describe('ReactSchedulerIntegration', () => {
128 ]);
129 });
130
142 - it('layout effects have immediate priority', () => {
143 - const {useLayoutEffect} = React;
144 - function ReadPriority() {
145 - Scheduler.unstable_yieldValue(
146 - 'Render priority: ' + getCurrentPriorityAsString(),
147 - );
148 - useLayoutEffect(() => {
149 - Scheduler.unstable_yieldValue(
150 - 'Layout priority: ' + getCurrentPriorityAsString(),
151 - );
152 - });
153 - return null;
154 - }
155 -
156 - ReactNoop.render(<ReadPriority />);
157 - expect(Scheduler).toFlushAndYield([
158 - 'Render priority: Normal',
159 - 'Layout priority: Immediate',
160 - ]);
161 - });
162 -
131 it('passive effects are called before Normal-pri scheduled in layout effects', async () => {
132 const {useEffect, useLayoutEffect} = React;
133 function Effects({step}) {