diff --git a/src/Hooks/UseDebounce.tsx b/src/Hooks/UseDebounce.tsx deleted file mode 100644 index 91b1a79..0000000 --- a/src/Hooks/UseDebounce.tsx +++ /dev/null @@ -1,32 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -/* -Copyright (C) 2023 The Falco Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -import { useEffect, useState } from "react"; - -export function useDebounce(value: T, delay?: number): T { - const [debouncedValue, setDebouncedValue] = useState(value); - - useEffect(() => { - const timer = setTimeout(() => setDebouncedValue(value), delay || 500); - - return () => { - clearTimeout(timer); - }; - }, [value, delay]); - - return debouncedValue; -} diff --git a/src/components/Editor/Monaco.tsx b/src/components/Editor/Monaco.tsx index 6a58b9e..a6bc910 100644 --- a/src/components/Editor/Monaco.tsx +++ b/src/components/Editor/Monaco.tsx @@ -54,6 +54,9 @@ const Monaco = () => { const originalURL = window.location.origin + window.location.pathname; + //debounce code + let debounce: NodeJS.Timeout; + useEffect(() => { if (monacoEL) { setEditor((editor) => { @@ -164,7 +167,10 @@ const Monaco = () => { }; squiggly(); editor?.getModel().onDidChangeContent(() => { - dispatch(autosave(editor.getModel().getValue())); + if (debounce) clearTimeout(debounce); + debounce = setTimeout(() => { + dispatch(autosave(editor.getModel().getValue())); + }, 500); }); return ; };