Skip to content

Commit 77e5711

Browse files
authored
debt - remove electron listeners (microsoft#210210)
1 parent 50a6f4f commit 77e5711

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

src/vs/platform/auxiliaryWindow/electron-main/auxiliaryWindowsMainService.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@ export class AuxiliaryWindowsMainService extends Disposable implements IAuxiliar
6262

6363
// This is a main window, listen to child windows getting created to claim it
6464
else {
65-
browserWindow.webContents.on('did-create-window', (browserWindow, details) => {
65+
const disposables = new DisposableStore();
66+
disposables.add(Event.fromNodeEventEmitter(browserWindow.webContents, 'did-create-window', (browserWindow, details) => ({ browserWindow, details }))(({ browserWindow, details }) => {
6667
const auxiliaryWindow = this.getWindowByWebContents(browserWindow.webContents);
6768
if (auxiliaryWindow) {
6869
this.logService.trace('[aux window] window.on("did-create-window"): Trying to claim auxiliary window');
6970

7071
auxiliaryWindow.tryClaimWindow(details.options);
7172
}
72-
});
73+
}));
74+
disposables.add(Event.fromNodeEventEmitter(browserWindow, 'closed')(() => disposables.dispose()));
7375
}
7476
});
7577

src/vs/platform/lifecycle/electron-main/lifecycleMainService.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { app, BrowserWindow } from 'electron';
6+
import { app, BrowserWindow, Event as ElectronEvent } from 'electron';
77
import { validatedIpcMain } from 'vs/base/parts/ipc/electron-main/ipcMain';
88
import { Barrier, Promises, timeout } from 'vs/base/common/async';
99
import { Emitter, Event } from 'vs/base/common/event';
@@ -428,7 +428,7 @@ export class LifecycleMainService extends Disposable implements ILifecycleMainSe
428428

429429
// Window Before Closing: Main -> Renderer
430430
const win = assertIsDefined(window.win);
431-
win.on('close', e => {
431+
windowListeners.add(Event.fromNodeEventEmitter<ElectronEvent>(win, 'close')(e => {
432432

433433
// The window already acknowledged to be closed
434434
const windowId = window.id;
@@ -457,10 +457,8 @@ export class LifecycleMainService extends Disposable implements ILifecycleMainSe
457457
// No veto, close window now
458458
window.close();
459459
});
460-
});
461-
462-
// Window After Closing
463-
win.on('closed', () => {
460+
}));
461+
windowListeners.add(Event.fromNodeEventEmitter<ElectronEvent>(win, 'closed')(() => {
464462
this.trace(`Lifecycle#window.on('closed') - window ID ${window.id}`);
465463

466464
// update window count
@@ -475,13 +473,14 @@ export class LifecycleMainService extends Disposable implements ILifecycleMainSe
475473
if (this.windowCounter === 0 && (!isMacintosh || this._quitRequested)) {
476474
this.fireOnWillShutdown(ShutdownReason.QUIT);
477475
}
478-
});
476+
}));
479477
}
480478

481479
registerAuxWindow(auxWindow: IAuxiliaryWindow): void {
482480
const win = assertIsDefined(auxWindow.win);
483481

484-
win.on('close', e => {
482+
const windowListeners = new DisposableStore();
483+
windowListeners.add(Event.fromNodeEventEmitter<ElectronEvent>(win, 'close')(e => {
485484
this.trace(`Lifecycle#auxWindow.on('close') - window ID ${auxWindow.id}`);
486485

487486
if (this._quitRequested) {
@@ -499,11 +498,12 @@ export class LifecycleMainService extends Disposable implements ILifecycleMainSe
499498

500499
e.preventDefault();
501500
}
502-
});
503-
504-
win.on('closed', () => {
501+
}));
502+
windowListeners.add(Event.fromNodeEventEmitter<ElectronEvent>(win, 'closed')(() => {
505503
this.trace(`Lifecycle#auxWindow.on('closed') - window ID ${auxWindow.id}`);
506-
});
504+
505+
windowListeners.dispose();
506+
}));
507507
}
508508

