Browse Source

fix: enable `BrowserWindow.id` access post-destruction (#38309)

fix: enable BrowserWindow id access post-destruction

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <[email protected]>
trop[bot] 1 year ago
parent
commit
3e6f5ca655
2 changed files with 15 additions and 0 deletions
  1. 8 0
      lib/browser/api/browser-window.ts
  2. 7 0
      spec/api-browser-window-spec.ts

+ 8 - 0
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 = {

+ 7 - 0
spec/api-browser-window-spec.ts

@@ -123,6 +123,13 @@ describe('BrowserWindow module', () => {
       w.webContents.on('destroyed', () => w.close());
     });
 
+    it('should allow access to id after destruction', async () => {
+      const closed = emittedOnce(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 = emittedOnce(w, 'closed');