Commit 31f18201 authored by Janka Uryga's avatar Janka Uryga

refactor types for dev-only RequestStore properties

parent 66c194a5
...@@ -16,6 +16,7 @@ import { ...@@ -16,6 +16,7 @@ import {
type WorkStore, type WorkStore,
} from '../app-render/work-async-storage.external' } from '../app-render/work-async-storage.external'
import type { import type {
DevStoreModernPartial,
PrerenderStoreModernRuntime, PrerenderStoreModernRuntime,
RequestStore, RequestStore,
} from '../app-render/work-unit-async-storage.external' } from '../app-render/work-unit-async-storage.external'
...@@ -2847,18 +2848,20 @@ async function renderWithRestartOnCacheMissInDev( ...@@ -2847,18 +2848,20 @@ async function renderWithRestartOnCacheMissInDev(
initialHangingPromiseController.signal initialHangingPromiseController.signal
) )
requestStore.prerenderResumeDataCache = prerenderResumeDataCache
// `getRenderResumeDataCache` will fall back to using `prerenderResumeDataCache` as `renderResumeDataCache`, // `getRenderResumeDataCache` will fall back to using `prerenderResumeDataCache` as `renderResumeDataCache`,
// so not having a resume data cache won't break any expectations in case we don't need to restart. // so not having a resume data cache won't break any expectations in case we don't need to restart.
requestStore.renderResumeDataCache = null requestStore.renderResumeDataCache = null
requestStore.stagedRendering = initialStageController Object.assign(requestStore, {
requestStore.asyncApiPromises = createAsyncApiPromisesInDev( stagedRendering: initialStageController,
initialStageController, asyncApiPromises: createAsyncApiPromisesInDev(
requestStore.cookies, initialStageController,
requestStore.mutableCookies, requestStore.cookies,
requestStore.headers requestStore.mutableCookies,
) requestStore.headers
requestStore.cacheSignal = cacheSignal ),
prerenderResumeDataCache,
cacheSignal,
} satisfies DevStoreModernPartial)
let debugChannel = setReactDebugChannel && createDebugChannel() let debugChannel = setReactDebugChannel && createDebugChannel()
...@@ -2963,18 +2966,20 @@ async function renderWithRestartOnCacheMissInDev( ...@@ -2963,18 +2966,20 @@ async function renderWithRestartOnCacheMissInDev(
// We've filled the caches, so now we can render as usual, // We've filled the caches, so now we can render as usual,
// without any cache-filling mechanics. // without any cache-filling mechanics.
requestStore.prerenderResumeDataCache = null
requestStore.renderResumeDataCache = createRenderResumeDataCache( requestStore.renderResumeDataCache = createRenderResumeDataCache(
prerenderResumeDataCache prerenderResumeDataCache
) )
requestStore.stagedRendering = finalStageController Object.assign(requestStore, {
requestStore.cacheSignal = null stagedRendering: finalStageController,
requestStore.asyncApiPromises = createAsyncApiPromisesInDev( asyncApiPromises: createAsyncApiPromisesInDev(
finalStageController, finalStageController,
requestStore.cookies, requestStore.cookies,
requestStore.mutableCookies, requestStore.mutableCookies,
requestStore.headers requestStore.headers
) ),
prerenderResumeDataCache: null,
cacheSignal: null,
} satisfies DevStoreModernPartial)
// The initial render already wrote to its debug channel. // The initial render already wrote to its debug channel.
// We're not using it, so we need to create a new one. // We're not using it, so we need to create a new one.
......
...@@ -29,7 +29,7 @@ export interface CommonWorkUnitStore { ...@@ -29,7 +29,7 @@ export interface CommonWorkUnitStore {
readonly implicitTags: ImplicitTags readonly implicitTags: ImplicitTags
} }
export interface RequestStore extends CommonWorkUnitStore { interface BaseRequestStore extends CommonWorkUnitStore {
readonly type: 'request' readonly type: 'request'
/** /**
...@@ -65,14 +65,46 @@ export interface RequestStore extends CommonWorkUnitStore { ...@@ -65,14 +65,46 @@ export interface RequestStore extends CommonWorkUnitStore {
* The resume data cache for this request. This will be a immutable cache. * The resume data cache for this request. This will be a immutable cache.
*/ */
renderResumeDataCache: RenderResumeDataCache | null renderResumeDataCache: RenderResumeDataCache | null
}
export type RequestStore = ProdRequestStore | DevRequestStore
export type ProdRequestStore = BaseRequestStore & AllMissing<DevStore>
export type DevRequestStore = BaseRequestStore & DevStore
// DEV-only // If `cacheComponents` is enabled, we add multiple extra properties on the store.
// We either want all of them to be present, or all of them to be undefined.
// Note that we don't want to remove the properties altogether, as in `{ a: A } | { a: A, b: B }`,
// because then typescript wouldn prevent us from doing `if (requestStore.someProp) { ... }` --
// we'd need to check `if ('someProp' in requestStore && requestStore.someProp) { ... }` instead,
// which is annoying to write everywhere.
type DevStore = DevStoreLegacy | DevStoreModern
type DevStoreLegacy = DevStoreCommon & AllMissing<DevStoreModernPartial>
type DevStoreModern = DevStoreCommon & DevStoreModernPartial
type DevStoreCommon = {
usedDynamic?: boolean usedDynamic?: boolean
devFallbackParams?: OpaqueFallbackRouteParams | null devFallbackParams?: OpaqueFallbackRouteParams | null
stagedRendering?: StagedRenderingController | null }
asyncApiPromises?: DevAsyncApiPromises
cacheSignal?: CacheSignal | null export type DevStoreModernPartial = {
prerenderResumeDataCache?: PrerenderResumeDataCache | null readonly stagedRendering: StagedRenderingController
readonly asyncApiPromises: DevAsyncApiPromises
} & (
| {
// In the initial render, we track and fill caches
readonly cacheSignal: CacheSignal
readonly prerenderResumeDataCache: PrerenderResumeDataCache
}
| {
// In the final (restarted) render, we do not track or fill caches
readonly cacheSignal: null
readonly prerenderResumeDataCache: null
}
)
type AllMissing<TObj extends Record<string, any>> = {
[key in keyof TObj]?: undefined
} }
type DevAsyncApiPromises = { type DevAsyncApiPromises = {
......
...@@ -17,6 +17,7 @@ import { prerender } from 'react-server-dom-webpack/static' ...@@ -17,6 +17,7 @@ import { prerender } from 'react-server-dom-webpack/static'
import type { WorkStore } from '../app-render/work-async-storage.external' import type { WorkStore } from '../app-render/work-async-storage.external'
import { workAsyncStorage } from '../app-render/work-async-storage.external' import { workAsyncStorage } from '../app-render/work-async-storage.external'
import type { import type {
DevRequestStore,
PrerenderStoreModernClient, PrerenderStoreModernClient,
PrerenderStoreModernRuntime, PrerenderStoreModernRuntime,
PrivateUseCacheStore, PrivateUseCacheStore,
...@@ -1848,7 +1849,7 @@ function isRecentlyRevalidatedTag(tag: string, workStore: WorkStore): boolean { ...@@ -1848,7 +1849,7 @@ function isRecentlyRevalidatedTag(tag: string, workStore: WorkStore): boolean {
async function delayBeforeCacheReadStartInDev( async function delayBeforeCacheReadStartInDev(
stage: NonStaticRenderStage, stage: NonStaticRenderStage,
requestStore: RequestStore requestStore: DevRequestStore
): Promise<void> { ): Promise<void> {
const { stagedRendering } = requestStore const { stagedRendering } = requestStore
if (stagedRendering && stagedRendering.currentStage < stage) { if (stagedRendering && stagedRendering.currentStage < stage) {
...@@ -1859,7 +1860,7 @@ async function delayBeforeCacheReadStartInDev( ...@@ -1859,7 +1860,7 @@ async function delayBeforeCacheReadStartInDev(
/** Note: Only call this after a `cacheSignal.beginRead()`. */ /** Note: Only call this after a `cacheSignal.beginRead()`. */
async function delayOrHangStartedCacheReadInDev( async function delayOrHangStartedCacheReadInDev(
stage: NonStaticRenderStage, stage: NonStaticRenderStage,
requestStore: RequestStore, requestStore: DevRequestStore,
cacheSignal: CacheSignal | null, cacheSignal: CacheSignal | null,
route: string, route: string,
expression: string expression: string
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment