From 45c5287d53b7ce6725c507880217bde7b7e5dd3d Mon Sep 17 00:00:00 2001 From: zadam Date: Tue, 10 Dec 2019 22:35:24 +0100 Subject: [PATCH] protection against note switching race conditions --- .../javascripts/services/note_detail_code.js | 21 +++++++++++-------- .../services/note_detail_relation_map.js | 13 +++++++----- .../javascripts/services/note_detail_text.js | 9 +++++--- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/public/javascripts/services/note_detail_code.js b/src/public/javascripts/services/note_detail_code.js index bea8a4e0f..f34d16f8c 100644 --- a/src/public/javascripts/services/note_detail_code.js +++ b/src/public/javascripts/services/note_detail_code.js @@ -55,18 +55,21 @@ class NoteDetailCode { this.onNoteChange(() => this.ctx.noteChanged()); } - // CodeMirror breaks pretty badly on null so even though it shouldn't happen (guarded by consistency check) - // we provide fallback - this.codeEditor.setValue(this.ctx.note.content || ""); + // lazy loading above can take time and tab might have been already switched to another note + if (this.ctx.note && this.ctx.note.type === 'code') { + // CodeMirror breaks pretty badly on null so even though it shouldn't happen (guarded by consistency check) + // we provide fallback + this.codeEditor.setValue(this.ctx.note.content || ""); - const info = CodeMirror.findModeByMIME(this.ctx.note.mime); + const info = CodeMirror.findModeByMIME(this.ctx.note.mime); - if (info) { - this.codeEditor.setOption("mode", info.mime); - CodeMirror.autoLoadMode(this.codeEditor, info.mode); + if (info) { + this.codeEditor.setOption("mode", info.mime); + CodeMirror.autoLoadMode(this.codeEditor, info.mode); + } + + this.show(); } - - this.show(); } show() { diff --git a/src/public/javascripts/services/note_detail_relation_map.js b/src/public/javascripts/services/note_detail_relation_map.js index 61af483c0..9a56ba3e4 100644 --- a/src/public/javascripts/services/note_detail_relation_map.js +++ b/src/public/javascripts/services/note_detail_relation_map.js @@ -238,14 +238,17 @@ class NoteDetailRelationMap { await libraryLoader.requireLibrary(libraryLoader.RELATION_MAP); - this.loadMapData(); - jsPlumb.ready(() => { - this.initJsPlumbInstance(); + // lazy loading above can take time and tab might have been already switched to another note + if (this.ctx.note && this.ctx.note.type === 'relation-map') { + this.loadMapData(); - this.initPanZoom(); + this.initJsPlumbInstance(); - this.loadNotesAndRelations(); + this.initPanZoom(); + + this.loadNotesAndRelations(); + } }); } diff --git a/src/public/javascripts/services/note_detail_text.js b/src/public/javascripts/services/note_detail_text.js index 838d7d3c4..15b063b48 100644 --- a/src/public/javascripts/services/note_detail_text.js +++ b/src/public/javascripts/services/note_detail_text.js @@ -98,11 +98,14 @@ class NoteDetailText { } } - this.textEditor.isReadOnly = await this.isReadOnly(); + // lazy loading above can take time and tab might have been already switched to another note + if (this.ctx.note && this.ctx.note.type === 'text') { + this.textEditor.isReadOnly = await this.isReadOnly(); - this.$component.show(); + this.$component.show(); - this.textEditor.setData(this.ctx.note.content); + this.textEditor.setData(this.ctx.note.content); + } } getContent() {