From dba62b4677fb8df2647ef0baf2746cbe663d0ce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Fri, 29 Dec 2023 19:47:21 -0500 Subject: [PATCH] refactor(client-side): add debounced versions of functions split autoEdit and autoSave into debounced and non-debounced versions --- lib/client/utils.ts | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/lib/client/utils.ts b/lib/client/utils.ts index e8f9178..8c71bf4 100644 --- a/lib/client/utils.ts +++ b/lib/client/utils.ts @@ -3,7 +3,7 @@ import { message } from "ant-design-vue"; import { IStory } from "@models/stories"; import { useAutoSaveStore } from "~/stores/autosaveStore"; -export const autoSave = debounce(async (values: any) => { +export const autoSave = async (values: any) => { const store = useAutoSaveStore(); const fid = store.$state.fetchId; if (store.$state.fetchId !== fid) return; @@ -25,21 +25,25 @@ export const autoSave = debounce(async (values: any) => { body: values, }); } -}, 20000); +}; -export const autoEdit = debounce( - (values: any, endpoint: string, method: "put" | "post") => { - const [messageApi, contextHolder] = message.useMessage(); - useApiFetch<{ success: boolean; data: IStory }>(endpoint, { - method, - body: values, - }).then(({ data, error }) => { - if (data.value?.success) { - messageApi.success("Your work has been saved successfully."); - } else if (error) { - messageApi.error("Error saving data."); - } - }); - }, - 10000, -); +export const autoEdit = ( + values: any, + endpoint: string, + method: "put" | "post", +) => { + const [messageApi, contextHolder] = message.useMessage(); + useApiFetch<{ success: boolean; data: IStory }>(endpoint, { + method, + body: values, + }).then(({ data, error }) => { + if (data.value?.success) { + messageApi.success("Your work has been saved successfully."); + } else if (error) { + messageApi.error("Error saving data."); + } + }); +}; + +export const debouncedAutoEdit = debounce(autoEdit, 5000); +export const debouncedAutoSave = debounce(autoSave, 10000);