From a34fc6846916a132bccf31a6e96383c452facfb2 Mon Sep 17 00:00:00 2001 From: Thomas Brouard Date: Tue, 10 Mar 2020 22:38:20 +0100 Subject: [PATCH] Add a way to abort closing (#53) --- README.md | 2 +- index.js | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4f386e1..375e181 100644 --- a/README.md +++ b/README.md @@ -227,7 +227,7 @@ The following events are available: * `tab.on("flash", (tab) => { ... });` * `tab.on("unflash", (tab) => { ... });` * `tab.on("close", (tab) => { ... });` -* `tab.on("closing", (tab) => { ... });` +* `tab.on("closing", (tab, abort) => { ... });` (Use `abort()` function to cancel closing) ## Drag and drop support diff --git a/index.js b/index.js index 4b3b7b0..261f73b 100644 --- a/index.js +++ b/index.js @@ -342,14 +342,20 @@ class Tab extends EventEmitter { } close (force) { - this.emit("closing", this); - if (this.isClosed || (!this.closable && !force)) return; + const abortController = new AbortController(); + const abort = () => abortController.abort(); + this.emit("closing", this, abort); + + const abortSignal = abortController.signal; + if (this.isClosed || (!this.closable && !force) || abortSignal.aborted) return; + this.isClosed = true; let tabGroup = this.tabGroup; tabGroup.tabContainer.removeChild(this.tab); tabGroup.viewContainer.removeChild(this.webview); let activeTab = this.tabGroup.getActiveTab(); TabGroupPrivate.removeTab.bind(tabGroup)(this, true); + this.emit("close", this); if (activeTab.id === this.id) {