forked from iway1/trpc-panel
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e3b7cc8
commit b57e389
Showing
5 changed files
with
66 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/trpc-ui/src/react-app/components/hooks/useAsyncDuration.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { useState } from "react"; | ||
|
||
export function useAsyncDuration() { | ||
const [duration, setDuration] = useState<number | null>(null); | ||
const [loading, setLoading] = useState(false); | ||
|
||
const measureAsyncDuration = async <T,>( | ||
asyncFunction: () => Promise<T>, | ||
): Promise<T> => { | ||
setLoading(true); | ||
const startTime = performance.now(); | ||
|
||
try { | ||
const result = await asyncFunction(); | ||
const endTime = performance.now(); | ||
setDuration(endTime - startTime); | ||
return result; | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
return { duration, loading, measureAsyncDuration }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters