From 72b429321553841ac1ff48cf974bfc65da01bb06 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 19 Dec 2022 12:23:39 -0300 Subject: pretty --- packages/merchant-backoffice-ui/src/hooks/async.ts | 41 ++++++++++++---------- 1 file changed, 22 insertions(+), 19 deletions(-) (limited to 'packages/merchant-backoffice-ui/src/hooks/async.ts') diff --git a/packages/merchant-backoffice-ui/src/hooks/async.ts b/packages/merchant-backoffice-ui/src/hooks/async.ts index a842c73e4..6c116e628 100644 --- a/packages/merchant-backoffice-ui/src/hooks/async.ts +++ b/packages/merchant-backoffice-ui/src/hooks/async.ts @@ -15,38 +15,41 @@ */ /** -* -* @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 { - 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(fn?: (...args: any) => Promise, { slowTolerance: tooLong }: Options = { slowTolerance: 1000 }): AsyncOperationApi { +export function useAsync( + fn?: (...args: any) => Promise, + { slowTolerance: tooLong }: Options = { slowTolerance: 1000 }, +): AsyncOperationApi { const [data, setData] = useState(undefined); const [isLoading, setLoading] = useState(false); const [error, setError] = useState(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 +58,14 @@ export function useAsync(fn?: (...args: any) => Promise, { slowTolerance: setError(error); } setLoading(false); - setSlow(false) - clearTimeout(handler) + setSlow(false); + clearTimeout(handler); }; function cancel() { - cancelPendingRequest() + cancelPendingRequest(); setLoading(false); - setSlow(false) + setSlow(false); } return { @@ -71,6 +74,6 @@ export function useAsync(fn?: (...args: any) => Promise, { slowTolerance: data, isSlow, isLoading, - error + error, }; } -- cgit v1.2.3