UNPKG

async-states

Version:

Core of async-states

56 lines (53 loc) 2.05 kB
import { isServer, maybeWindow } from '../utils.js'; import { initial } from '../enums.js'; let HYDRATION_DATA_KEY = "__$$"; const attemptHydratedState = isServer ? attemptHydratedStateServer : attemptHydratedStateDOM; // unused parameters to keep the same exported signature function attemptHydratedStateServer(_instance) { return null; } function attemptHydratedStateDOM(instance) { if (!maybeWindow) { return null; } let { key, config, ctx } = instance; let contextName = ctx?.name ?? null; // why there are two possible keys ? // When coming from the server, sources there can be created from two ways: // globally via createSource, or locally via hooks. // either ways, the hydrated source belongs to a Provider's Context, which has // a name, so the hydrated data will come to the client with that key. // So, this source either has a context with a name (React.useId()) when // created initially inside a Provider, or else, we'll fallback to the // global context hydration in case the source wasn't created from inside // a Provider. let hydrationKey = HYDRATION_DATA_KEY; if (contextName !== null) { hydrationKey = contextName; } let savedHydrationData = maybeWindow[hydrationKey]; if (!savedHydrationData) { return null; } let instanceHydration = savedHydrationData[key]; if (!instanceHydration) { return null; } delete savedHydrationData[key]; if (Object.keys(savedHydrationData).length === 0) { delete maybeWindow[hydrationKey]; } let [state] = instanceHydration; let { status, props } = state; if (typeof state.data === "undefined") { state.data = undefined; if (status === initial && props.args[0] === null && !config.initialValue) { props.args[0] = undefined; } } return instanceHydration; } export { attemptHydratedState, attemptHydratedStateDOM, attemptHydratedStateServer }; //# sourceMappingURL=StateHydration.js.map