[d565449] | 1 | /**
|
---|
| 2 | * @license React
|
---|
| 3 | * scheduler-unstable_post_task.development.js
|
---|
| 4 | *
|
---|
| 5 | * Copyright (c) Facebook, Inc. and its affiliates.
|
---|
| 6 | *
|
---|
| 7 | * This source code is licensed under the MIT license found in the
|
---|
| 8 | * LICENSE file in the root directory of this source tree.
|
---|
| 9 | */
|
---|
| 10 |
|
---|
| 11 | 'use strict';
|
---|
| 12 |
|
---|
| 13 | if (process.env.NODE_ENV !== "production") {
|
---|
| 14 | (function() {
|
---|
| 15 | 'use strict';
|
---|
| 16 |
|
---|
| 17 | // TODO: Use symbols?
|
---|
| 18 | var ImmediatePriority = 1;
|
---|
| 19 | var UserBlockingPriority = 2;
|
---|
| 20 | var NormalPriority = 3;
|
---|
| 21 | var LowPriority = 4;
|
---|
| 22 | var IdlePriority = 5;
|
---|
| 23 |
|
---|
| 24 | var perf = window.performance;
|
---|
| 25 | var setTimeout = window.setTimeout; // Use experimental Chrome Scheduler postTask API.
|
---|
| 26 |
|
---|
| 27 | var scheduler = global.scheduler;
|
---|
| 28 | var getCurrentTime = perf.now.bind(perf);
|
---|
| 29 | var unstable_now = getCurrentTime; // Scheduler periodically yields in case there is other work on the main
|
---|
| 30 | // thread, like user events. By default, it yields multiple times per frame.
|
---|
| 31 | // It does not attempt to align with frame boundaries, since most tasks don't
|
---|
| 32 | // need to be frame aligned; for those that do, use requestAnimationFrame.
|
---|
| 33 |
|
---|
| 34 | var yieldInterval = 5;
|
---|
| 35 | var deadline = 0;
|
---|
| 36 | var currentPriorityLevel_DEPRECATED = NormalPriority; // `isInputPending` is not available. Since we have no way of knowing if
|
---|
| 37 | // there's pending input, always yield at the end of the frame.
|
---|
| 38 |
|
---|
| 39 | function unstable_shouldYield() {
|
---|
| 40 | return getCurrentTime() >= deadline;
|
---|
| 41 | }
|
---|
| 42 | function unstable_requestPaint() {// Since we yield every frame regardless, `requestPaint` has no effect.
|
---|
| 43 | }
|
---|
| 44 | function unstable_scheduleCallback(priorityLevel, callback, options) {
|
---|
| 45 | var postTaskPriority;
|
---|
| 46 |
|
---|
| 47 | switch (priorityLevel) {
|
---|
| 48 | case ImmediatePriority:
|
---|
| 49 | case UserBlockingPriority:
|
---|
| 50 | postTaskPriority = 'user-blocking';
|
---|
| 51 | break;
|
---|
| 52 |
|
---|
| 53 | case LowPriority:
|
---|
| 54 | case NormalPriority:
|
---|
| 55 | postTaskPriority = 'user-visible';
|
---|
| 56 | break;
|
---|
| 57 |
|
---|
| 58 | case IdlePriority:
|
---|
| 59 | postTaskPriority = 'background';
|
---|
| 60 | break;
|
---|
| 61 |
|
---|
| 62 | default:
|
---|
| 63 | postTaskPriority = 'user-visible';
|
---|
| 64 | break;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | var controller = new TaskController();
|
---|
| 68 | var postTaskOptions = {
|
---|
| 69 | priority: postTaskPriority,
|
---|
| 70 | delay: typeof options === 'object' && options !== null ? options.delay : 0,
|
---|
| 71 | signal: controller.signal
|
---|
| 72 | };
|
---|
| 73 | var node = {
|
---|
| 74 | _controller: controller
|
---|
| 75 | };
|
---|
| 76 | scheduler.postTask(runTask.bind(null, priorityLevel, postTaskPriority, node, callback), postTaskOptions).catch(handleAbortError);
|
---|
| 77 | return node;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | function runTask(priorityLevel, postTaskPriority, node, callback) {
|
---|
| 81 | deadline = getCurrentTime() + yieldInterval;
|
---|
| 82 |
|
---|
| 83 | try {
|
---|
| 84 | currentPriorityLevel_DEPRECATED = priorityLevel;
|
---|
| 85 | var _didTimeout_DEPRECATED = false;
|
---|
| 86 | var result = callback(_didTimeout_DEPRECATED);
|
---|
| 87 |
|
---|
| 88 | if (typeof result === 'function') {
|
---|
| 89 | // Assume this is a continuation
|
---|
| 90 | var continuation = result;
|
---|
| 91 | var continuationController = new TaskController();
|
---|
| 92 | var continuationOptions = {
|
---|
| 93 | priority: postTaskPriority,
|
---|
| 94 | signal: continuationController.signal
|
---|
| 95 | }; // Update the original callback node's controller, since even though we're
|
---|
| 96 | // posting a new task, conceptually it's the same one.
|
---|
| 97 |
|
---|
| 98 | node._controller = continuationController;
|
---|
| 99 | scheduler.postTask(runTask.bind(null, priorityLevel, postTaskPriority, node, continuation), continuationOptions).catch(handleAbortError);
|
---|
| 100 | }
|
---|
| 101 | } catch (error) {
|
---|
| 102 | // We're inside a `postTask` promise. If we don't handle this error, then it
|
---|
| 103 | // will trigger an "Unhandled promise rejection" error. We don't want that,
|
---|
| 104 | // but we do want the default error reporting behavior that normal
|
---|
| 105 | // (non-Promise) tasks get for unhandled errors.
|
---|
| 106 | //
|
---|
| 107 | // So we'll re-throw the error inside a regular browser task.
|
---|
| 108 | setTimeout(function () {
|
---|
| 109 | throw error;
|
---|
| 110 | });
|
---|
| 111 | } finally {
|
---|
| 112 | currentPriorityLevel_DEPRECATED = NormalPriority;
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | function handleAbortError(error) {// Abort errors are an implementation detail. We don't expose the
|
---|
| 117 | // TaskController to the user, nor do we expose the promise that is returned
|
---|
| 118 | // from `postTask`. So we should suppress them, since there's no way for the
|
---|
| 119 | // user to handle them.
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | function unstable_cancelCallback(node) {
|
---|
| 123 | var controller = node._controller;
|
---|
| 124 | controller.abort();
|
---|
| 125 | }
|
---|
| 126 | function unstable_runWithPriority(priorityLevel, callback) {
|
---|
| 127 | var previousPriorityLevel = currentPriorityLevel_DEPRECATED;
|
---|
| 128 | currentPriorityLevel_DEPRECATED = priorityLevel;
|
---|
| 129 |
|
---|
| 130 | try {
|
---|
| 131 | return callback();
|
---|
| 132 | } finally {
|
---|
| 133 | currentPriorityLevel_DEPRECATED = previousPriorityLevel;
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 | function unstable_getCurrentPriorityLevel() {
|
---|
| 137 | return currentPriorityLevel_DEPRECATED;
|
---|
| 138 | }
|
---|
| 139 | function unstable_next(callback) {
|
---|
| 140 | var priorityLevel;
|
---|
| 141 |
|
---|
| 142 | switch (currentPriorityLevel_DEPRECATED) {
|
---|
| 143 | case ImmediatePriority:
|
---|
| 144 | case UserBlockingPriority:
|
---|
| 145 | case NormalPriority:
|
---|
| 146 | // Shift down to normal priority
|
---|
| 147 | priorityLevel = NormalPriority;
|
---|
| 148 | break;
|
---|
| 149 |
|
---|
| 150 | default:
|
---|
| 151 | // Anything lower than normal priority should remain at the current level.
|
---|
| 152 | priorityLevel = currentPriorityLevel_DEPRECATED;
|
---|
| 153 | break;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | var previousPriorityLevel = currentPriorityLevel_DEPRECATED;
|
---|
| 157 | currentPriorityLevel_DEPRECATED = priorityLevel;
|
---|
| 158 |
|
---|
| 159 | try {
|
---|
| 160 | return callback();
|
---|
| 161 | } finally {
|
---|
| 162 | currentPriorityLevel_DEPRECATED = previousPriorityLevel;
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 | function unstable_wrapCallback(callback) {
|
---|
| 166 | var parentPriorityLevel = currentPriorityLevel_DEPRECATED;
|
---|
| 167 | return function () {
|
---|
| 168 | var previousPriorityLevel = currentPriorityLevel_DEPRECATED;
|
---|
| 169 | currentPriorityLevel_DEPRECATED = parentPriorityLevel;
|
---|
| 170 |
|
---|
| 171 | try {
|
---|
| 172 | return callback();
|
---|
| 173 | } finally {
|
---|
| 174 | currentPriorityLevel_DEPRECATED = previousPriorityLevel;
|
---|
| 175 | }
|
---|
| 176 | };
|
---|
| 177 | }
|
---|
| 178 | function unstable_forceFrameRate() {}
|
---|
| 179 | function unstable_pauseExecution() {}
|
---|
| 180 | function unstable_continueExecution() {}
|
---|
| 181 | function unstable_getFirstCallbackNode() {
|
---|
| 182 | return null;
|
---|
| 183 | } // Currently no profiling build
|
---|
| 184 |
|
---|
| 185 | var unstable_Profiling = null;
|
---|
| 186 |
|
---|
| 187 | exports.unstable_IdlePriority = IdlePriority;
|
---|
| 188 | exports.unstable_ImmediatePriority = ImmediatePriority;
|
---|
| 189 | exports.unstable_LowPriority = LowPriority;
|
---|
| 190 | exports.unstable_NormalPriority = NormalPriority;
|
---|
| 191 | exports.unstable_Profiling = unstable_Profiling;
|
---|
| 192 | exports.unstable_UserBlockingPriority = UserBlockingPriority;
|
---|
| 193 | exports.unstable_cancelCallback = unstable_cancelCallback;
|
---|
| 194 | exports.unstable_continueExecution = unstable_continueExecution;
|
---|
| 195 | exports.unstable_forceFrameRate = unstable_forceFrameRate;
|
---|
| 196 | exports.unstable_getCurrentPriorityLevel = unstable_getCurrentPriorityLevel;
|
---|
| 197 | exports.unstable_getFirstCallbackNode = unstable_getFirstCallbackNode;
|
---|
| 198 | exports.unstable_next = unstable_next;
|
---|
| 199 | exports.unstable_now = unstable_now;
|
---|
| 200 | exports.unstable_pauseExecution = unstable_pauseExecution;
|
---|
| 201 | exports.unstable_requestPaint = unstable_requestPaint;
|
---|
| 202 | exports.unstable_runWithPriority = unstable_runWithPriority;
|
---|
| 203 | exports.unstable_scheduleCallback = unstable_scheduleCallback;
|
---|
| 204 | exports.unstable_shouldYield = unstable_shouldYield;
|
---|
| 205 | exports.unstable_wrapCallback = unstable_wrapCallback;
|
---|
| 206 | })();
|
---|
| 207 | }
|
---|