{"version":3,"sources":["../../../src/server/app-render/stale-time.ts"],"sourcesContent":["import type { ExperimentalConfig } from '../config-shared'\nimport { INFINITE_CACHE } from '../../lib/constants'\n\n/**\n * An AsyncIterable<number> that yields staleTime values. Each call to\n * `update()` yields the new value. When `close()` is called, the iteration\n * ends.\n *\n * This is included in the RSC payload so Flight serializes each yielded value\n * into the stream immediately. If the prerender is aborted by sync IO, the last\n * yielded value is already in the stream, allowing the prerender to be aborted\n * synchronously.\n */\nexport class StaleTimeIterable {\n  private _resolve: ((result: IteratorResult<number>) => void) | null = null\n  private _done = false\n  private _buffer: number[] = []\n\n  /** The last value passed to `update()`. */\n  public currentValue: number = 0\n\n  update(value: number): void {\n    if (this._done) return\n    this.currentValue = value\n    if (this._resolve) {\n      this._resolve({ value, done: false })\n      this._resolve = null\n    } else {\n      this._buffer.push(value)\n    }\n  }\n\n  close(): void {\n    if (this._done) return\n    this._done = true\n    if (this._resolve) {\n      this._resolve({ value: undefined, done: true })\n      this._resolve = null\n    }\n  }\n\n  [Symbol.asyncIterator](): AsyncIterator<number> {\n    return {\n      next: () => {\n        if (this._buffer.length > 0) {\n          return Promise.resolve({ value: this._buffer.shift()!, done: false })\n        }\n        if (this._done) {\n          return Promise.resolve({ value: undefined, done: true })\n        }\n        return new Promise<IteratorResult<number>>((resolve) => {\n          this._resolve = resolve\n        })\n      },\n    }\n  }\n}\n\nexport function createSelectStaleTime(experimental: ExperimentalConfig) {\n  return (stale: number) =>\n    stale === INFINITE_CACHE &&\n    typeof experimental.staleTimes?.static === 'number'\n      ? experimental.staleTimes.static\n      : stale\n}\n\n/**\n * Intercepts writes to the `stale` field on the prerender store and yields\n * each update (after applying selectStaleTime) through the iterable. This\n * ensures the latest stale time is always serialized in the Flight stream,\n * even if the prerender is aborted by sync IO.\n */\nexport function trackStaleTime(\n  store: { stale: number },\n  iterable: StaleTimeIterable,\n  selectStaleTime: (stale: number) => number\n): void {\n  let _stale = store.stale\n  iterable.update(selectStaleTime(_stale))\n  Object.defineProperty(store, 'stale', {\n    get: () => _stale,\n    set: (value: number) => {\n      _stale = value\n      iterable.update(selectStaleTime(value))\n    },\n    configurable: true,\n    enumerable: true,\n  })\n}\n\n/**\n * Closes the stale time iterable and waits for React to flush the closing\n * chunk into the Flight stream. This also allows the prerender to complete if\n * no other work is pending.\n *\n * Flight's internal work gets scheduled as a microtask when we close the\n * iterable. We need to ensure Flight's pending queues are emptied before this\n * function returns, because the caller will abort the prerender immediately\n * after. We can't use a macrotask (that would allow dynamic IO to sneak into\n * the response), so we use microtasks instead. The exact number of awaits\n * isn't important as long as we wait enough ticks for Flight to finish writing.\n */\nexport async function finishStaleTimeTracking(\n  iterable: StaleTimeIterable\n): Promise<void> {\n  iterable.close()\n  await Promise.resolve()\n  await Promise.resolve()\n  await Promise.resolve()\n}\n"],"names":["StaleTimeIterable","createSelectStaleTime","finishStaleTimeTracking","trackStaleTime","update","value","_done","currentValue","_resolve","done","_buffer","push","close","undefined","Symbol","asyncIterator","next","length","Promise","resolve","shift","experimental","stale","INFINITE_CACHE","staleTimes","static","store","iterable","selectStaleTime","_stale","Object","defineProperty","get","set","configurable","enumerable"],"mappings":";;;;;;;;;;;;;;;;;IAaaA,iBAAiB;eAAjBA;;IA6CGC,qBAAqB;eAArBA;;IA4CMC,uBAAuB;eAAvBA;;IA9BNC,cAAc;eAAdA;;;2BAvEe;AAYxB,MAAMH;IAQXI,OAAOC,KAAa,EAAQ;QAC1B,IAAI,IAAI,CAACC,KAAK,EAAE;QAChB,IAAI,CAACC,YAAY,GAAGF;QACpB,IAAI,IAAI,CAACG,QAAQ,EAAE;YACjB,IAAI,CAACA,QAAQ,CAAC;gBAAEH;gBAAOI,MAAM;YAAM;YACnC,IAAI,CAACD,QAAQ,GAAG;QAClB,OAAO;YACL,IAAI,CAACE,OAAO,CAACC,IAAI,CAACN;QACpB;IACF;IAEAO,QAAc;QACZ,IAAI,IAAI,CAACN,KAAK,EAAE;QAChB,IAAI,CAACA,KAAK,GAAG;QACb,I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