mirror of
https://github.com/joinmarket-webui/jam.git
synced 2026-08-15 12:50:48 +02:00
* build(deps): update dependencies @tailwindcss/vite 4.2.2 → 4.2.4 @tanstack/eslint-plugin-query 5.99.0 → 5.100.1 @tanstack/react-query 5.99.0 → 5.100.1 prettier-plugin-tailwindcss 0.7.2 → 0.7.3 react-i18next 17.0.2 → 17.0.4 react-router-dom 7.14.1 → 7.14.2 tailwindcss 4.2.2 → 4.2.4 typescript 6.0.2 → 6.0.3 @scure/bip32 2.0.1 → 2.2.0 @scure/bip39 2.0.1 → 2.2.0 eslint-plugin-react-hooks 7.0.1 → 7.1.1 lucide-react 1.8.0 → 1.9.0 react-hook-form 7.72.1 → 7.73.1 typescript-eslint 8.58.2 → 8.59.0 * chore(build): fix violations after eslint plugin updates * chore: check window availability in useIsMobile * build(deps): update dependencies @tanstack/eslint-plugin-query 5.100.1 → 5.100.6 @tanstack/react-query 5.100.1 → 5.100.6 prettier-plugin-tailwindcss 0.7.3 → 0.7.4 react-i18next 17.0.4 → 17.0.6 typescript-eslint 8.59.0 → 8.59.1 * build(deps): update dependencies jsdom 29.0.2 → 29.1.0 lucide-react 1.9.0 → 1.13.0 prettier-plugin-tailwindcss 0.7.4 → 0.8.0 react-hook-form 7.73.1 → 7.74.0 * build(ci): check format * chore(ci): run pipeline for master and devel
64 lines
2 KiB
TypeScript
64 lines
2 KiB
TypeScript
import { QueryClient } from '@tanstack/react-query'
|
|
import type { MutationFunction, QueryFunction, QueryKey } from '@tanstack/react-query'
|
|
import type { Milliseconds } from '@/types/global'
|
|
import { delayedPromise } from './utils'
|
|
|
|
export const queryClient = new QueryClient({
|
|
defaultOptions: {
|
|
queries: {
|
|
refetchOnWindowFocus: false,
|
|
retryDelay: (attemptIndex) => Math.min(1_000 * 2 ** attemptIndex, 30_000),
|
|
retry: false,
|
|
},
|
|
mutations: {
|
|
retry: false,
|
|
},
|
|
},
|
|
})
|
|
|
|
type WithQueryDelayOptions = {
|
|
delayBefore?: Milliseconds
|
|
delayAfter?: Milliseconds
|
|
throttle?: Milliseconds
|
|
}
|
|
|
|
export function withQueryDelay<TQueryFnData, TQueryKey extends QueryKey>(
|
|
queryFn: QueryFunction<TQueryFnData, TQueryKey> | undefined,
|
|
delayOptions: WithQueryDelayOptions,
|
|
): QueryFunction<TQueryFnData, TQueryKey> | undefined {
|
|
if (queryFn === undefined) return undefined
|
|
return async (context) => {
|
|
return await applyDelay(async () => await queryFn(context), delayOptions)
|
|
}
|
|
}
|
|
|
|
export function withMutationDelay<TMutateFnData, TVariables = void>(
|
|
queryFn: MutationFunction<TMutateFnData, TVariables>,
|
|
delayOptions: WithQueryDelayOptions,
|
|
): MutationFunction<TMutateFnData, TVariables> {
|
|
return async (variables, options) => {
|
|
return await applyDelay(async () => await queryFn(variables, options), delayOptions)
|
|
}
|
|
}
|
|
|
|
async function applyDelay<T>(
|
|
queryFn: () => Promise<T>,
|
|
{ delayBefore, delayAfter, throttle }: WithQueryDelayOptions,
|
|
): Promise<T> {
|
|
const now = globalThis.performance.now()
|
|
if (delayBefore !== undefined && delayBefore > 0) {
|
|
await delayedPromise(delayBefore)
|
|
}
|
|
const result = await queryFn()
|
|
if (delayAfter !== undefined && delayAfter > 0) {
|
|
await delayedPromise(delayAfter)
|
|
}
|
|
if (throttle != undefined && throttle > 0) {
|
|
const requestDuration = globalThis.performance.now() - now
|
|
const throttleDelay = Math.max(0, throttle - requestDuration)
|
|
if (throttleDelay > 0) {
|
|
await delayedPromise(throttleDelay)
|
|
}
|
|
}
|
|
return result
|
|
}
|