|
|
import { dehydrate, hydrate } from '@tanstack/query-core' |
|
|
import type { |
|
|
DehydrateOptions, |
|
|
DehydratedState, |
|
|
HydrateOptions, |
|
|
NotifyEventType, |
|
|
QueryClient, |
|
|
} from '@tanstack/query-core' |
|
|
|
|
|
export type Promisable<T> = T | PromiseLike<T> |
|
|
|
|
|
export interface Persister { |
|
|
persistClient: (persistClient: PersistedClient) => Promisable<void> |
|
|
restoreClient: () => Promisable<PersistedClient | undefined> |
|
|
removeClient: () => Promisable<void> |
|
|
} |
|
|
|
|
|
export interface PersistedClient { |
|
|
timestamp: number |
|
|
buster: string |
|
|
clientState: DehydratedState |
|
|
} |
|
|
|
|
|
export interface PersistQueryClientRootOptions { |
|
|
|
|
|
queryClient: QueryClient |
|
|
|
|
|
|
|
|
persister: Persister |
|
|
|
|
|
|
|
|
buster?: string |
|
|
} |
|
|
|
|
|
export interface PersistedQueryClientRestoreOptions |
|
|
extends PersistQueryClientRootOptions { |
|
|
|
|
|
|
|
|
|
|
|
maxAge?: number |
|
|
|
|
|
hydrateOptions?: HydrateOptions |
|
|
} |
|
|
|
|
|
export interface PersistedQueryClientSaveOptions |
|
|
extends PersistQueryClientRootOptions { |
|
|
|
|
|
dehydrateOptions?: DehydrateOptions |
|
|
} |
|
|
|
|
|
export interface PersistQueryClientOptions |
|
|
extends PersistedQueryClientRestoreOptions, |
|
|
PersistedQueryClientSaveOptions, |
|
|
PersistQueryClientRootOptions {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const cacheEventTypes: Array<NotifyEventType> = ['added', 'removed', 'updated'] |
|
|
|
|
|
function isCacheEventType(eventType: NotifyEventType) { |
|
|
return cacheEventTypes.includes(eventType) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function persistQueryClientRestore({ |
|
|
queryClient, |
|
|
persister, |
|
|
maxAge = 1000 * 60 * 60 * 24, |
|
|
buster = '', |
|
|
hydrateOptions, |
|
|
}: PersistedQueryClientRestoreOptions) { |
|
|
try { |
|
|
const persistedClient = await persister.restoreClient() |
|
|
|
|
|
if (persistedClient) { |
|
|
if (persistedClient.timestamp) { |
|
|
const expired = Date.now() - persistedClient.timestamp > maxAge |
|
|
const busted = persistedClient.buster !== buster |
|
|
if (expired || busted) { |
|
|
return persister.removeClient() |
|
|
} else { |
|
|
hydrate(queryClient, persistedClient.clientState, hydrateOptions) |
|
|
} |
|
|
} else { |
|
|
return persister.removeClient() |
|
|
} |
|
|
} |
|
|
} catch (err) { |
|
|
if (process.env.NODE_ENV !== 'production') { |
|
|
console.error(err) |
|
|
console.warn( |
|
|
'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.', |
|
|
) |
|
|
} |
|
|
|
|
|
await persister.removeClient() |
|
|
|
|
|
throw err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function persistQueryClientSave({ |
|
|
queryClient, |
|
|
persister, |
|
|
buster = '', |
|
|
dehydrateOptions, |
|
|
}: PersistedQueryClientSaveOptions) { |
|
|
const persistClient: PersistedClient = { |
|
|
buster, |
|
|
timestamp: Date.now(), |
|
|
clientState: dehydrate(queryClient, dehydrateOptions), |
|
|
} |
|
|
|
|
|
await persister.persistClient(persistClient) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function persistQueryClientSubscribe( |
|
|
props: PersistedQueryClientSaveOptions, |
|
|
) { |
|
|
const unsubscribeQueryCache = props.queryClient |
|
|
.getQueryCache() |
|
|
.subscribe((event) => { |
|
|
if (isCacheEventType(event.type)) { |
|
|
persistQueryClientSave(props) |
|
|
} |
|
|
}) |
|
|
|
|
|
const unsubscribeMutationCache = props.queryClient |
|
|
.getMutationCache() |
|
|
.subscribe((event) => { |
|
|
if (isCacheEventType(event.type)) { |
|
|
persistQueryClientSave(props) |
|
|
} |
|
|
}) |
|
|
|
|
|
return () => { |
|
|
unsubscribeQueryCache() |
|
|
unsubscribeMutationCache() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function persistQueryClient( |
|
|
props: PersistQueryClientOptions, |
|
|
): [() => void, Promise<void>] { |
|
|
let hasUnsubscribed = false |
|
|
let persistQueryClientUnsubscribe: (() => void) | undefined |
|
|
const unsubscribe = () => { |
|
|
hasUnsubscribed = true |
|
|
persistQueryClientUnsubscribe?.() |
|
|
} |
|
|
|
|
|
|
|
|
const restorePromise = persistQueryClientRestore(props).then(() => { |
|
|
if (!hasUnsubscribed) { |
|
|
|
|
|
persistQueryClientUnsubscribe = persistQueryClientSubscribe(props) |
|
|
} |
|
|
}) |
|
|
|
|
|
return [unsubscribe, restorePromise] |
|
|
} |
|
|
|