Просмотр исходного кода

Call detachDoc when cleaning up a doc (#16262)

GitOrigin-RevId: a7479e3685e552c2c3b73ed18a333d647f228c3e
Alf Eaton 2 лет назад
Родитель
Сommit
731f071a4e

+ 5 - 3
services/web/frontend/js/features/ide-react/editor/document.ts

@@ -102,7 +102,8 @@ export class Document extends EventEmitter {
     readonly socket: Socket,
     readonly socket: Socket,
     private readonly globalEditorWatchdogManager: EditorWatchdogManager,
     private readonly globalEditorWatchdogManager: EditorWatchdogManager,
     private readonly ideEventEmitter: IdeEventEmitter,
     private readonly ideEventEmitter: IdeEventEmitter,
-    private readonly eventLog: EventLog
+    private readonly eventLog: EventLog,
+    private readonly detachDoc: (docId: string, doc: Document) => void
   ) {
   ) {
     super()
     super()
     this.connected = this.socket.socket.connected
     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`
         `[cleanUp] Document (${this.doc_id}) has buffered ops, refusing to remove from openDocs`
       )
       )
       return // return immediately, do not unbind from events
       return // return immediately, do not unbind from events
-    } else {
-      this.emit('detach', this.doc_id)
     }
     }
+
+    this.detachDoc(this.doc_id, this)
+
     this.unBindFromEditorEvents()
     this.unBindFromEditorEvents()
     this.unBindFromSocketEvents()
     this.unBindFromSocketEvents()
   }
   }

+ 14 - 4
services/web/frontend/js/features/ide-react/editor/open-documents.ts

@@ -49,16 +49,26 @@ export class OpenDocuments {
       this.socket,
       this.socket,
       this.globalEditorWatchdogManager,
       this.globalEditorWatchdogManager,
       this.events,
       this.events,
-      this.eventLog
+      this.eventLog,
+      this.detachDoc.bind(this)
     )
     )
     this.openDocs.set(docId, doc)
     this.openDocs.set(docId, doc)
-    doc.on('detach', () => {
+  }
+
+  detachDoc(docId: string, doc: Document) {
+    if (this.openDocs.get(docId) === doc) {
       debugConsole.log(
       debugConsole.log(
         `[detach] Removing document with ID (${docId}) from openDocs`
         `[detach] Removing document with ID (${docId}) from openDocs`
       )
       )
-      doc.off('detach')
       this.openDocs.delete(docId)
       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() {
   hasUnsavedChanges() {