{"version":3,"file":"PersistQueryClientProvider.cjs","names":["React","persistQueryClientSubscribe","QueryClientProvider","IsRestoringProvider"],"sources":["../../src/PersistQueryClientProvider.tsx"],"sourcesContent":["'use client'\nimport * as React from 'react'\n\nimport {\n  persistQueryClientRestore,\n  persistQueryClientSubscribe,\n} from '@tanstack/query-persist-client-core'\nimport { IsRestoringProvider, QueryClientProvider } from '@tanstack/react-query'\nimport type { PersistQueryClientOptions } from '@tanstack/query-persist-client-core'\nimport type { OmitKeyof, QueryClientProviderProps } from '@tanstack/react-query'\n\n/**\n * The props of `PersistQueryClientProvider`: the props of `QueryClientProvider`, plus the\n * `persistOptions` and callbacks for when restoring succeeds or fails.\n */\nexport type PersistQueryClientProviderProps = QueryClientProviderProps & {\n  persistOptions: OmitKeyof<PersistQueryClientOptions, 'queryClient'>\n  onSuccess?: () => Promise<unknown> | unknown\n  onError?: () => Promise<unknown> | unknown\n}\n\n/**\n * Provides the `QueryClient` like `QueryClientProvider`, and restores the persisted client first:\n * while restoring, `useIsRestoring` returns `true` and queries wait for the restore to finish\n * before subscribing. Once restored, the client is saved with the persister whenever the cache\n * changes.\n * @param props - The `QueryClientProvider` props, the `persistOptions`, and the `onSuccess` and\n * `onError` callbacks. `onSuccess` is called once restoring finishes, even if nothing was restored\n * (e.g. because the persisted client expired), and `onError` if restoring or `onSuccess` throws.\n * @returns The `QueryClientProvider` wrapping the children.\n */\nexport const PersistQueryClientProvider = ({\n  children,\n  persistOptions,\n  onSuccess,\n  onError,\n  ...props\n}: PersistQueryClientProviderProps): React.JSX.Element => {\n  const [isRestoring, setIsRestoring] = React.useState(true)\n  const optionsRef = React.useRef({ persistOptions, onSuccess, onError })\n  const didRestoreRef = React.useRef(false)\n\n  React.useEffect(() => {\n    optionsRef.current = { persistOptions, onSuccess, onError }\n  })\n\n  React.useEffect(() => {\n    const options = {\n      ...optionsRef.current.persistOptions,\n      queryClient: props.client,\n    }\n    if (!didRestoreRef.current) {\n      didRestoreRef.current = true\n      persistQueryClientRestore(options)\n        .then(() => optionsRef.current.onSuccess?.())\n        .catch(() => optionsRef.current.onError?.())\n        .finally(() => {\n          setIsRestoring(false)\n        })\n    }\n    return isRestoring ? undefined : persistQueryClientSubscribe(options)\n  }, [props.client, isRestoring])\n\n  return (\n    <QueryClientProvider {...props}>\n      <IsRestoringProvider value={isRestoring}>{children}</IsRestoringProvider>\n    </QueryClientProvider>\n  )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BA,MAAa,8BAA8B,EACzC,UACA,gBACA,WACA,SACA,GAAG,YACqD;CACxD,MAAM,CAAC,aAAa,kBAAkBA,MAAM,SAAS,IAAI;CACzD,MAAM,aAAaA,MAAM,OAAO;EAAE;EAAgB;EAAW;CAAQ,CAAC;CACtE,MAAM,gBAAgBA,MAAM,OAAO,KAAK;CAExC,MAAM,gBAAgB;EACpB,WAAW,UAAU;GAAE;GAAgB;GAAW;EAAQ;CAC5D,CAAC;CAED,MAAM,gBAAgB;EACpB,MAAM,UAAU;GACd,GAAG,WAAW,QAAQ;GACtB,aAAa,MAAM;EACrB;EACA,IAAI,CAAC,cAAc,SAAS;GAC1B,cAAc,UAAU;GACxB,CAAA,GAAA,oCAAA,0BAAA,CAA0B,OAAO,CAAC,CAC/B,WAAW;;IAAW,QAAA,yBAAA,sBAAA,WAAA,QAAA,CAAQ,eAAA,QAAA,0BAAA,KAAA,IAAA,KAAA,IAAA,sBAAA,KAAA,mBAAY;GAAC,CAAA,CAAC,CAC5C,YAAY;;IAAW,QAAA,0BAAA,uBAAA,WAAA,QAAA,CAAQ,aAAA,QAAA,2BAAA,KAAA,IAAA,KAAA,IAAA,uBAAA,KAAA,oBAAU;GAAC,CAAA,CAAC,CAC3C,cAAc;IACb,eAAe,KAAK;GACtB,CAAC;EACL;EACA,OAAO,cAAc,KAAA,KAAA,GAAYC,oCAAAA,4BAAAA,CAA4B,OAAO;CACtE,GAAG,CAAC,MAAM,QAAQ,WAAW,CAAC;CAE9B,OACE,iBAAA,GAAA,kBAAA,IAAA,CAACC,sBAAAA,qBAAD;EAAqB,GAAI;EACvB,UAAA,iBAAA,GAAA,kBAAA,IAAA,CAACC,sBAAAA,qBAAD;GAAqB,OAAO;GAAc;EAA8B,CAAA;CACrD,CAAA;AAEzB"}