fix dragging out tab creating multiple windows, closes #2944

This commit is contained in:
zadam 2022-06-29 22:38:35 +02:00
parent 2e1bef2df7
commit 33c272e86a
2 changed files with 28 additions and 7 deletions

View File

@ -142,6 +142,11 @@ export default class TabManager extends Component {
return this.noteContexts; return this.noteContexts;
} }
/** @returns {NoteContext[]} */
getMainNoteContexts() {
return this.noteContexts.filter(nc => nc.isMainContext());
}
/** @returns {NoteContext} */ /** @returns {NoteContext} */
getNoteContextById(ntxId) { getNoteContextById(ntxId) {
const noteContext = this.noteContexts.find(nc => nc.ntxId === ntxId); const noteContext = this.noteContexts.find(nc => nc.ntxId === ntxId);
@ -294,11 +299,23 @@ export default class TabManager extends Component {
this.setCurrentNotePathToHash(); this.setCurrentNotePathToHash();
} }
/**
* @param ntxId
* @returns {Promise<boolean>} true if note context has been removed, false otherwise
*/
async removeNoteContext(ntxId) { async removeNoteContext(ntxId) {
// removing note context is async process which can take some time, if users presses CTRL-W quickly, two // 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. // close events could interleave which would then lead to attempting to activate already removed context.
await this.mutex.runExclusively(async () => { return await this.mutex.runExclusively(async () => {
const noteContextToRemove = this.getNoteContextById(ntxId); let noteContextToRemove;
try {
noteContextToRemove = this.getNoteContextById(ntxId);
}
catch {
// note context not found
return false;
}
if (noteContextToRemove.isMainContext()) { if (noteContextToRemove.isMainContext()) {
// forbid removing last main note context // forbid removing last main note context
@ -308,7 +325,7 @@ export default class TabManager extends Component {
if (mainNoteContexts.length === 1) { if (mainNoteContexts.length === 1) {
await this.clearLastMainNoteContext(noteContextToRemove); await this.clearLastMainNoteContext(noteContextToRemove);
return; return false;
} }
} }
@ -338,6 +355,8 @@ export default class TabManager extends Component {
} }
this.removeNoteContexts(noteContextsToRemove); 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); 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() { async reopenLastTabCommand() {

View File

@ -19,7 +19,7 @@ export default class Mutex {
const unlock = await this.lock(); const unlock = await this.lock();
try { try {
await cb(); return await cb();
} }
finally { finally {
unlock(); unlock();