|
7 | 7 | * @flow |
8 | 8 | */ |
9 | 9 |
|
10 | | -export * from './ScheduleDOM'; |
| 10 | +import { |
| 11 | + requestWork, |
| 12 | + cancelWork, |
| 13 | + getCurrentTime, |
| 14 | + getTimeRemainingInFrame, |
| 15 | + isRemainingTimeInFrame, |
| 16 | +} from './ScheduleDOM'; |
| 17 | + |
| 18 | +type Options = { |
| 19 | + timeout?: number, |
| 20 | +}; |
| 21 | + |
| 22 | +opaque type CallbackNode = {| |
| 23 | + callback: () => mixed, |
| 24 | + timesOutAt: number, |
| 25 | + next: CallbackNode, |
| 26 | + previous: CallbackNode, |
| 27 | +|}; |
| 28 | + |
| 29 | +// TODO: Currently there's only a single priority level, Deferred. Need to add |
| 30 | +// additional priorities (serial, offscreen). |
| 31 | +type Priority = 0; |
| 32 | + |
| 33 | +const Deferred = 0; |
| 34 | + |
| 35 | +const DEFERRED_TIMEOUT = 5000; |
| 36 | + |
| 37 | +// Callbacks are stored as a circular, doubly linked list. |
| 38 | +let firstCallbackNode: CallbackNode | null = null; |
| 39 | + |
| 40 | +let priorityContext: Priority = Deferred; |
| 41 | +let isPerformingWork: boolean = false; |
| 42 | + |
| 43 | +let isHostCallbackScheduled: boolean = false; |
| 44 | + |
| 45 | +const deadlineObject = { |
| 46 | + timeRemaining: getTimeRemainingInFrame, |
| 47 | + didTimeout: false, |
| 48 | +}; |
| 49 | + |
| 50 | +function ensureHostCallbackIsScheduled(highestPriorityNode) { |
| 51 | + if (isPerformingWork) { |
| 52 | + // Don't schedule work yet; wait until the next time we yield. |
| 53 | + return; |
| 54 | + } |
| 55 | + const timesOutAt = highestPriorityNode.timesOutAt; |
| 56 | + if (!isHostCallbackScheduled) { |
| 57 | + isHostCallbackScheduled = true; |
| 58 | + } else { |
| 59 | + // Cancel the existing work. |
| 60 | + cancelWork(); |
| 61 | + } |
| 62 | + // Schedule work using the highest priority callback's timeout. |
| 63 | + requestWork(flushWork, timesOutAt); |
| 64 | +} |
| 65 | + |
| 66 | +function computeAbsoluteTimeoutForPriority(currentTime, priority) { |
| 67 | + if (priority === Deferred) { |
| 68 | + return currentTime + DEFERRED_TIMEOUT; |
| 69 | + } |
| 70 | + throw new Error('Not yet implemented.'); |
| 71 | +} |
| 72 | + |
| 73 | +function flushCallback(node) { |
| 74 | + // This is already true; only assigning to appease Flow. |
| 75 | + firstCallbackNode = node; |
| 76 | + |
| 77 | + // Remove the node from the list before calling the callback. That way the |
| 78 | + // list is in a consistent state even if the callback throws. |
| 79 | + const next = firstCallbackNode.next; |
| 80 | + if (firstCallbackNode === next) { |
| 81 | + // This is the last callback in the list. |
| 82 | + firstCallbackNode = null; |
| 83 | + } else { |
| 84 | + const previous = firstCallbackNode.previous; |
| 85 | + firstCallbackNode = previous.next = next; |
| 86 | + next.previous = previous; |
| 87 | + } |
| 88 | + |
| 89 | + node.next = node.previous = null; |
| 90 | + |
| 91 | + // Now it's safe to call the callback. |
| 92 | + const callback = node.callback; |
| 93 | + callback(deadlineObject); |
| 94 | +} |
| 95 | + |
| 96 | +function flushWork(didTimeout) { |
| 97 | + isPerformingWork = true; |
| 98 | + deadlineObject.didTimeout = didTimeout; |
| 99 | + try { |
| 100 | + if (firstCallbackNode !== null) { |
| 101 | + if (didTimeout) { |
| 102 | + // Flush all the timed out callbacks without yielding. |
| 103 | + do { |
| 104 | + flushCallback(firstCallbackNode); |
| 105 | + } while ( |
| 106 | + firstCallbackNode !== null && |
| 107 | + firstCallbackNode.timesOutAt <= getCurrentTime() |
| 108 | + ); |
| 109 | + } else { |
| 110 | + // Keep flushing callbacks until we run out of time in the frame. |
| 111 | + while (firstCallbackNode !== null && isRemainingTimeInFrame()) { |
| 112 | + flushCallback(firstCallbackNode); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } finally { |
| 117 | + isPerformingWork = false; |
| 118 | + if (firstCallbackNode !== null) { |
| 119 | + // There's still work remaining. Request another callback. |
| 120 | + ensureHostCallbackIsScheduled(firstCallbackNode); |
| 121 | + } else { |
| 122 | + isHostCallbackScheduled = false; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +export function unstable_scheduleWork( |
| 128 | + callback: () => mixed, |
| 129 | + options?: Options, |
| 130 | +): CallbackNode { |
| 131 | + const currentTime = getCurrentTime(); |
| 132 | + |
| 133 | + let timesOutAt; |
| 134 | + if (options !== undefined && options !== null) { |
| 135 | + const timeoutOption = options.timeout; |
| 136 | + if (timeoutOption !== null && timeoutOption !== undefined) { |
| 137 | + // If an explicit timeout is provided, it takes precedence over the |
| 138 | + // priority context. |
| 139 | + timesOutAt = currentTime + timeoutOption; |
| 140 | + } else { |
| 141 | + // Compute an absolute timeout using the current priority context. |
| 142 | + timesOutAt = computeAbsoluteTimeoutForPriority( |
| 143 | + currentTime, |
| 144 | + priorityContext, |
| 145 | + ); |
| 146 | + } |
| 147 | + } else { |
| 148 | + timesOutAt = computeAbsoluteTimeoutForPriority( |
| 149 | + currentTime, |
| 150 | + priorityContext, |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + const newNode: CallbackNode = { |
| 155 | + callback, |
| 156 | + timesOutAt, |
| 157 | + next: (null: any), |
| 158 | + previous: (null: any), |
| 159 | + }; |
| 160 | + |
| 161 | + // Insert the new callback into the list, sorted by its timeout. |
| 162 | + if (firstCallbackNode === null) { |
| 163 | + // This is the first callback in the list. |
| 164 | + firstCallbackNode = newNode.next = newNode.previous = newNode; |
| 165 | + ensureHostCallbackIsScheduled(firstCallbackNode); |
| 166 | + } else { |
| 167 | + let next = null; |
| 168 | + let node = firstCallbackNode; |
| 169 | + do { |
| 170 | + if (node.timesOutAt > timesOutAt) { |
| 171 | + // This callback is lower priority than the new one. |
| 172 | + next = node; |
| 173 | + break; |
| 174 | + } |
| 175 | + node = node.next; |
| 176 | + } while (node !== firstCallbackNode); |
| 177 | + |
| 178 | + if (next === null) { |
| 179 | + // No lower priority callback was found, which means the new callback is |
| 180 | + // the lowest priority callback in the list. |
| 181 | + next = firstCallbackNode; |
| 182 | + } else if (next === firstCallbackNode) { |
| 183 | + // The new callback is the highest priority callback in the list. |
| 184 | + firstCallbackNode = newNode; |
| 185 | + ensureHostCallbackIsScheduled(firstCallbackNode); |
| 186 | + } |
| 187 | + |
| 188 | + const previous = next.previous; |
| 189 | + previous.next = next.previous = newNode; |
| 190 | + newNode.next = next; |
| 191 | + newNode.previous = previous; |
| 192 | + } |
| 193 | + |
| 194 | + return newNode; |
| 195 | +} |
| 196 | + |
| 197 | +export function unstable_cancelScheduledWork(callbackNode: CallbackNode): void { |
| 198 | + const next = callbackNode.next; |
| 199 | + if (next === null) { |
| 200 | + // Already cancelled. |
| 201 | + return; |
| 202 | + } |
| 203 | + |
| 204 | + if (next === callbackNode) { |
| 205 | + // This is the only scheduled callback. Clear the list. |
| 206 | + firstCallbackNode = null; |
| 207 | + } else { |
| 208 | + // Remove the callback from its position in the list. |
| 209 | + if (callbackNode === firstCallbackNode) { |
| 210 | + firstCallbackNode = next; |
| 211 | + } |
| 212 | + const previous = callbackNode.previous; |
| 213 | + previous.next = next; |
| 214 | + next.previous = previous; |
| 215 | + } |
| 216 | + |
| 217 | + callbackNode.next = callbackNode.previous = (null: any); |
| 218 | +} |
0 commit comments