Add a way to abort closing (#53)

This commit is contained in:
Thomas Brouard 2020-03-10 22:38:20 +01:00
parent 601c7fc24c
commit a34fc68469
2 changed files with 9 additions and 3 deletions

View file

@ -227,7 +227,7 @@ The following events are available:
* `tab.on("flash", (tab) => { ... });` * `tab.on("flash", (tab) => { ... });`
* `tab.on("unflash", (tab) => { ... });` * `tab.on("unflash", (tab) => { ... });`
* `tab.on("close", (tab) => { ... });` * `tab.on("close", (tab) => { ... });`
* `tab.on("closing", (tab) => { ... });` * `tab.on("closing", (tab, abort) => { ... });` (Use `abort()` function to cancel closing)
## Drag and drop support ## Drag and drop support

View file

@ -342,14 +342,20 @@ class Tab extends EventEmitter {
} }
close (force) { close (force) {
this.emit("closing", this); const abortController = new AbortController();
if (this.isClosed || (!this.closable && !force)) return; 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; this.isClosed = true;
let tabGroup = this.tabGroup; let tabGroup = this.tabGroup;
tabGroup.tabContainer.removeChild(this.tab); tabGroup.tabContainer.removeChild(this.tab);
tabGroup.viewContainer.removeChild(this.webview); tabGroup.viewContainer.removeChild(this.webview);
let activeTab = this.tabGroup.getActiveTab(); let activeTab = this.tabGroup.getActiveTab();
TabGroupPrivate.removeTab.bind(tabGroup)(this, true); TabGroupPrivate.removeTab.bind(tabGroup)(this, true);
this.emit("close", this); this.emit("close", this);
if (activeTab.id === this.id) { if (activeTab.id === this.id) {