509509
async reload(window: ICodeWindow, cli?: NativeParsedArgs): Promise<void> {

src/vs/platform/windows/electron-main/windowImpl.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { app, BrowserWindow, Display, nativeImage, NativeImage, Rectangle, screen, SegmentedControlSegment, systemPreferences, TouchBar, TouchBarSegmentedControl, WebContents } from 'electron';
6+
import { app, BrowserWindow, Display, nativeImage, NativeImage, Rectangle, screen, SegmentedControlSegment, systemPreferences, TouchBar, TouchBarSegmentedControl, WebContents, Event as ElectronEvent } from 'electron';
77
import { DeferredPromise, RunOnceScheduler, timeout } from 'vs/base/common/async';
88
import { CancellationToken } from 'vs/base/common/cancellation';
99
import { toErrorMessage } from 'vs/base/common/errorMessage';
@@ -682,29 +682,27 @@ export class CodeWindow extends BaseWindow implements ICodeWindow {
682682
private registerListeners(): void {
683683

684684
// Window error conditions to handle
685-
this._win.on('unresponsive', () => this.onWindowError(WindowError.UNRESPONSIVE));
686-
this._win.webContents.on('render-process-gone', (event, details) => this.onWindowError(WindowError.PROCESS_GONE, { ...details }));
687-
this._win.webContents.on('did-fail-load', (event, exitCode, reason) => this.onWindowError(WindowError.LOAD, { reason, exitCode }));
685+
this._register(Event.fromNodeEventEmitter(this._win, 'unresponsive')(() => this.onWindowError(WindowError.UNRESPONSIVE)));
686+
this._register(Event.fromNodeEventEmitter(this._win.webContents, 'render-process-gone', (event, details) => details)(details => this.onWindowError(WindowError.PROCESS_GONE, { ...details })));
687+
this._register(Event.fromNodeEventEmitter(this._win.webContents, 'did-fail-load', (event, exitCode, reason) => ({ exitCode, reason }))(({ exitCode, reason }) => this.onWindowError(WindowError.LOAD, { reason, exitCode })));
688688

689689
// Prevent windows/iframes from blocking the unload
690690
// through DOM events. We have our own logic for
691691
// unloading a window that should not be confused
692692
// with the DOM way.
693693
// (https://github.com/microsoft/vscode/issues/122736)
694-
this._win.webContents.on('will-prevent-unload', event => {
695-
event.preventDefault();
696-
});
694+
this._register(Event.fromNodeEventEmitter<ElectronEvent>(this._win.webContents, 'will-prevent-unload')(event => event.preventDefault()));
697695

698696
// Remember that we loaded
699-
this._win.webContents.on('did-finish-load', () => {
697+
this._register(Event.fromNodeEventEmitter(this._win.webContents, 'did-finish-load')(() => {
700698

701699
// Associate properties from the load request if provided
702700
if (this.pendingLoadConfig) {
703701
this._config = this.pendingLoadConfig;
704702

705703
this.pendingLoadConfig = undefined;
706704
}
707-
});
705+
}));
708706

709707
// Window (Un)Maximize
710708
this._register(this.onDidMaximize(() => {

src/vs/platform/windows/electron-main/windowsMainService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1506,7 +1506,7 @@ export class WindowsMainService extends Disposable implements IWindowsMainServic
15061506

15071507
const webContents = assertIsDefined(createdWindow.win?.webContents);
15081508
webContents.removeAllListeners('devtools-reload-page'); // remove built in listener so we can handle this on our own
1509-
webContents.on('devtools-reload-page', () => this.lifecycleMainService.reload(createdWindow));
1509+
disposables.add(Event.fromNodeEventEmitter(webContents, 'devtools-reload-page')(() => this.lifecycleMainService.reload(createdWindow)));
15101510

15111511
// Lifecycle
15121512
this.lifecycleMainService.registerWindow(createdWindow);

0 commit comments

Comments
 (0)