| 133 | } |
| 134 | |
| 135 | export function createRenderLoop<S>(options: CreateRenderLoopOptions<S>): AppRenderLoop { |
| 136 | function advanceThemeTransitionFrame(): void { |
| 137 | const active = options.getThemeTransition(); |
| 138 | if (!active) return; |
| 139 | const nextFrame = active.frame + 1; |
| 140 | if (nextFrame >= active.totalFrames) { |
| 141 | options.setTheme(active.to); |
| 142 | options.setThemeTransition(null); |
| 143 | return; |
| 144 | } |
| 145 | options.setTheme(blendThemeColors(active.from, active.to, nextFrame / active.totalFrames)); |
| 146 | options.setThemeTransition( |
| 147 | Object.freeze({ |
| 148 | ...active, |
| 149 | frame: nextFrame, |
| 150 | }), |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | function scheduleThemeTransitionContinuation(): void { |
| 155 | if (!options.getThemeTransition() || !options.isRunning()) return; |
| 156 | options.markDirty(DIRTY_VIEW, false); |
| 157 | options.setRenderRequestQueuedForCurrentTurn(true); |
| 158 | options.enqueueWorkItem({ kind: "renderRequest" }); |
| 159 | } |
| 160 | |
| 161 | function scheduleFrameSettlement( |
| 162 | p: Promise<void>, |
| 163 | submitStart: number | null, |
| 164 | submitEnd: number | null, |
| 165 | ): void { |
| 166 | if (isSyncFrameAck(p)) { |
| 167 | if (PERF_ENABLED && submitStart !== null) { |
| 168 | const ackNow = perfNow(); |
| 169 | perfRecord("backend_ack", ackNow - submitStart); |
| 170 | if (submitEnd !== null) { |
| 171 | perfRecord("frame_build", submitEnd - submitStart); |
| 172 | perfRecord("worker_roundtrip", ackNow - submitEnd); |
| 173 | } |
| 174 | } |
| 175 | options.setFramesInFlight(Math.max(0, options.getFramesInFlight() - 1)); |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | const acceptedAck = getAcceptedFrameAck(p); |
| 180 | const ackPromise = acceptedAck ?? p; |
| 181 | |
| 182 | void ackPromise.then( |
| 183 | () => { |
| 184 | if (PERF_ENABLED && submitStart !== null) { |
| 185 | const ackNow = perfNow(); |
| 186 | perfRecord("backend_ack", ackNow - submitStart); |
| 187 | if (submitEnd !== null) { |
| 188 | perfRecord("frame_build", submitEnd - submitStart); |
| 189 | perfRecord("worker_roundtrip", ackNow - submitEnd); |
| 190 | } |
| 191 | } |
| 192 | options.enqueueWorkItem({ kind: "frameDone" }); |