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