| import PromiseQueue from 'next/dist/compiled/p-queue' |
| import type { RequestLifecycleOpts } from '../base-server' |
| import type { AfterCallback, AfterTask } from './after' |
| import { InvariantError } from '../../shared/lib/invariant-error' |
| import { isThenable } from '../../shared/lib/is-thenable' |
| import { workAsyncStorage } from '../app-render/work-async-storage.external' |
| import { withExecuteRevalidates } from '../revalidation-utils' |
| import { bindSnapshot } from '../app-render/async-local-storage' |
| import { |
| workUnitAsyncStorage, |
| type WorkUnitStore, |
| } from '../app-render/work-unit-async-storage.external' |
| import { afterTaskAsyncStorage } from '../app-render/after-task-async-storage.external' |
|
|
| export type AfterContextOpts = { |
| waitUntil: RequestLifecycleOpts['waitUntil'] | undefined |
| onClose: RequestLifecycleOpts['onClose'] |
| onTaskError: RequestLifecycleOpts['onAfterTaskError'] | undefined |
| } |
|
|
| export class AfterContext { |
| private waitUntil: RequestLifecycleOpts['waitUntil'] | undefined |
| private onClose: RequestLifecycleOpts['onClose'] |
| private onTaskError: RequestLifecycleOpts['onAfterTaskError'] | undefined |
|
|
| private runCallbacksOnClosePromise: Promise<void> | undefined |
| private callbackQueue: PromiseQueue |
| private workUnitStores = new Set<WorkUnitStore>() |
|
|
| constructor({ waitUntil, onClose, onTaskError }: AfterContextOpts) { |
| this.waitUntil = waitUntil |
| this.onClose = onClose |
| this.onTaskError = onTaskError |
|
|
| this.callbackQueue = new PromiseQueue() |
| this.callbackQueue.pause() |
| } |
|
|
| public after(task: AfterTask): void { |
| if (isThenable(task)) { |
| if (!this.waitUntil) { |
| errorWaitUntilNotAvailable() |
| } |
| this.waitUntil( |
| task.catch((error) => this.reportTaskError('promise', error)) |
| ) |
| } else if (typeof task === 'function') { |
| |
| this.addCallback(task) |
| } else { |
| throw new Error('`after()`: Argument must be a promise or a function') |
| } |
| } |
|
|
| private addCallback(callback: AfterCallback) { |
| |
| if (!this.waitUntil) { |
| errorWaitUntilNotAvailable() |
| } |
|
|
| const workUnitStore = workUnitAsyncStorage.getStore() |
| if (workUnitStore) { |
| this.workUnitStores.add(workUnitStore) |
| } |
|
|
| const afterTaskStore = afterTaskAsyncStorage.getStore() |
|
|
| |
| |
| |
| |
| const rootTaskSpawnPhase = afterTaskStore |
| ? afterTaskStore.rootTaskSpawnPhase |
| : workUnitStore?.phase |
|
|
| |
| if (!this.runCallbacksOnClosePromise) { |
| this.runCallbacksOnClosePromise = this.runCallbacksOnClose() |
| this.waitUntil(this.runCallbacksOnClosePromise) |
| } |
|
|
| |
| |
| |
| |
| |
| const wrappedCallback = bindSnapshot(async () => { |
| try { |
| await afterTaskAsyncStorage.run({ rootTaskSpawnPhase }, () => |
| callback() |
| ) |
| } catch (error) { |
| this.reportTaskError('function', error) |
| } |
| }) |
|
|
| this.callbackQueue.add(wrappedCallback) |
| } |
|
|
| private async runCallbacksOnClose() { |
| await new Promise<void>((resolve) => this.onClose!(resolve)) |
| return this.runCallbacks() |
| } |
|
|
| private async runCallbacks(): Promise<void> { |
| if (this.callbackQueue.size === 0) return |
|
|
| for (const workUnitStore of this.workUnitStores) { |
| workUnitStore.phase = 'after' |
| } |
|
|
| const workStore = workAsyncStorage.getStore() |
| if (!workStore) { |
| throw new InvariantError('Missing workStore in AfterContext.runCallbacks') |
| } |
|
|
| return withExecuteRevalidates(workStore, () => { |
| this.callbackQueue.start() |
| return this.callbackQueue.onIdle() |
| }) |
| } |
|
|
| private reportTaskError(taskKind: 'promise' | 'function', error: unknown) { |
| |
| |
| console.error( |
| taskKind === 'promise' |
| ? `A promise passed to \`after()\` rejected:` |
| : `An error occurred in a function passed to \`after()\`:`, |
| error |
| ) |
| if (this.onTaskError) { |
| |
| try { |
| this.onTaskError?.(error) |
| } catch (handlerError) { |
| console.error( |
| new InvariantError( |
| '`onTaskError` threw while handling an error thrown from an `after` task', |
| { |
| cause: handlerError, |
| } |
| ) |
| ) |
| } |
| } |
| } |
| } |
|
|
| function errorWaitUntilNotAvailable(): never { |
| throw new Error( |
| '`after()` will not work correctly, because `waitUntil` is not available in the current environment.' |
| ) |
| } |
|
|