From 0ab05695875ef92f6efe569c3548a55361bef145 Mon Sep 17 00:00:00 2001 From: kwkr Date: Tue, 19 Mar 2024 11:59:48 +0100 Subject: [PATCH] update cleanup logic --- components/Editor/index.tsx | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/components/Editor/index.tsx b/components/Editor/index.tsx index 904af9b..430f946 100644 --- a/components/Editor/index.tsx +++ b/components/Editor/index.tsx @@ -77,6 +77,7 @@ const Editor = ({ readOnly = false }: Props) => { const editorRef = useRef() const [showArgumentsHelper, setShowArgumentsHelper] = useState(false) + const timeoutRef = useRef(null) useEffect(() => { const query = router.query @@ -293,7 +294,10 @@ const Editor = ({ readOnly = false }: Props) => { } setCairoCode(lines.join('\n')) - const timeoutId = setTimeout( + if (timeoutRef.current) { + clearTimeout(timeoutRef.current) + } + timeoutRef.current = setTimeout( () => textareaRef.setSelectionRange( selectionStart + charOffsetStart, @@ -301,8 +305,6 @@ const Editor = ({ readOnly = false }: Props) => { ), 0, ) - - return () => clearTimeout(timeoutId) }, [cairoCode]) useEffect(() => { @@ -318,6 +320,14 @@ const Editor = ({ readOnly = false }: Props) => { } }, [handleCommentLine, cairoCode]) + useEffect(() => { + return () => { + if (timeoutRef.current) { + clearTimeout(timeoutRef.current) + } + } + }, []) + return ( <>