const EXTERNAL_HYDRATION_PROMISE = Symbol.for('octane.external-hydration-promise'); type ExternalHydrationThenable = PromiseLike & { [EXTERNAL_HYDRATION_PROMISE]: true; status?: ThenableStatus; value?: T; reason?: unknown; }; type ThenableStatus = 'pending' | 'fulfilled' | 'rejected'; const externalHydrationThenables = new WeakMap>(); /** * Wrap a router-owned promise so Octane still schedules its suspense boundary, * while TanStack's serializer remains the only owner of its hydration value. */ export function toExternalHydrationThenable(thenable: PromiseLike): PromiseLike { const key = thenable as object; const existing = externalHydrationThenables.get(key); if (existing) { return existing as ExternalHydrationThenable; } let localStatus: ThenableStatus | undefined; let localValue: T | undefined; let localReason: unknown; let hasLocalValue = false; let hasLocalReason = false; const externalThenable: ExternalHydrationThenable = { [EXTERNAL_HYDRATION_PROMISE]: true, get status() { return localStatus ?? readThenableStatus(thenable); }, set status(status) { localStatus = status; }, get value(): T | undefined { return hasLocalValue ? localValue : readThenableProperty(thenable, 'value'); }, set value(value: T | undefined) { hasLocalValue = true; localValue = value; }, get reason(): unknown { return hasLocalReason ? localReason : readThenableProperty(thenable, 'reason'); }, set reason(reason) { hasLocalReason = true; localReason = reason; }, then(onfulfilled, onrejected) { return thenable.then(onfulfilled, onrejected); }, }; externalHydrationThenables.set(key, externalThenable); return externalThenable; } function readThenableStatus(thenable: PromiseLike) { const status = readThenableProperty(thenable, 'status'); return status === 'pending' || status === 'fulfilled' || status === 'rejected' ? status : undefined; } function readThenableProperty( thenable: PromiseLike, property: 'status' | 'value' | 'reason', ): T | undefined { try { return (thenable as PromiseLike & Record)[property]; } catch { return undefined; } }