diff options
Diffstat (limited to 'packages/merchant-backoffice-ui/src/hooks/async.ts')
-rw-r--r-- | packages/merchant-backoffice-ui/src/hooks/async.ts | 45 |
1 files changed, 23 insertions, 22 deletions
diff --git a/packages/merchant-backoffice-ui/src/hooks/async.ts b/packages/merchant-backoffice-ui/src/hooks/async.ts index 69cb231a4..212ef2211 100644 --- a/packages/merchant-backoffice-ui/src/hooks/async.ts +++ b/packages/merchant-backoffice-ui/src/hooks/async.ts @@ -1,6 +1,6 @@ /* This file is part of GNU Taler - (C) 2021 Taler Systems S.A. + (C) 2021-2024 Taler Systems S.A. GNU Taler is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -15,38 +15,40 @@ */ /** -* -* @author Sebastian Javier Marchano (sebasjm) -*/ + * + * @author Sebastian Javier Marchano (sebasjm) + */ import { useState } from "preact/hooks"; -import { cancelPendingRequest } from "./backend.js"; export interface Options { - slowTolerance: number, + slowTolerance: number; } export interface AsyncOperationApi<T> { - request: (...a: any) => void, - cancel: () => void, - data: T | undefined, - isSlow: boolean, - isLoading: boolean, - error: string | undefined + request: (...a: any) => void; + cancel: () => void; + data: T | undefined; + isSlow: boolean; + isLoading: boolean; + error: string | undefined; } -export function useAsync<T>(fn?: (...args: any) => Promise<T>, { slowTolerance: tooLong }: Options = { slowTolerance: 1000 }): AsyncOperationApi<T> { +export function useAsync<T>( + fn?: (...args: any) => Promise<T>, + { slowTolerance: tooLong }: Options = { slowTolerance: 1000 }, +): AsyncOperationApi<T> { const [data, setData] = useState<T | undefined>(undefined); const [isLoading, setLoading] = useState<boolean>(false); const [error, setError] = useState<any>(undefined); - const [isSlow, setSlow] = useState(false) + const [isSlow, setSlow] = useState(false); const request = async (...args: any) => { if (!fn) return; setLoading(true); const handler = setTimeout(() => { - setSlow(true) - }, tooLong) + setSlow(true); + }, tooLong); try { const result = await fn(...args); @@ -55,14 +57,13 @@ export function useAsync<T>(fn?: (...args: any) => Promise<T>, { slowTolerance: setError(error); } setLoading(false); - setSlow(false) - clearTimeout(handler) + setSlow(false); + clearTimeout(handler); }; - function cancel() { - cancelPendingRequest() + function cancel(): void { setLoading(false); - setSlow(false) + setSlow(false); } return { @@ -71,6 +72,6 @@ export function useAsync<T>(fn?: (...args: any) => Promise<T>, { slowTolerance: data, isSlow, isLoading, - error + error, }; } |