{"version":3,"file":"utils.cjs","names":["preloadedSSREntries"],"sources":["../../src/EntryAdapter/utils.ts"],"sourcesContent":["import { isEntry } from '@pastweb/tools/createEntry';\nimport { registerAsyncTask } from '@pastweb/tools/ssrUtils/asyncTasks';\nimport { getCurrentSSRTracker } from '@pastweb/tools/ssrUtils/ssrTracker';\nimport { preloadedSSREntries } from './constants';\nimport type { EntryAdapterProps, GenericEntry } from './types';\n\n/**\n * Returns a preloaded server entry or registers its async loader for a later\n * SSR pass.\n *\n * On the first collection render, the loader is queued through\n * `registerAsyncTask` and the function returns `undefined`. After\n * `resolveAsyncTasks` runs, subsequent render passes receive the resolved\n * server entry from {@link preloadedSSREntries}.\n *\n * @param loader - Optional async server-entry loader.\n * @returns The resolved server entry when available.\n */\nexport function getServerEntry(loader?: EntryAdapterProps['ssrEntry']): GenericEntry | undefined {\n  if (!loader) return undefined;\n\n  const preloaded = preloadedSSREntries.get(loader);\n  if (preloaded) return preloaded;\n\n  registerAsyncTask(async () => {\n    const entry = await loader();\n    preloadedSSREntries.set(loader, entry);\n  });\n\n  return undefined;\n}\n\n/**\n * Renders the server side of {@link import('./EntryAdapter').EntryAdapter}.\n *\n * The function preserves the client/server bundle split by registering the\n * async server entry loader during the collection pass, then rendering the\n * loaded server entry on later SSR passes. The returned string is the SSR\n * placeholder id that will be replaced by `getComposedSSR`.\n *\n * When no async loader is provided, the regular `entry` is used on the server.\n * This supports frameworks that share one entry renderer across client and SSR.\n *\n * @param entry - Entry used as the server renderer when no `ssrEntry` loader is provided.\n * @param loader - Optional async server-entry loader.\n * @param props - Props merged into the active entry `initData`.\n * @returns SSR placeholder id, or `null` while waiting for the async server entry.\n */\nexport function renderServerEntryAdapter(\n  entry: GenericEntry,\n  loader: EntryAdapterProps['ssrEntry'],\n  props: Record<string, any>,\n): string | null {\n  if (!isEntry(entry)) {\n    throw Error('The entry must be generate via \"createEntry\" function.');\n  }\n\n  entry.mergeOptions({ initData: { ...props } });\n\n  const serverEntry = getServerEntry(loader);\n  if (loader && !serverEntry) return null;\n\n  const activeEntry = serverEntry || entry;\n\n  if (!isEntry(activeEntry)) {\n    throw Error('The ssrEntry must be generate via \"createEntry\" function.');\n  }\n\n  activeEntry.mergeOptions({ initData: { ...props } });\n\n  const tracker = getCurrentSSRTracker();\n  const isStatic = tracker?.isStatic ?? false;\n  const ssrId = activeEntry.ssrId as string;\n  const mounted = activeEntry.mount({ isStatic });\n\n  if (typeof mounted === 'string' && mounted !== ssrId) {\n    activeEntry.memoSSR(async () => mounted);\n  }\n\n  return ssrId;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAkBA,SAAgB,eAAe,QAAkE;CAC/F,IAAI,CAAC,QAAQ,OAAO,KAAA;CAEpB,MAAM,YAAYA,+BAAAA,oBAAoB,IAAI,MAAM;CAChD,IAAI,WAAW,OAAO;CAEtB,CAAA,GAAA,mCAAA,kBAAA,CAAkB,YAAY;EAC5B,MAAM,QAAQ,MAAM,OAAO;EAC3B,+BAAA,oBAAoB,IAAI,QAAQ,KAAK;CACvC,CAAC;AAGH;;;;;;;;;;;;;;;;;AAkBA,SAAgB,yBACd,OACA,QACA,OACe;CACf,IAAI,EAAA,GAAA,2BAAA,QAAA,CAAS,KAAK,GAChB,MAAM,MAAM,0DAAwD;CAGtE,MAAM,aAAa,EAAE,UAAU,EAAE,GAAG,MAAM,EAAE,CAAC;CAE7C,MAAM,cAAc,eAAe,MAAM;CACzC,IAAI,UAAU,CAAC,aAAa,OAAO;CAEnC,MAAM,cAAc,eAAe;CAEnC,IAAI,EAAA,GAAA,2BAAA,QAAA,CAAS,WAAW,GACtB,MAAM,MAAM,6DAA2D;CAGzE,YAAY,aAAa,EAAE,UAAU,EAAE,GAAG,MAAM,EAAE,CAAC;CAGnD,MAAM,YAAA,GAAA,mCAAA,qBAAA,CAAiB,CAAC,EAAE,YAAY;CACtC,MAAM,QAAQ,YAAY;CAC1B,MAAM,UAAU,YAAY,MAAM,EAAE,SAAS,CAAC;CAE9C,IAAI,OAAO,YAAY,YAAY,YAAY,OAC7C,YAAY,QAAQ,YAAY,OAAO;CAGzC,OAAO;AACT"}