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

discard previous doc load when new doc opened

GitOrigin-RevId: 0bbbb94b6ca5ebd4b5a0b4e3472f705d5337cdf5
This commit is contained in:
Timothée Alby 2020-09-15 14:48:40 +02:00 committed by Copybot
parent d0b6e877dc
commit 8f4ddcbbbb

View file

@ -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)
})