From 9bd9d312f8d59d3a80defba4aeb760e2c3e7e10c Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Tue, 16 May 2023 09:29:00 +0200 Subject: [PATCH] fix: enable `BrowserWindow.id` access post-destruction (#38241) fix: enable BrowserWindow id access post-destruction --- lib/browser/api/browser-window.ts | 8 ++++++++ spec/api-browser-window-spec.ts | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/lib/browser/api/browser-window.ts b/lib/browser/api/browser-window.ts index 8d18fddd8e0b..a53129d726c1 100644 --- a/lib/browser/api/browser-window.ts +++ b/lib/browser/api/browser-window.ts @@ -12,6 +12,14 @@ BrowserWindow.prototype._init = function (this: BWT) { // Avoid recursive require. const { app } = require('electron'); + // Set ID at constructon time so it's accessible after + // underlying window destruction. + const id = this.id; + Object.defineProperty(this, 'id', { + value: id, + writable: false + }); + const nativeSetBounds = this.setBounds; this.setBounds = (bounds, ...opts) => { bounds = { diff --git a/spec/api-browser-window-spec.ts b/spec/api-browser-window-spec.ts index de1035cc2ec0..902c072e6f5c 100644 --- a/spec/api-browser-window-spec.ts +++ b/spec/api-browser-window-spec.ts @@ -126,6 +126,13 @@ describe('BrowserWindow module', () => { w.webContents.on('destroyed', () => w.close()); }); + it('should allow access to id after destruction', async () => { + const closed = once(w, 'closed'); + w.destroy(); + await closed; + expect(w.id).to.be.a('number'); + }); + it('should emit unload handler', async () => { await w.loadFile(path.join(fixtures, 'api', 'unload.html')); const closed = once(w, 'closed');