From d989e1e942a77749c31e82296dd6c83a97184fd0 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Tue, 8 Feb 2022 17:45:04 +0200 Subject: Show timing info --- src/api.ts | 30 ++++++------------------------ 1 file changed, 6 insertions(+), 24 deletions(-) (limited to 'src/api.ts') diff --git a/src/api.ts b/src/api.ts index be0c7a1..2670e3c 100644 --- a/src/api.ts +++ b/src/api.ts @@ -76,35 +76,14 @@ export const useExamples = () => mapSWRResult - -const postSource = async (text: string): Promise => { - const apiResult = await postRequest( - { - baseUrl: RUNNER_API_URL, - path: RunnerApi.SubmitRequest.path, - body: { - source: text, - }, - }, - ) - - switch (apiResult.type) { - case "success": - return { type: "success", data: apiResult.data.result } - case "failed": - return { type: "failed", error: apiResult.error } - case "not_yet_requested": - case "loading": - return apiResult - } -} +export type SubmitResult = APIResult export const useSubmitSource = () => { const [result, setResult] = useState({ type: "not_yet_requested" }) const submitSource = async (text: string) => { try { + const startRoundtripTime = performance.now() setResult({ type: "loading" }) const apiResult = await postRequest( { @@ -115,10 +94,13 @@ export const useSubmitSource = () => { }, }, ) + const endRoundtripTime = performance.now() + const roundtripTime = endRoundtripTime - startRoundtripTime switch (apiResult.type) { case "success": - setResult({ type: "success", data: apiResult.data.result }) + setResult({ type: "success", data: { ...apiResult.data, roundtripTime }}) break + case "failed": setResult({ type: "failed", error: apiResult.error }) break -- cgit v1.3