{"version":3,"file":"suspense.cjs","names":[],"sources":["../../src/suspense.ts"],"sourcesContent":["import type {\n  DefaultError,\n  DefaultedQueryObserverOptions,\n  Query,\n  QueryKey,\n  QueryObserver,\n  QueryObserverResult,\n} from '@tanstack/query-core'\nimport type { QueryErrorResetBoundaryValue } from './QueryErrorResetBoundary'\n\n/**\n * The default `throwOnError` of the suspense hooks: throws the error to the nearest error boundary\n * only if the query has no data to show.\n * @param _error - The error of the query. Unused.\n * @param query - The query that errored.\n * @returns `true` if the query has no data.\n */\nexport const defaultThrowOnError = <\n  TQueryFnData = unknown,\n  TError = DefaultError,\n  TData = TQueryFnData,\n  TQueryKey extends QueryKey = QueryKey,\n>(\n  _error: TError,\n  query: Query<TQueryFnData, TError, TData, TQueryKey>,\n) => query.state.data === undefined\n\n/**\n * Raises `staleTime` and a numeric `gcTime` to at least 1000ms when `suspense` is enabled, so a\n * component that remounts after suspending doesn't refetch right away or find the query garbage\n * collected. A `'static'` `staleTime` is kept as is. Mutates the options.\n * @param defaultedOptions - The defaulted query options to adjust.\n */\nexport const ensureSuspenseTimers = (\n  defaultedOptions: DefaultedQueryObserverOptions<any, any, any, any, any>,\n) => {\n  if (defaultedOptions.suspense) {\n    // Handle staleTime to ensure minimum 1000ms in Suspense mode\n    // This prevents unnecessary refetching when components remount after suspending\n    const MIN_SUSPENSE_TIME_MS = 1000\n\n    const clamp = (value: number | 'static' | undefined) =>\n      value === 'static'\n        ? value\n        : Math.max(value ?? MIN_SUSPENSE_TIME_MS, MIN_SUSPENSE_TIME_MS)\n\n    const originalStaleTime = defaultedOptions.staleTime\n    defaultedOptions.staleTime =\n      typeof originalStaleTime === 'function'\n        ? (...args) => clamp(originalStaleTime(...args))\n        : clamp(originalStaleTime)\n\n    if (typeof defaultedOptions.gcTime === 'number') {\n      defaultedOptions.gcTime = Math.max(\n        defaultedOptions.gcTime,\n        MIN_SUSPENSE_TIME_MS,\n      )\n    }\n  }\n}\n\n/**\n * Checks whether a query should suspend: `suspense` is enabled and the result is still `pending`.\n * @param defaultedOptions - The defaulted query options, if any.\n * @param result - The current result of the observer.\n * @returns `true` if the component should suspend.\n */\nexport const shouldSuspend = (\n  defaultedOptions:\n    | DefaultedQueryObserverOptions<any, any, any, any, any>\n    | undefined,\n  result: QueryObserverResult<any, any>,\n) => defaultedOptions?.suspense && result.isPending\n\n/**\n * Fetches the query for a suspending component, without affecting the observer's own result. If\n * the fetch fails, the reset state of the error boundary is cleared, so the error is thrown to it.\n * @param defaultedOptions - The defaulted query options to fetch with.\n * @param observer - The observer of the query.\n * @param errorResetBoundary - The value of the nearest `QueryErrorResetBoundary`.\n * @returns A promise that resolves once the fetch settles. It never rejects.\n */\nexport const fetchOptimistic = <\n  TQueryFnData,\n  TError,\n  TData,\n  TQueryData,\n  TQueryKey extends QueryKey,\n>(\n  defaultedOptions: DefaultedQueryObserverOptions<\n    TQueryFnData,\n    TError,\n    TData,\n    TQueryData,\n    TQueryKey\n  >,\n  observer: QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>,\n  errorResetBoundary: QueryErrorResetBoundaryValue,\n) =>\n  observer.fetchOptimistic(defaultedOptions).catch(() => {\n    errorResetBoundary.clearReset()\n  })\n"],"mappings":";;;;;;;;;AAiBA,MAAa,uBAMX,QACA,UACG,MAAM,MAAM,SAAS,KAAA;;;;;;;AAQ1B,MAAa,wBACX,qBACG;CACH,IAAI,iBAAiB,UAAU;EAG7B,MAAM,uBAAuB;EAE7B,MAAM,SAAS,UACb,UAAU,WACN,QACA,KAAK,IAAI,SAAS,sBAAsB,oBAAoB;EAElE,MAAM,oBAAoB,iBAAiB;EAC3C,iBAAiB,YACf,OAAO,sBAAsB,cACxB,GAAG,SAAS,MAAM,kBAAkB,GAAG,IAAI,CAAC,IAC7C,MAAM,iBAAiB;EAE7B,IAAI,OAAO,iBAAiB,WAAW,UACrC,iBAAiB,SAAS,KAAK,IAC7B,iBAAiB,QACjB,oBACF;CAEJ;AACF;;;;;;;AAQA,MAAa,iBACX,kBAGA,YAAA,qBAAA,QAAA,qBAAA,KAAA,IAAA,KAAA,IACG,iBAAkB,aAAY,OAAO;;;;;;;;;AAU1C,MAAa,mBAOX,kBAOA,UACA,uBAEA,SAAS,gBAAgB,gBAAgB,CAAC,CAAC,YAAY;CACrD,mBAAmB,WAAW;AAChC,CAAC"}