diff --git a/packages/react-art/src/ReactARTHostConfig.js b/packages/react-art/src/ReactARTHostConfig.js index 64114982cc379..7f0196d9f6645 100644 --- a/packages/react-art/src/ReactARTHostConfig.js +++ b/packages/react-art/src/ReactARTHostConfig.js @@ -343,8 +343,6 @@ export function getChildHostContext() { export const scheduleTimeout = setTimeout; export const cancelTimeout = clearTimeout; export const noTimeout = -1; -export const schedulePassiveEffects = scheduleDeferredCallback; -export const cancelPassiveEffects = cancelDeferredCallback; export function shouldSetTextContent(type, props) { return ( diff --git a/packages/react-dom/src/client/ReactDOMHostConfig.js b/packages/react-dom/src/client/ReactDOMHostConfig.js index 3467711921a1c..1576b6ae9ff92 100644 --- a/packages/react-dom/src/client/ReactDOMHostConfig.js +++ b/packages/react-dom/src/client/ReactDOMHostConfig.js @@ -310,8 +310,6 @@ export const scheduleTimeout = export const cancelTimeout = typeof clearTimeout === 'function' ? clearTimeout : (undefined: any); export const noTimeout = -1; -export const schedulePassiveEffects = scheduleDeferredCallback; -export const cancelPassiveEffects = cancelDeferredCallback; // ------------------- // Mutation diff --git a/packages/react-native-renderer/src/ReactFabricHostConfig.js b/packages/react-native-renderer/src/ReactFabricHostConfig.js index a1e0b882c6ef6..bb4646b190bce 100644 --- a/packages/react-native-renderer/src/ReactFabricHostConfig.js +++ b/packages/react-native-renderer/src/ReactFabricHostConfig.js @@ -20,12 +20,6 @@ import { warnForStyleProps, } from './NativeMethodsMixinUtils'; import {create, diff} from './ReactNativeAttributePayload'; -import { - now as ReactNativeFrameSchedulingNow, - cancelDeferredCallback as ReactNativeFrameSchedulingCancelDeferredCallback, - scheduleDeferredCallback as ReactNativeFrameSchedulingScheduleDeferredCallback, - shouldYield as ReactNativeFrameSchedulingShouldYield, -} from './ReactNativeFrameScheduling'; import {get as getViewConfigForType} from 'ReactNativeViewConfigRegistry'; import deepFreezeAndThrowOnMutationInDev from 'deepFreezeAndThrowOnMutationInDev'; @@ -333,16 +327,10 @@ export function shouldSetTextContent(type: string, props: Props): boolean { // The Fabric renderer is secondary to the existing React Native renderer. export const isPrimaryRenderer = false; -export const now = ReactNativeFrameSchedulingNow; -export const scheduleDeferredCallback = ReactNativeFrameSchedulingScheduleDeferredCallback; -export const cancelDeferredCallback = ReactNativeFrameSchedulingCancelDeferredCallback; -export const shouldYield = ReactNativeFrameSchedulingShouldYield; export const scheduleTimeout = setTimeout; export const cancelTimeout = clearTimeout; export const noTimeout = -1; -export const schedulePassiveEffects = scheduleDeferredCallback; -export const cancelPassiveEffects = cancelDeferredCallback; // ------------------- // Persistence diff --git a/packages/react-native-renderer/src/ReactNativeFrameScheduling.js b/packages/react-native-renderer/src/ReactNativeFrameScheduling.js deleted file mode 100644 index 3c11c0d20ac79..0000000000000 --- a/packages/react-native-renderer/src/ReactNativeFrameScheduling.js +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow - */ - -const hasNativePerformanceNow = - typeof performance === 'object' && typeof performance.now === 'function'; - -const now = hasNativePerformanceNow - ? () => performance.now() - : () => Date.now(); - -let scheduledCallback: (() => mixed) | null = null; -let frameDeadline: number = 0; - -function setTimeoutCallback() { - // TODO (bvaughn) Hard-coded 5ms unblocks initial async testing. - // React API probably changing to boolean rather than time remaining. - // Longer-term plan is to rewrite this using shared memory, - // And just return the value of the bit as the boolean. - frameDeadline = now() + 5; - - const callback = scheduledCallback; - scheduledCallback = null; - if (callback !== null) { - callback(); - } -} - -// RN has a poor polyfill for requestIdleCallback so we aren't using it. -// This implementation is only intended for short-term use anyway. -// We also don't implement cancel functionality b'c Fiber doesn't currently need it. -function scheduleDeferredCallback( - callback: () => mixed, - options?: {timeout: number}, -): number { - // We assume only one callback is scheduled at a time b'c that's how Fiber works. - scheduledCallback = callback; - const timeoutId = setTimeout(setTimeoutCallback, 1); - return (timeoutId: any); // Timeouts are always numbers on RN -} - -function cancelDeferredCallback(callbackID: number) { - scheduledCallback = null; - clearTimeout((callbackID: any)); // Timeouts are always numbers on RN -} - -function shouldYield() { - return frameDeadline <= now(); -} - -export {now, scheduleDeferredCallback, cancelDeferredCallback, shouldYield}; diff --git a/packages/react-native-renderer/src/ReactNativeHostConfig.js b/packages/react-native-renderer/src/ReactNativeHostConfig.js index b16a3174eba91..dca90571b0c37 100644 --- a/packages/react-native-renderer/src/ReactNativeHostConfig.js +++ b/packages/react-native-renderer/src/ReactNativeHostConfig.js @@ -23,12 +23,6 @@ import { updateFiberProps, } from './ReactNativeComponentTree'; import ReactNativeFiberHostComponent from './ReactNativeFiberHostComponent'; -import { - now as ReactNativeFrameSchedulingNow, - cancelDeferredCallback as ReactNativeFrameSchedulingCancelDeferredCallback, - scheduleDeferredCallback as ReactNativeFrameSchedulingScheduleDeferredCallback, - shouldYield as ReactNativeFrameSchedulingShouldYield, -} from './ReactNativeFrameScheduling'; export type Type = string; export type Props = Object; @@ -234,17 +228,11 @@ export function resetAfterCommit(containerInfo: Container): void { // Noop } -export const now = ReactNativeFrameSchedulingNow; export const isPrimaryRenderer = true; -export const scheduleDeferredCallback = ReactNativeFrameSchedulingScheduleDeferredCallback; -export const cancelDeferredCallback = ReactNativeFrameSchedulingCancelDeferredCallback; -export const shouldYield = ReactNativeFrameSchedulingShouldYield; export const scheduleTimeout = setTimeout; export const cancelTimeout = clearTimeout; export const noTimeout = -1; -export const schedulePassiveEffects = scheduleDeferredCallback; -export const cancelPassiveEffects = cancelDeferredCallback; export function shouldDeprioritizeSubtree(type: string, props: Props): boolean { return false; diff --git a/packages/react-noop-renderer/src/createReactNoop.js b/packages/react-noop-renderer/src/createReactNoop.js index ab1a93ad4ce66..b24bb68a2a8d1 100644 --- a/packages/react-noop-renderer/src/createReactNoop.js +++ b/packages/react-noop-renderer/src/createReactNoop.js @@ -304,18 +304,10 @@ function createReactNoop(reconciler: Function, useMutation: boolean) { return inst; }, - scheduleDeferredCallback: Scheduler.unstable_scheduleCallback, - cancelDeferredCallback: Scheduler.unstable_cancelCallback, - - shouldYield: Scheduler.unstable_shouldYield, - scheduleTimeout: setTimeout, cancelTimeout: clearTimeout, noTimeout: -1, - schedulePassiveEffects: Scheduler.unstable_scheduleCallback, - cancelPassiveEffects: Scheduler.unstable_cancelCallback, - prepareForCommit(): void {}, resetAfterCommit(): void {}, diff --git a/packages/react-reconciler/src/ReactFiberScheduler.js b/packages/react-reconciler/src/ReactFiberScheduler.js index be5062139ab13..390f46af4ea4c 100644 --- a/packages/react-reconciler/src/ReactFiberScheduler.js +++ b/packages/react-reconciler/src/ReactFiberScheduler.js @@ -14,9 +14,7 @@ import type {Interaction} from 'scheduler/src/Tracing'; // Intentionally not named imports because Rollup would use dynamic dispatch for // CommonJS interop named imports. -// TODO: We're not using this import anymore, but I've left this here so we -// don't accidentally use named imports when we add it back. -// import * as Scheduler from 'scheduler'; +import * as Scheduler from 'scheduler'; import { __interactionsRef, __subscriberRef, @@ -78,17 +76,11 @@ import { setCurrentFiber, } from './ReactCurrentFiber'; import { - now, - scheduleDeferredCallback, - cancelDeferredCallback, - shouldYield, prepareForCommit, resetAfterCommit, scheduleTimeout, cancelTimeout, noTimeout, - schedulePassiveEffects, - cancelPassiveEffects, } from './ReactFiberHostConfig'; import { markPendingPriorityLevel, @@ -172,6 +164,15 @@ import { } from './ReactFiberCommitWork'; import {ContextOnlyDispatcher} from './ReactFiberHooks'; +// Intentionally not named imports because Rollup would use dynamic dispatch for +// CommonJS interop named imports. +const { + unstable_scheduleCallback: scheduleCallback, + unstable_cancelCallback: cancelCallback, + unstable_shouldYield: shouldYield, + unstable_now: now, +} = Scheduler; + export type Thenable = { then(resolve: () => mixed, reject?: () => mixed): mixed, }; @@ -598,7 +599,7 @@ function markLegacyErrorBoundaryAsFailed(instance: mixed) { function flushPassiveEffects() { if (passiveEffectCallbackHandle !== null) { - cancelPassiveEffects(passiveEffectCallbackHandle); + cancelCallback(passiveEffectCallbackHandle); } if (passiveEffectCallback !== null) { // We call the scheduled callback instead of commitPassiveEffects directly @@ -807,7 +808,7 @@ function commitRoot(root: FiberRoot, finishedWork: Fiber): void { // here because that code is still in flux. callback = Scheduler_tracing_wrap(callback); } - passiveEffectCallbackHandle = schedulePassiveEffects(callback); + passiveEffectCallbackHandle = scheduleCallback(callback); passiveEffectCallback = callback; } @@ -1978,7 +1979,7 @@ function scheduleCallbackWithExpirationTime( if (callbackID !== null) { // Existing callback has insufficient timeout. Cancel and schedule a // new one. - cancelDeferredCallback(callbackID); + cancelCallback(callbackID); } } // The request callback timer is already running. Don't start a new one. @@ -1990,7 +1991,7 @@ function scheduleCallbackWithExpirationTime( const currentMs = now() - originalStartTimeMs; const expirationTimeMs = expirationTimeToMs(expirationTime); const timeout = expirationTimeMs - currentMs; - callbackID = scheduleDeferredCallback(performAsyncWork, {timeout}); + callbackID = scheduleCallback(performAsyncWork, {timeout}); } // For every call to renderRoot, one of onFatal, onComplete, onSuspend, and diff --git a/packages/react-reconciler/src/ReactProfilerTimer.js b/packages/react-reconciler/src/ReactProfilerTimer.js index 5b78822ca647d..6aa1642dfe6c0 100644 --- a/packages/react-reconciler/src/ReactProfilerTimer.js +++ b/packages/react-reconciler/src/ReactProfilerTimer.js @@ -11,7 +11,11 @@ import type {Fiber} from './ReactFiber'; import {enableProfilerTimer} from 'shared/ReactFeatureFlags'; -import {now} from './ReactFiberHostConfig'; +// Intentionally not named imports because Rollup would use dynamic dispatch for +// CommonJS interop named imports. +import * as Scheduler from 'scheduler'; + +const {unstable_now: now} = Scheduler; export type ProfilerTimer = { getCommitTime(): number, diff --git a/packages/react-reconciler/src/forks/ReactFiberHostConfig.custom.js b/packages/react-reconciler/src/forks/ReactFiberHostConfig.custom.js index 7dde223171587..994ca852ad327 100644 --- a/packages/react-reconciler/src/forks/ReactFiberHostConfig.custom.js +++ b/packages/react-reconciler/src/forks/ReactFiberHostConfig.custom.js @@ -51,14 +51,9 @@ export const shouldSetTextContent = $$$hostConfig.shouldSetTextContent; export const shouldDeprioritizeSubtree = $$$hostConfig.shouldDeprioritizeSubtree; export const createTextInstance = $$$hostConfig.createTextInstance; -export const scheduleDeferredCallback = $$$hostConfig.scheduleDeferredCallback; -export const cancelDeferredCallback = $$$hostConfig.cancelDeferredCallback; -export const shouldYield = $$$hostConfig.shouldYield; export const scheduleTimeout = $$$hostConfig.setTimeout; export const cancelTimeout = $$$hostConfig.clearTimeout; export const noTimeout = $$$hostConfig.noTimeout; -export const schedulePassiveEffects = $$$hostConfig.schedulePassiveEffects; -export const cancelPassiveEffects = $$$hostConfig.cancelPassiveEffects; export const now = $$$hostConfig.now; export const isPrimaryRenderer = $$$hostConfig.isPrimaryRenderer; export const supportsMutation = $$$hostConfig.supportsMutation; diff --git a/packages/react-test-renderer/src/ReactTestHostConfig.js b/packages/react-test-renderer/src/ReactTestHostConfig.js index f8d98bc0e18b8..0e4f1d8e0d437 100644 --- a/packages/react-test-renderer/src/ReactTestHostConfig.js +++ b/packages/react-test-renderer/src/ReactTestHostConfig.js @@ -7,7 +7,6 @@ * @flow */ -import * as Scheduler from 'scheduler/unstable_mock'; import warning from 'shared/warning'; export type Type = string; @@ -195,18 +194,10 @@ export function createTextInstance( } export const isPrimaryRenderer = false; -// This approach enables `now` to be mocked by tests, -// Even after the reconciler has initialized and read host config values. -export const now = Scheduler.unstable_now; -export const scheduleDeferredCallback = Scheduler.unstable_scheduleCallback; -export const cancelDeferredCallback = Scheduler.unstable_cancelCallback; -export const shouldYield = Scheduler.unstable_shouldYield; export const scheduleTimeout = setTimeout; export const cancelTimeout = clearTimeout; export const noTimeout = -1; -export const schedulePassiveEffects = Scheduler.unstable_scheduleCallback; -export const cancelPassiveEffects = Scheduler.unstable_cancelCallback; // ------------------- // Mutation diff --git a/packages/react/src/__tests__/ReactProfiler-test.internal.js b/packages/react/src/__tests__/ReactProfiler-test.internal.js index 7d8bcf65108cc..42fbb387dd096 100644 --- a/packages/react/src/__tests__/ReactProfiler-test.internal.js +++ b/packages/react/src/__tests__/ReactProfiler-test.internal.js @@ -261,7 +261,7 @@ describe('Profiler', () => { it('does not record times for components outside of Profiler tree', () => { // Mock the Scheduler module so we can track how many times the current // time is read - jest.mock('scheduler/unstable_mock', obj => { + jest.mock('scheduler', obj => { const ActualScheduler = require.requireActual( 'scheduler/unstable_mock', ); @@ -300,8 +300,10 @@ describe('Profiler', () => { 'read current time', ]); - // Remove mock - jest.unmock('scheduler/unstable_mock'); + // Restore original mock + jest.mock('scheduler', () => + require.requireActual('scheduler/unstable_mock'), + ); }); it('logs render times for both mount and update', () => {