{"version":3,"sources":["../../src/use/index.ts"],"names":["useContext","ErrorBoundaryContext_default","SuspenseContext_default","_value","_root","_awaitOnly","useSyncExternalStore","_subscribeWithError","_valueToggler","handleSuspense_default","alwaysNoop_default","noop"],"mappings":";;;;;;;;;;;;;AAQA,IAAM,GAAA,GA6CF,CAAC,KAAA,EAAO,UAAe,KAAA;AACzB,EAAM,MAAA,gBAAA,GAAmBA,iBAAWC,8CAAoB,CAAA;AAExD,EAAM,MAAA,WAAA,GAAcD,iBAAWE,yCAAe,CAAA;AAE9C,EAAA,IAAI,KAAO,EAAA;AACT,IAAA,MAAM,aAAa,KAAM,CAAA,KAAA;AAEzB,IAAA,MAAM,MAAM,UAAWC,CAAAA,CAAAA;AAEvB,IAAA,MAAM,UAAU,GAAQ,KAAA,KAAA,CAAA;AAExB,IAAI,IAAA,OAAA,IAAW,CAAC,UAAY,EAAA;AAC1B,MAAM,MAAA,GAAA;AAAA;AAGR,IAAA,MAAM,OAAO,KAAMC,CAAAA,CAAAA;AAEnB,IAAI,IAAA,IAAA,CAAKD,CAAW,KAAA,KAAA,CAAA,IAAa,OAAS,EAAA;AACxC,MAAM,MAAA,iBAAA,GAAoB,CAAC,KAAME,CAAAA,CAAAA;AAEjC,MAAAC,0BAAA;AAAA,QAAqB,KAAMC,CAAAA,CAAAA;AAAA,QAAqB,MAC9C,iBAAA,GACK,UAAWC,CAAAA,CAAAA,IAAiB,CAAK,GAAA,KAAA,CAAMA,CACrC,GAAA,CAAA,UAAA,CAAWL,CAAW,KAAA,KAAA,CAAA,KAAsB,CAC7C,GAAA,IAAA,CAAKA,CAAW,KAAA,KAAA;AAAA,OACxB;AAEA,MAAA,MAAM,KAAQ,GAAA,iBAAA,GAAoB,KAAM,CAAA,GAAA,EAAQ,GAAA,KAAA,CAAA;AAEhD,MAAA,OAAO,UAAa,GAAA,CAAC,KAAO,EAAA,GAAG,CAAI,GAAA,KAAA;AAAA;AAGrC,IAAM,MAAAM,wCAAA,CAAe,IAAM,EAAA,gBAAA,EAAkB,WAAW,CAAA;AAAA;AAG1D,EAAAH,0BAAA,CAAqBI,sCAAYC,qBAAI,CAAA;AACvC,CAAA;AAEA,IAAO,WAAQ,GAAA","file":"chunk-2EQ2536U.cjs","sourcesContent":["import { useContext, useSyncExternalStore } from 'react';\nimport type { AsyncState, Falsy } from '../types';\nimport ErrorBoundaryContext from '../utils/ErrorBoundaryContext';\nimport SuspenseContext from '../utils/SuspenseContext';\nimport handleSuspense from '../utils/handleSuspense';\nimport alwaysNoop from '../utils/alwaysNoop';\nimport noop from 'lodash.noop';\n\nconst use: {\n  /**\n   * Hook to retrieve the current value of the loaded {@link state}.\n   * If the {@link state} isn't loaded, the component using this hook suspends.\n   * Ensure the component is wrapped in a <Suspense> component to handle the loading state.\n   * If loading fails and {@link safeReturn} is not enabled, an error is thrown.\n   *\n   * @example\n   * ```jsx\n   * const DataComponent = () => {\n   *   const data = use(asyncState); // Suspends if not loaded\n   *\n   *   return <div>Data: {JSON.stringify(data)}</div>;\n   * };\n   *\n   * const SafeComponent = () => {\n   *   const [data, error] = use(asyncState, true); // Safely returns a tuple\n   *\n   *   if (error) {\n   *     return <div>Error: {error.message}</div>;\n   *   }\n   *\n   *   return <div>Data: {JSON.stringify(data)}</div>;\n   * };\n   *\n   * const App = () => (\n   *   <>\n   *     <Suspense fallback={<div>Loading...</div>}>\n   *       <DataComponent />\n   *     </Suspense>\n   *     <Suspense fallback={<div>Loading...</div>}>\n   *       <SafeComponent />\n   *     </Suspense>\n   *   </>\n   * );\n   * ```\n   */\n  <S extends AsyncState | Falsy, SafeReturn extends boolean = false>(\n    state: S,\n    safeReturn?: SafeReturn\n  ): S extends AsyncState<infer T, infer E>\n    ? SafeReturn extends false\n      ? T\n      : Readonly<[value: T, error: undefined] | [value: undefined, error: E]>\n    : undefined;\n} = (state, safeReturn) => {\n  const errorBoundaryCtx = useContext(ErrorBoundaryContext);\n\n  const suspenseCtx = useContext(SuspenseContext);\n\n  if (state) {\n    const errorState = state.error;\n\n    const err = errorState._value;\n\n    const isError = err !== undefined;\n\n    if (isError && !safeReturn) {\n      throw err;\n    }\n\n    const root = state._root;\n\n    if (root._value !== undefined || isError) {\n      const withValueWatching = !state._awaitOnly;\n\n      useSyncExternalStore(state._subscribeWithError, () =>\n        withValueWatching\n          ? (errorState._valueToggler << 1) | state._valueToggler\n          : (((errorState._value === undefined) as any) << 1) |\n            ((root._value !== undefined) as any)\n      );\n\n      const value = withValueWatching ? state.get() : undefined;\n\n      return safeReturn ? [value, err] : value;\n    }\n\n    throw handleSuspense(root, errorBoundaryCtx, suspenseCtx);\n  }\n\n  useSyncExternalStore(alwaysNoop, noop);\n};\n\nexport default use;\n"]}