{"version":3,"file":"WaitFor.cjs","names":["isServer","Fragment"],"sources":["../../../src/createEntry/WaitFor/WaitFor.tsx"],"sourcesContent":["import {\n  useEffect,\n  useMemo,\n  useState,\n  Fragment,\n  isValidElement,\n  cloneElement,\n  createElement,\n  type ReactElement,\n  type ReactNode,\n} from 'react';\nimport { normalizeAsyncQueue } from '@pastweb/tools/createAsyncStore/normalizeAsyncQueue';\nimport { isServer } from '@pastweb/tools/envs';\n\nimport type { Component } from '../types';\nimport type { WaitForProps } from './types';\n\n/**\n * Waits for one or more async operations before rendering children.\n *\n * Supports:\n * - Promises\n * - Async functions\n * - AsyncStore instances\n * - Arrays of async tasks\n *\n * On the client:\n * - renders a fallback while waiting\n * - resolves all async tasks in parallel\n *\n * On the server:\n * - awaits all tasks before rendering synchronously\n *\n * @param props - WaitFor configuration.\n *\n * @returns The fallback while loading, otherwise the rendered children.\n */\nexport function WaitFor(props: WaitForProps) {\n  return isServer ? serverSide(props) : clientSide(props);\n}\n\n/**\n * Client-side implementation.\n */\nfunction clientSide(props: WaitForProps): ReactElement | null {\n  const { wait, fallback: Fallback = Fragment, children } = props;\n  const [isLoading, setLoading] = useState(true);\n\n  /**\n   * Memoize normalized queue to avoid recomputation\n   * on every render.\n   */\n  const queue = useMemo(() => normalizeAsyncQueue(wait), [wait]);\n\n  useEffect(() => {\n    let mounted = true;\n\n    (async () => {\n      try {\n        if (queue.length) await Promise.all(queue);\n\n        if (mounted) setLoading(false);\n      } catch (error) {\n        console.error(error);\n\n        if (mounted) setLoading(false);\n      }\n    })();\n\n    return () => { mounted = false; };\n  }, [queue]);\n\n  if (isLoading) return createFallback(Fallback);\n\n  return renderChildren(children);\n}\n\n/**\n * Server-side implementation.\n */\nasync function serverSide(props: WaitForProps): Promise<ReactElement | null> {\n  const { wait, children } = props;\n\n  try {\n    const queue = normalizeAsyncQueue(wait);\n\n    if (queue.length) {\n      await Promise.all(queue);\n    }\n\n    return renderChildren(children);\n  } catch (error) {\n    console.error(error);\n    return null;\n  }\n}\n\n/**\n * Safely renders fallback components.\n */\nfunction createFallback(Fallback: ReactNode | Component): ReactElement | null {\n  return isValidElement(Fallback) ? cloneElement(Fallback as ReactElement) : createElement(Fallback as Component);\n}\n\n/**\n * Safely renders children.\n */\nfunction renderChildren(children: ReactNode): ReactElement | null {\n  return isValidElement(children) ? cloneElement(children as ReactElement) : null;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAqCA,SAAgB,QAAQ,OAAqB;CAC3C,OAAOA,oBAAAA,WAAW,WAAW,KAAK,IAAI,WAAW,KAAK;AACxD;;;;AAKA,SAAS,WAAW,OAA0C;CAC5D,MAAM,EAAE,MAAM,UAAU,WAAWC,MAAAA,UAAU,aAAa;CAC1D,MAAM,CAAC,WAAW,eAAA,GAAA,MAAA,SAAA,CAAuB,IAAI;;;;;CAM7C,MAAM,SAAA,GAAA,MAAA,QAAA,QAAA,GAAA,oDAAA,oBAAA,CAA0C,IAAI,GAAG,CAAC,IAAI,CAAC;CAE7D,CAAA,GAAA,MAAA,UAAA,OAAgB;EACd,IAAI,UAAU;EAEd,CAAC,YAAY;GACX,IAAI;IACF,IAAI,MAAM,QAAQ,MAAM,QAAQ,IAAI,KAAK;IAEzC,IAAI,SAAS,WAAW,KAAK;GAC/B,SAAS,OAAO;IACd,QAAQ,MAAM,KAAK;IAEnB,IAAI,SAAS,WAAW,KAAK;GAC/B;EACF,EAAA,CAAG;EAEH,aAAa;GAAE,UAAU;EAAO;CAClC,GAAG,CAAC,KAAK,CAAC;CAEV,IAAI,WAAW,OAAO,eAAe,QAAQ;CAE7C,OAAO,eAAe,QAAQ;AAChC;;;;AAKA,eAAe,WAAW,OAAmD;CAC3E,MAAM,EAAE,MAAM,aAAa;CAE3B,IAAI;EACF,MAAM,SAAA,GAAA,oDAAA,oBAAA,CAA4B,IAAI;EAEtC,IAAI,MAAM,QACR,MAAM,QAAQ,IAAI,KAAK;EAGzB,OAAO,eAAe,QAAQ;CAChC,SAAS,OAAO;EAC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