From aa4dade1e5930480a81800418fe4505c4bf41613 Mon Sep 17 00:00:00 2001 From: zadam Date: Wed, 29 Jun 2022 22:38:35 +0200 Subject: [PATCH] fix dragging out tab creating multiple windows, closes #2944 --- src/public/app/services/tab_manager.js | 33 +++++++++++++++++++++----- src/public/app/utils/mutex.js | 2 +- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/public/app/services/tab_manager.js b/src/public/app/services/tab_manager.js index 59318d6e3..2156400c7 100644 --- a/src/public/app/services/tab_manager.js +++ b/src/public/app/services/tab_manager.js @@ -142,6 +142,11 @@ export default class TabManager extends Component { return this.noteContexts; } + /** @returns {NoteContext[]} */ + getMainNoteContexts() { + return this.noteContexts.filter(nc => nc.isMainContext()); + } + /** @returns {NoteContext} */ getNoteContextById(ntxId) { const noteContext = this.noteContexts.find(nc => nc.ntxId === ntxId); @@ -294,11 +299,23 @@ export default class TabManager extends Component { this.setCurrentNotePathToHash(); } + /** + * @param ntxId + * @returns {Promise} true if note context has been removed, false otherwise + */ async removeNoteContext(ntxId) { // removing note context is async process which can take some time, if users presses CTRL-W quickly, two // close events could interleave which would then lead to attempting to activate already removed context. - await this.mutex.runExclusively(async () => { - const noteContextToRemove = this.getNoteContextById(ntxId); + return await this.mutex.runExclusively(async () => { + let noteContextToRemove; + + try { + noteContextToRemove = this.getNoteContextById(ntxId); + } + catch { + // note context not found + return false; + } if (noteContextToRemove.isMainContext()) { // forbid removing last main note context @@ -308,7 +325,7 @@ export default class TabManager extends Component { if (mainNoteContexts.length === 1) { await this.clearLastMainNoteContext(noteContextToRemove); - return; + return false; } } @@ -338,6 +355,8 @@ export default class TabManager extends Component { } this.removeNoteContexts(noteContextsToRemove); + + return true; }); } @@ -445,12 +464,14 @@ export default class TabManager extends Component { } } - moveTabToNewWindowCommand({ntxId}) { + async moveTabToNewWindowCommand({ntxId}) { const {notePath, hoistedNoteId} = this.getNoteContextById(ntxId); - this.removeNoteContext(ntxId); + const removed = await this.removeNoteContext(ntxId); - this.triggerCommand('openInWindow', {notePath, hoistedNoteId}); + if (removed) { + this.triggerCommand('openInWindow', {notePath, hoistedNoteId}); + } } async reopenLastTabCommand() { diff --git a/src/public/app/utils/mutex.js b/src/public/app/utils/mutex.js index a48b2660e..483e0f345 100644 --- a/src/public/app/utils/mutex.js +++ b/src/public/app/utils/mutex.js @@ -19,7 +19,7 @@ export default class Mutex { const unlock = await this.lock(); try { - await cb(); + return await cb(); } finally { unlock();