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,73 +376,93 @@ function useCodeMirrorScope(view: EditorView) {
if (docName) { if (docName) {
docNameRef.current = docName docNameRef.current = docName
view.dispatch( window.setTimeout(() => {
setDocName(docNameRef.current), view.dispatch(
setLanguage( setDocName(docNameRef.current),
docNameRef.current, setLanguage(
metadataRef.current, docNameRef.current,
settingsRef.current.syntaxValidation metadataRef.current,
settingsRef.current.syntaxValidation
)
) )
) })
} }
}, [view, docName]) }, [view, docName])
useEffect(() => { useEffect(() => {
visualRef.current.visual = showVisual visualRef.current.visual = showVisual
view.dispatch(setVisual(visualRef.current)) window.setTimeout(() => {
view.dispatch({ view.dispatch(setVisual(visualRef.current))
effects: EditorView.scrollIntoView(view.state.selection.main.head), view.dispatch({
effects: EditorView.scrollIntoView(view.state.selection.main.head),
})
// clear performance measures and marks when switching between Source and Rich Text
window.dispatchEvent(new Event('editor:visual-switch'))
}) })
// clear performance measures and marks when switching between Source and Rich Text
window.dispatchEvent(new Event('editor:visual-switch'))
}, [view, showVisual]) }, [view, showVisual])
useEffect(() => { useEffect(() => {
visualRef.current.previewByPath = previewByPath visualRef.current.previewByPath = previewByPath
view.dispatch(setVisual(visualRef.current)) window.setTimeout(() => {
view.dispatch(setVisual(visualRef.current))
})
}, [view, previewByPath]) }, [view, previewByPath])
useEffect(() => { useEffect(() => {
editableRef.current = permissions.write editableRef.current = permissions.write
view.dispatch(setEditable(editableRef.current)) // the editor needs to be locked when there's a problem saving data window.setTimeout(() => {
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
view.dispatch(setPhrases(phrases)) window.setTimeout(() => {
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
view.dispatch(setAutoPair(autoPairDelimiters)) window.setTimeout(() => {
view.dispatch(setAutoPair(autoPairDelimiters))
})
}, [view, autoPairDelimiters]) }, [view, autoPairDelimiters])
useEffect(() => { useEffect(() => {
settingsRef.current.autoComplete = autoComplete settingsRef.current.autoComplete = autoComplete
view.dispatch( window.setTimeout(() => {
setAutoComplete({ view.dispatch(
enabled: autoComplete, setAutoComplete({
projectFeatures: projectFeaturesRef.current, enabled: autoComplete,
}) 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 => {
view.dispatch(spec) window.setTimeout(() => {
view.dispatch(spec)
})
}) })
}, [view, mode]) }, [view, mode])
useEffect(() => { useEffect(() => {
settingsRef.current.syntaxValidation = syntaxValidation settingsRef.current.syntaxValidation = syntaxValidation
view.dispatch(setSyntaxValidation(syntaxValidation)) window.setTimeout(() => {
view.dispatch(setSyntaxValidation(syntaxValidation))
})
}, [view, syntaxValidation]) }, [view, syntaxValidation])
useEffect(() => { useEffect(() => {
settingsRef.current.mathPreview = mathPreview settingsRef.current.mathPreview = mathPreview
view.dispatch(setMathPreview(mathPreview)) window.setTimeout(() => {
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>) => {
view.dispatch(removeLearnedWord(spellCheckLanguage, event.detail)) window.setTimeout(() => {
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(() => {
view.dispatch(resetLearnedWords()) window.setTimeout(() => {
view.dispatch(resetLearnedWords())
})
}, [view]) }, [view])
useEventListener('learnedWords:reset', handleResetLearnedWords) useEventListener('learnedWords:reset', handleResetLearnedWords)
@ -584,7 +606,9 @@ function useCodeMirrorScope(view: EditorView) {
) )
useEffect(() => { useEffect(() => {
view.dispatch(reviewPanelToggled()) window.setTimeout(() => {
view.dispatch(reviewPanelToggled())
})
}, [reviewPanelOpen, miniReviewPanelVisible, view]) }, [reviewPanelOpen, miniReviewPanelVisible, view])
} }