From 02404d793b84c057e0f3f712f8ce565f81d4b7ab Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Fri, 1 Mar 2019 15:04:15 +0000 Subject: [PATCH] Avoid dynamic dispatch for scheduler calls (#14968) --- packages/react-art/src/ReactARTHostConfig.js | 22 ++++--- packages/react-cache/src/LRU.js | 6 +- .../src/client/ReactDOMHostConfig.js | 23 ++++--- .../src/ReactFiberScheduler.js | 24 ++++--- packages/react/src/ReactSharedInternals.js | 62 ++----------------- 5 files changed, 48 insertions(+), 89 deletions(-) diff --git a/packages/react-art/src/ReactARTHostConfig.js b/packages/react-art/src/ReactARTHostConfig.js index 5fc1810236da1..64114982cc379 100644 --- a/packages/react-art/src/ReactARTHostConfig.js +++ b/packages/react-art/src/ReactARTHostConfig.js @@ -5,22 +5,24 @@ * LICENSE file in the root directory of this source tree. */ -import { - unstable_scheduleCallback as scheduleDeferredCallback, - unstable_cancelCallback as cancelDeferredCallback, -} from 'scheduler'; -export { - unstable_now as now, - unstable_scheduleCallback as scheduleDeferredCallback, - unstable_shouldYield as shouldYield, - unstable_cancelCallback as cancelDeferredCallback, -} from 'scheduler'; import Transform from 'art/core/transform'; import Mode from 'art/modes/current'; +import * as Scheduler from 'scheduler'; import invariant from 'shared/invariant'; import {TYPES, EVENT_TYPES, childrenAsString} from './ReactARTInternals'; +// Intentionally not named imports because Rollup would +// use dynamic dispatch for CommonJS interop named imports. +const { + unstable_now: now, + unstable_scheduleCallback: scheduleDeferredCallback, + unstable_shouldYield: shouldYield, + unstable_cancelCallback: cancelDeferredCallback, +} = Scheduler; + +export {now, scheduleDeferredCallback, shouldYield, cancelDeferredCallback}; + const pooledTransform = new Transform(); const NO_CONTEXT = {}; diff --git a/packages/react-cache/src/LRU.js b/packages/react-cache/src/LRU.js index e4c0d2fa80c99..c12a0f6f4bea2 100644 --- a/packages/react-cache/src/LRU.js +++ b/packages/react-cache/src/LRU.js @@ -7,7 +7,11 @@ * @flow */ -import {unstable_scheduleCallback as scheduleCallback} from 'scheduler'; +import * as Scheduler from 'scheduler'; + +// Intentionally not named imports because Rollup would +// use dynamic dispatch for CommonJS interop named imports. +const {unstable_scheduleCallback: scheduleCallback} = Scheduler; type Entry = {| value: T, diff --git a/packages/react-dom/src/client/ReactDOMHostConfig.js b/packages/react-dom/src/client/ReactDOMHostConfig.js index 7196a28e80c8a..3467711921a1c 100644 --- a/packages/react-dom/src/client/ReactDOMHostConfig.js +++ b/packages/react-dom/src/client/ReactDOMHostConfig.js @@ -7,6 +7,8 @@ * @flow */ +import * as Scheduler from 'scheduler'; + import {precacheFiberNode, updateFiberProps} from './ReactDOMComponentTree'; import { createElement, @@ -70,17 +72,18 @@ export type ChildSet = void; // Unused export type TimeoutHandle = TimeoutID; export type NoTimeout = -1; -import { - unstable_scheduleCallback as scheduleDeferredCallback, - unstable_cancelCallback as cancelDeferredCallback, -} from 'scheduler'; import {enableSuspenseServerRenderer} from 'shared/ReactFeatureFlags'; -export { - unstable_now as now, - unstable_scheduleCallback as scheduleDeferredCallback, - unstable_shouldYield as shouldYield, - unstable_cancelCallback as cancelDeferredCallback, -} from 'scheduler'; + +// Intentionally not named imports because Rollup would +// use dynamic dispatch for CommonJS interop named imports. +const { + unstable_now: now, + unstable_scheduleCallback: scheduleDeferredCallback, + unstable_shouldYield: shouldYield, + unstable_cancelCallback: cancelDeferredCallback, +} = Scheduler; + +export {now, scheduleDeferredCallback, shouldYield, cancelDeferredCallback}; let SUPPRESS_HYDRATION_WARNING; if (__DEV__) { diff --git a/packages/react-reconciler/src/ReactFiberScheduler.js b/packages/react-reconciler/src/ReactFiberScheduler.js index 415d5bbc98ae2..e09ceb403027f 100644 --- a/packages/react-reconciler/src/ReactFiberScheduler.js +++ b/packages/react-reconciler/src/ReactFiberScheduler.js @@ -17,16 +17,7 @@ import { __subscriberRef, unstable_wrap as Scheduler_tracing_wrap, } from 'scheduler/tracing'; -import { - unstable_next as Scheduler_next, - unstable_getCurrentPriorityLevel as getCurrentPriorityLevel, - unstable_runWithPriority as runWithPriority, - unstable_ImmediatePriority as ImmediatePriority, - unstable_UserBlockingPriority as UserBlockingPriority, - unstable_NormalPriority as NormalPriority, - unstable_LowPriority as LowPriority, - unstable_IdlePriority as IdlePriority, -} from 'scheduler'; +import * as Scheduler from 'scheduler'; import { invokeGuardedCallback, hasCaughtError, @@ -181,6 +172,19 @@ export type Thenable = { then(resolve: () => mixed, reject?: () => mixed): mixed, }; +// Intentionally not named imports because Rollup would +// use dynamic dispatch for CommonJS interop named imports. +const { + unstable_next: Scheduler_next, + unstable_getCurrentPriorityLevel: getCurrentPriorityLevel, + unstable_runWithPriority: runWithPriority, + unstable_ImmediatePriority: ImmediatePriority, + unstable_UserBlockingPriority: UserBlockingPriority, + unstable_NormalPriority: NormalPriority, + unstable_LowPriority: LowPriority, + unstable_IdlePriority: IdlePriority, +} = Scheduler; + const {ReactCurrentDispatcher, ReactCurrentOwner} = ReactSharedInternals; let didWarnAboutStateTransition; diff --git a/packages/react/src/ReactSharedInternals.js b/packages/react/src/ReactSharedInternals.js index b4d1b9358c483..53dc43d977e51 100644 --- a/packages/react/src/ReactSharedInternals.js +++ b/packages/react/src/ReactSharedInternals.js @@ -6,35 +6,8 @@ */ import assign from 'object-assign'; -import { - unstable_cancelCallback, - unstable_shouldYield, - unstable_now, - unstable_scheduleCallback, - unstable_runWithPriority, - unstable_next, - unstable_getFirstCallbackNode, - unstable_pauseExecution, - unstable_continueExecution, - unstable_wrapCallback, - unstable_getCurrentPriorityLevel, - unstable_IdlePriority, - unstable_ImmediatePriority, - unstable_LowPriority, - unstable_NormalPriority, - unstable_UserBlockingPriority, -} from 'scheduler'; -import { - __interactionsRef, - __subscriberRef, - unstable_clear, - unstable_getCurrent, - unstable_getThreadID, - unstable_subscribe, - unstable_trace, - unstable_unsubscribe, - unstable_wrap, -} from 'scheduler/tracing'; +import * as Scheduler from 'scheduler'; +import * as SchedulerTracing from 'scheduler/tracing'; import ReactCurrentDispatcher from './ReactCurrentDispatcher'; import ReactCurrentOwner from './ReactCurrentOwner'; import ReactDebugCurrentFrame from './ReactDebugCurrentFrame'; @@ -53,35 +26,8 @@ if (__UMD__) { // This re-export is only required for UMD bundles; // CJS bundles use the shared NPM package. Object.assign(ReactSharedInternals, { - Scheduler: { - unstable_cancelCallback, - unstable_shouldYield, - unstable_now, - unstable_scheduleCallback, - unstable_runWithPriority, - unstable_next, - unstable_wrapCallback, - unstable_getFirstCallbackNode, - unstable_pauseExecution, - unstable_continueExecution, - unstable_getCurrentPriorityLevel, - unstable_IdlePriority, - unstable_ImmediatePriority, - unstable_LowPriority, - unstable_NormalPriority, - unstable_UserBlockingPriority, - }, - SchedulerTracing: { - __interactionsRef, - __subscriberRef, - unstable_clear, - unstable_getCurrent, - unstable_getThreadID, - unstable_subscribe, - unstable_trace, - unstable_unsubscribe, - unstable_wrap, - }, + Scheduler, + SchedulerTracing, }); }