Skip to content

Commit

Permalink
update cleanup logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kwkr committed Mar 19, 2024
1 parent f3ac9bf commit 0ab0569
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions components/Editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const Editor = ({ readOnly = false }: Props) => {

const editorRef = useRef<SCEditorRef>()
const [showArgumentsHelper, setShowArgumentsHelper] = useState(false)
const timeoutRef = useRef<NodeJS.Timeout | null>(null)

useEffect(() => {
const query = router.query
Expand Down Expand Up @@ -293,16 +294,17 @@ 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,
selectionEnd + charOffsetEnd,
),
0,
)

return () => clearTimeout(timeoutId)
}, [cairoCode])

useEffect(() => {
Expand All @@ -318,6 +320,14 @@ const Editor = ({ readOnly = false }: Props) => {
}
}, [handleCommentLine, cairoCode])

useEffect(() => {
return () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current)
}
}
}, [])

return (
<>
<div className="bg-gray-100 dark:bg-black-700 rounded-lg">
Expand Down

0 comments on commit 0ab0569

Please sign in to comment.