diff --git a/services/web/frontend/js/features/ide-react/editor/document.ts b/services/web/frontend/js/features/ide-react/editor/document.ts index 76d1e49e43..8a7ce6a251 100644 --- a/services/web/frontend/js/features/ide-react/editor/document.ts +++ b/services/web/frontend/js/features/ide-react/editor/document.ts @@ -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() } diff --git a/services/web/frontend/js/features/ide-react/editor/open-documents.ts b/services/web/frontend/js/features/ide-react/editor/open-documents.ts index 3efbbe514b..1e631d8eba 100644 --- a/services/web/frontend/js/features/ide-react/editor/open-documents.ts +++ b/services/web/frontend/js/features/ide-react/editor/open-documents.ts @@ -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() {