Wrap all view.dispatch in setTimeout (#21590)

GitOrigin-RevId: a78e314683642e173c61b0bef95b30f5b96c82b5
This commit is contained in:
Alf Eaton 2024-11-07 09:17:17 +00:00 committed by Copybot
parent 9352b51d28
commit 31e7f113f8

View file

@ -376,6 +376,7 @@ function useCodeMirrorScope(view: EditorView) {
if (docName) { if (docName) {
docNameRef.current = docName docNameRef.current = docName
window.setTimeout(() => {
view.dispatch( view.dispatch(
setDocName(docNameRef.current), setDocName(docNameRef.current),
setLanguage( setLanguage(
@ -384,65 +385,84 @@ function useCodeMirrorScope(view: EditorView) {
settingsRef.current.syntaxValidation settingsRef.current.syntaxValidation
) )
) )
})
} }
}, [view, docName]) }, [view, docName])
useEffect(() => { useEffect(() => {
visualRef.current.visual = showVisual visualRef.current.visual = showVisual
window.setTimeout(() => {
view.dispatch(setVisual(visualRef.current)) view.dispatch(setVisual(visualRef.current))
view.dispatch({ view.dispatch({
effects: EditorView.scrollIntoView(view.state.selection.main.head), effects: EditorView.scrollIntoView(view.state.selection.main.head),
}) })
// clear performance measures and marks when switching between Source and Rich Text // clear performance measures and marks when switching between Source and Rich Text
window.dispatchEvent(new Event('editor:visual-switch')) window.dispatchEvent(new Event('editor:visual-switch'))
})
}, [view, showVisual]) }, [view, showVisual])
useEffect(() => { useEffect(() => {
visualRef.current.previewByPath = previewByPath visualRef.current.previewByPath = previewByPath
window.setTimeout(() => {
view.dispatch(setVisual(visualRef.current)) view.dispatch(setVisual(visualRef.current))
})
}, [view, previewByPath]) }, [view, previewByPath])
useEffect(() => { useEffect(() => {
editableRef.current = permissions.write editableRef.current = permissions.write
window.setTimeout(() => {
view.dispatch(setEditable(editableRef.current)) // the editor needs to be locked when there's a problem saving data view.dispatch(setEditable(editableRef.current)) // the editor needs to be locked when there's a problem saving data
})
}, [view, permissions.write]) }, [view, permissions.write])
useEffect(() => { useEffect(() => {
phrasesRef.current = phrases phrasesRef.current = phrases
window.setTimeout(() => {
view.dispatch(setPhrases(phrases)) view.dispatch(setPhrases(phrases))
})
}, [view, phrases]) }, [view, phrases])
// listen to editor settings updates // listen to editor settings updates
useEffect(() => { useEffect(() => {
settingsRef.current.autoPairDelimiters = autoPairDelimiters settingsRef.current.autoPairDelimiters = autoPairDelimiters
window.setTimeout(() => {
view.dispatch(setAutoPair(autoPairDelimiters)) view.dispatch(setAutoPair(autoPairDelimiters))
})
}, [view, autoPairDelimiters]) }, [view, autoPairDelimiters])
useEffect(() => { useEffect(() => {
settingsRef.current.autoComplete = autoComplete settingsRef.current.autoComplete = autoComplete
window.setTimeout(() => {
view.dispatch( view.dispatch(
setAutoComplete({ setAutoComplete({
enabled: autoComplete, enabled: autoComplete,
projectFeatures: projectFeaturesRef.current, projectFeatures: projectFeaturesRef.current,
}) })
) )
})
}, [view, autoComplete]) }, [view, autoComplete])
useEffect(() => { useEffect(() => {
settingsRef.current.mode = mode settingsRef.current.mode = mode
setKeybindings(mode).then(spec => { setKeybindings(mode).then(spec => {
window.setTimeout(() => {
view.dispatch(spec) view.dispatch(spec)
}) })
})
}, [view, mode]) }, [view, mode])
useEffect(() => { useEffect(() => {
settingsRef.current.syntaxValidation = syntaxValidation settingsRef.current.syntaxValidation = syntaxValidation
window.setTimeout(() => {
view.dispatch(setSyntaxValidation(syntaxValidation)) view.dispatch(setSyntaxValidation(syntaxValidation))
})
}, [view, syntaxValidation]) }, [view, syntaxValidation])
useEffect(() => { useEffect(() => {
settingsRef.current.mathPreview = mathPreview settingsRef.current.mathPreview = mathPreview
window.setTimeout(() => {
view.dispatch(setMathPreview(mathPreview)) view.dispatch(setMathPreview(mathPreview))
})
}, [view, mathPreview]) }, [view, mathPreview])
const emitSyncToPdf = useScopeEventEmitter('cursor:editor:syncToPdf') const emitSyncToPdf = useScopeEventEmitter('cursor:editor:syncToPdf')
@ -511,10 +531,9 @@ function useCodeMirrorScope(view: EditorView) {
// enable/disable the compile log linter as appropriate // enable/disable the compile log linter as appropriate
useEffect(() => { useEffect(() => {
// dispatch in a timeout, so the dispatch isn't in the same cycle as the edit which caused it
window.setTimeout(() => { window.setTimeout(() => {
view.dispatch(showCompileLogDiagnostics(enableCompileLogLinter)) view.dispatch(showCompileLogDiagnostics(enableCompileLogLinter))
}, 0) })
}, [view, enableCompileLogLinter]) }, [view, enableCompileLogLinter])
// set the compile log annotations when they change // set the compile log annotations when they change
@ -522,7 +541,6 @@ function useCodeMirrorScope(view: EditorView) {
if (currentDoc && logEntryAnnotations) { if (currentDoc && logEntryAnnotations) {
const annotations = logEntryAnnotations[currentDoc.doc_id] const annotations = logEntryAnnotations[currentDoc.doc_id]
// dispatch in a timeout, so the dispatch isn't in the same cycle as the edit which caused it
window.setTimeout(() => { window.setTimeout(() => {
view.dispatch( view.dispatch(
setAnnotations(view.state, annotations || []), setAnnotations(view.state, annotations || []),
@ -563,7 +581,9 @@ function useCodeMirrorScope(view: EditorView) {
const handleRemoveLearnedWords = useCallback( const handleRemoveLearnedWords = useCallback(
(event: CustomEvent<string>) => { (event: CustomEvent<string>) => {
window.setTimeout(() => {
view.dispatch(removeLearnedWord(spellCheckLanguage, event.detail)) view.dispatch(removeLearnedWord(spellCheckLanguage, event.detail))
})
}, },
[spellCheckLanguage, view] [spellCheckLanguage, view]
) )
@ -571,7 +591,9 @@ function useCodeMirrorScope(view: EditorView) {
useEventListener('learnedWords:remove', handleRemoveLearnedWords) useEventListener('learnedWords:remove', handleRemoveLearnedWords)
const handleResetLearnedWords = useCallback(() => { const handleResetLearnedWords = useCallback(() => {
window.setTimeout(() => {
view.dispatch(resetLearnedWords()) view.dispatch(resetLearnedWords())
})
}, [view]) }, [view])
useEventListener('learnedWords:reset', handleResetLearnedWords) useEventListener('learnedWords:reset', handleResetLearnedWords)
@ -584,7 +606,9 @@ function useCodeMirrorScope(view: EditorView) {
) )
useEffect(() => { useEffect(() => {
window.setTimeout(() => {
view.dispatch(reviewPanelToggled()) view.dispatch(reviewPanelToggled())
})
}, [reviewPanelOpen, miniReviewPanelVisible, view]) }, [reviewPanelOpen, miniReviewPanelVisible, view])
} }