From 16443f78934e9f2162656750791cc62e0d120035 Mon Sep 17 00:00:00 2001 From: Andrew Clark Date: Fri, 18 Jan 2019 17:38:14 -0800 Subject: [PATCH] Add Scheduler.unstable_next --- packages/react/src/ReactSharedInternals.js | 2 ++ .../npm/umd/scheduler.development.js | 8 +++++ .../npm/umd/scheduler.production.min.js | 8 +++++ .../npm/umd/scheduler.profiling.min.js | 8 +++++ packages/scheduler/src/Scheduler.js | 32 +++++++++++++++++++ 5 files changed, 58 insertions(+) diff --git a/packages/react/src/ReactSharedInternals.js b/packages/react/src/ReactSharedInternals.js index 9cdce1891bc5c..1fe0c2391bd13 100644 --- a/packages/react/src/ReactSharedInternals.js +++ b/packages/react/src/ReactSharedInternals.js @@ -12,6 +12,7 @@ import { unstable_now, unstable_scheduleCallback, unstable_runWithPriority, + unstable_next, unstable_getFirstCallbackNode, unstable_pauseExecution, unstable_continueExecution, @@ -53,6 +54,7 @@ if (__UMD__) { unstable_now, unstable_scheduleCallback, unstable_runWithPriority, + unstable_next, unstable_wrapCallback, unstable_getFirstCallbackNode, unstable_pauseExecution, diff --git a/packages/scheduler/npm/umd/scheduler.development.js b/packages/scheduler/npm/umd/scheduler.development.js index 41ac8e437bbd6..ac632eb288bff 100644 --- a/packages/scheduler/npm/umd/scheduler.development.js +++ b/packages/scheduler/npm/umd/scheduler.development.js @@ -54,6 +54,13 @@ ); } + function unstable_next() { + return global.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Scheduler.unstable_next.apply( + this, + arguments + ); + } + function unstable_wrapCallback() { return global.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Scheduler.unstable_wrapCallback.apply( this, @@ -95,6 +102,7 @@ unstable_cancelCallback: unstable_cancelCallback, unstable_shouldYield: unstable_shouldYield, unstable_runWithPriority: unstable_runWithPriority, + unstable_next: unstable_next, unstable_wrapCallback: unstable_wrapCallback, unstable_getCurrentPriorityLevel: unstable_getCurrentPriorityLevel, unstable_continueExecution: unstable_continueExecution, diff --git a/packages/scheduler/npm/umd/scheduler.production.min.js b/packages/scheduler/npm/umd/scheduler.production.min.js index cea54f4da3cba..da2aefa9e4bf1 100644 --- a/packages/scheduler/npm/umd/scheduler.production.min.js +++ b/packages/scheduler/npm/umd/scheduler.production.min.js @@ -54,6 +54,13 @@ ); } + function unstable_next() { + return global.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Scheduler.unstable_next.apply( + this, + arguments + ); + } + function unstable_wrapCallback() { return global.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Scheduler.unstable_wrapCallback.apply( this, @@ -89,6 +96,7 @@ unstable_cancelCallback: unstable_cancelCallback, unstable_shouldYield: unstable_shouldYield, unstable_runWithPriority: unstable_runWithPriority, + unstable_next: unstable_next, unstable_wrapCallback: unstable_wrapCallback, unstable_getCurrentPriorityLevel: unstable_getCurrentPriorityLevel, unstable_continueExecution: unstable_continueExecution, diff --git a/packages/scheduler/npm/umd/scheduler.profiling.min.js b/packages/scheduler/npm/umd/scheduler.profiling.min.js index cea54f4da3cba..da2aefa9e4bf1 100644 --- a/packages/scheduler/npm/umd/scheduler.profiling.min.js +++ b/packages/scheduler/npm/umd/scheduler.profiling.min.js @@ -54,6 +54,13 @@ ); } + function unstable_next() { + return global.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Scheduler.unstable_next.apply( + this, + arguments + ); + } + function unstable_wrapCallback() { return global.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Scheduler.unstable_wrapCallback.apply( this, @@ -89,6 +96,7 @@ unstable_cancelCallback: unstable_cancelCallback, unstable_shouldYield: unstable_shouldYield, unstable_runWithPriority: unstable_runWithPriority, + unstable_next: unstable_next, unstable_wrapCallback: unstable_wrapCallback, unstable_getCurrentPriorityLevel: unstable_getCurrentPriorityLevel, unstable_continueExecution: unstable_continueExecution, diff --git a/packages/scheduler/src/Scheduler.js b/packages/scheduler/src/Scheduler.js index a6e27850dab71..df1e9b3bdada0 100644 --- a/packages/scheduler/src/Scheduler.js +++ b/packages/scheduler/src/Scheduler.js @@ -264,6 +264,37 @@ function unstable_runWithPriority(priorityLevel, eventHandler) { } } +function unstable_next(eventHandler) { + let priorityLevel; + switch (currentPriorityLevel) { + case ImmediatePriority: + case UserBlockingPriority: + case NormalPriority: + // Shift down to normal priority + priorityLevel = NormalPriority; + break; + default: + // Anything lower than normal priority should remain at the current level. + priorityLevel = currentPriorityLevel; + break; + } + + var previousPriorityLevel = currentPriorityLevel; + var previousEventStartTime = currentEventStartTime; + currentPriorityLevel = priorityLevel; + currentEventStartTime = getCurrentTime(); + + try { + return eventHandler(); + } finally { + currentPriorityLevel = previousPriorityLevel; + currentEventStartTime = previousEventStartTime; + + // Before exiting, flush all the immediate work that was scheduled. + flushImmediateWork(); + } +} + function unstable_wrapCallback(callback) { var parentPriorityLevel = currentPriorityLevel; return function() { @@ -688,6 +719,7 @@ export { IdlePriority as unstable_IdlePriority, LowPriority as unstable_LowPriority, unstable_runWithPriority, + unstable_next, unstable_scheduleCallback, unstable_cancelCallback, unstable_wrapCallback,