Call detachDoc when cleaning up a doc (#16262)

GitOrigin-RevId: a7479e3685e552c2c3b73ed18a333d647f228c3e
This commit is contained in:
Alf Eaton
2023-12-15 12:18:56 +00:00
committed by Copybot
parent fbed0cb943
commit 731f071a4e
2 changed files with 19 additions and 7 deletions
@@ -102,7 +102,8 @@ export class Document extends EventEmitter {
readonly socket: Socket,
private readonly globalEditorWatchdogManager: EditorWatchdogManager,
private readonly ideEventEmitter: IdeEventEmitter,
private readonly eventLog: EventLog
private readonly eventLog: EventLog,
private readonly detachDoc: (docId: string, doc: Document) => void
) {
super()
this.connected = this.socket.socket.connected
@@ -550,9 +551,10 @@ export class Document extends EventEmitter {
`[cleanUp] Document (${this.doc_id}) has buffered ops, refusing to remove from openDocs`
)
return // return immediately, do not unbind from events
} else {
this.emit('detach', this.doc_id)
}
this.detachDoc(this.doc_id, this)
this.unBindFromEditorEvents()
this.unBindFromSocketEvents()
}
@@ -49,16 +49,26 @@ export class OpenDocuments {
this.socket,
this.globalEditorWatchdogManager,
this.events,
this.eventLog
this.eventLog,
this.detachDoc.bind(this)
)
this.openDocs.set(docId, doc)
doc.on('detach', () => {
}
detachDoc(docId: string, doc: Document) {
if (this.openDocs.get(docId) === doc) {
debugConsole.log(
`[detach] Removing document with ID (${docId}) from openDocs`
)
doc.off('detach')
this.openDocs.delete(docId)
})
} else {
// It's possible that this instance has error, and the doc has been reloaded.
// This creates a new instance in Document.openDoc with the same id. We shouldn't
// clear it because it's not this instance.
debugConsole.log(
`[_cleanUp] New instance of (${docId}) created. Not removing`
)
}
}
hasUnsavedChanges() {