Explorar o código

Merge pull request #3170 from overleaf/fix-editor-race

discard previous doc load when new doc opened

GitOrigin-RevId: 0bbbb94b6ca5ebd4b5a0b4e3472f705d5337cdf5
Timothée Alby %!s(int64=6) %!d(string=hai) anos
pai
achega
8f4ddcbbbb
Modificáronse 1 ficheiros con 16 adicións e 1 borrados
  1. 16 1
      services/web/frontend/js/ide/editor/EditorManager.js

+ 16 - 1
services/web/frontend/js/ide/editor/EditorManager.js

@@ -28,6 +28,7 @@ export default (EditorManager = (function() {
     }
     constructor(ide, $scope, localStorage) {
       this.ide = ide
+      this.editorOpenDocEpoch = 0 // track pending document loads
       this.$scope = $scope
       this.localStorage = localStorage
       this.$scope.editor = {
@@ -178,6 +179,12 @@ export default (EditorManager = (function() {
 
       this.$scope.editor.opening = true
       return this._openNewDocument(doc, (error, sharejs_doc) => {
+        if (error && error.message === 'another document was loaded') {
+          sl_console.log(
+            `[openDoc] another document was loaded while ${doc.id} was loading`
+          )
+          return
+        }
         if (error != null) {
           this.ide.showGenericMessageModal(
             'Error opening document',
@@ -216,11 +223,19 @@ export default (EditorManager = (function() {
         current_sharejs_doc.leaveAndCleanUp()
         this._unbindFromDocumentEvents(current_sharejs_doc)
       }
-
+      const editorOpenDocEpoch = ++this.editorOpenDocEpoch
       return new_sharejs_doc.join(error => {
         if (error != null) {
           return callback(error)
         }
+        if (this.editorOpenDocEpoch !== editorOpenDocEpoch) {
+          sl_console.log(
+            `[openNewDocument] editorOpenDocEpoch mismatch ${
+              this.editorOpenDocEpoch
+            } vs ${editorOpenDocEpoch}`
+          )
+          return callback(new Error('another document was loaded'))
+        }
         this._bindToDocumentEvents(doc, new_sharejs_doc)
         return callback(null, new_sharejs_doc)
       })