Skip to content

Commit

Permalink
fix: fix usage with react strict mode
Browse files Browse the repository at this point in the history
immediately unregister file
  • Loading branch information
Loïc Mangeonjean committed Jun 11, 2024
1 parent 85d6e70 commit 01a6732
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/MonacoEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,15 @@ function MonacoEditor ({
editorRef.current!.setModel(null)
return
}
let cancelled = false
async function updateModel () {
const disposableStore = new DisposableStore()
async function updateModel (): Promise<void> {
modelRef.current = undefined
editorRef.current!.setModel(null)
setModelReady(false)

const value = valueRef.current
let modelIRef: IReference<ITextFileEditorModel> | undefined
let model: monaco.editor.ITextModel
const disposableStore = new DisposableStore()
if (fileUri != null) {
const uri = monaco.Uri.parse(fileUri)

Expand All @@ -224,9 +223,8 @@ function MonacoEditor ({
}
})
modelIRef = (await modelIRefPromise)!
if (cancelled) {
modelIRef.dispose()
return () => {}
if (disposableStore.isDisposed) {
return
}
disposableStore.add(modelIRef.object.onDidSave(lastOnDidSave))
model = modelIRef.object.textEditorModel!
Expand All @@ -243,17 +241,17 @@ function MonacoEditor ({
if (editorRef.current != null) {
lastRestoreViewState(editorRef.current, model)
}
return () => {
if (editorRef.current != null) {
lastSaveViewState(editorRef.current, model)
disposableStore.add({
dispose () {
if (editorRef.current != null) {
lastSaveViewState(editorRef.current, model)
}
}
disposableStore.dispose()
}
})
}
const disposePromise = updateModel()
updateModel().catch(console.error)
return () => {
cancelled = true
disposePromise.then(dispose => dispose(), console.error)
disposableStore.dispose()
}
}, [monacoLanguage, fileUri, valueRef, lastSaveViewState, lastRestoreViewState, hasValue, lastOnDidSave])

Expand Down

0 comments on commit 01a6732

Please sign in to comment.