debt - remove electron listeners (#210210)

This commit is contained in:
Benjamin Pasero 2024-04-12 08:50:18 +02:00 committed by GitHub
parent 50a6f4f200
commit 77e5711bee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 24 additions and 24 deletions

View file

@ -62,14 +62,16 @@ export class AuxiliaryWindowsMainService extends Disposable implements IAuxiliar
// This is a main window, listen to child windows getting created to claim it
else {
browserWindow.webContents.on('did-create-window', (browserWindow, details) => {
const disposables = new DisposableStore();
disposables.add(Event.fromNodeEventEmitter(browserWindow.webContents, 'did-create-window', (browserWindow, details) => ({ browserWindow, details }))(({ browserWindow, details }) => {
const auxiliaryWindow = this.getWindowByWebContents(browserWindow.webContents);
if (auxiliaryWindow) {
this.logService.trace('[aux window] window.on("did-create-window"): Trying to claim auxiliary window');
auxiliaryWindow.tryClaimWindow(details.options);
}
});
}));
disposables.add(Event.fromNodeEventEmitter(browserWindow, 'closed')(() => disposables.dispose()));
}
});

View file

@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { app, BrowserWindow } from 'electron';
import { app, BrowserWindow, Event as ElectronEvent } from 'electron';
import { validatedIpcMain } from 'vs/base/parts/ipc/electron-main/ipcMain';
import { Barrier, Promises, timeout } from 'vs/base/common/async';
import { Emitter, Event } from 'vs/base/common/event';
@ -428,7 +428,7 @@ export class LifecycleMainService extends Disposable implements ILifecycleMainSe
// Window Before Closing: Main -> Renderer
const win = assertIsDefined(window.win);
win.on('close', e => {
windowListeners.add(Event.fromNodeEventEmitter<ElectronEvent>(win, 'close')(e => {
// The window already acknowledged to be closed
const windowId = window.id;
@ -457,10 +457,8 @@ export class LifecycleMainService extends Disposable implements ILifecycleMainSe
// No veto, close window now
window.close();
});
});
// Window After Closing
win.on('closed', () => {
}));
windowListeners.add(Event.fromNodeEventEmitter<ElectronEvent>(win, 'closed')(() => {
this.trace(`Lifecycle#window.on('closed') - window ID ${window.id}`);
// update window count
@ -475,13 +473,14 @@ export class LifecycleMainService extends Disposable implements ILifecycleMainSe
if (this.windowCounter === 0 && (!isMacintosh || this._quitRequested)) {
this.fireOnWillShutdown(ShutdownReason.QUIT);
}
});
}));
}
registerAuxWindow(auxWindow: IAuxiliaryWindow): void {
const win = assertIsDefined(auxWindow.win);
win.on('close', e => {
const windowListeners = new DisposableStore();
windowListeners.add(Event.fromNodeEventEmitter<ElectronEvent>(win, 'close')(e => {
this.trace(`Lifecycle#auxWindow.on('close') - window ID ${auxWindow.id}`);
if (this._quitRequested) {
@ -499,11 +498,12 @@ export class LifecycleMainService extends Disposable implements ILifecycleMainSe
e.preventDefault();
}
});
win.on('closed', () => {
}));
windowListeners.add(Event.fromNodeEventEmitter<ElectronEvent>(win, 'closed')(() => {
this.trace(`Lifecycle#auxWindow.on('closed') - window ID ${auxWindow.id}`);
});
windowListeners.dispose();
}));
}
async reload(window: ICodeWindow, cli?: NativeParsedArgs): Promise<void> {

View file

@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { app, BrowserWindow, Display, nativeImage, NativeImage, Rectangle, screen, SegmentedControlSegment, systemPreferences, TouchBar, TouchBarSegmentedControl, WebContents } from 'electron';
import { app, BrowserWindow, Display, nativeImage, NativeImage, Rectangle, screen, SegmentedControlSegment, systemPreferences, TouchBar, TouchBarSegmentedControl, WebContents, Event as ElectronEvent } from 'electron';
import { DeferredPromise, RunOnceScheduler, timeout } from 'vs/base/common/async';
import { CancellationToken } from 'vs/base/common/cancellation';
import { toErrorMessage } from 'vs/base/common/errorMessage';
@ -682,21 +682,19 @@ export class CodeWindow extends BaseWindow implements ICodeWindow {
private registerListeners(): void {
// Window error conditions to handle
this._win.on('unresponsive', () => this.onWindowError(WindowError.UNRESPONSIVE));
this._win.webContents.on('render-process-gone', (event, details) => this.onWindowError(WindowError.PROCESS_GONE, { ...details }));
this._win.webContents.on('did-fail-load', (event, exitCode, reason) => this.onWindowError(WindowError.LOAD, { reason, exitCode }));
this._register(Event.fromNodeEventEmitter(this._win, 'unresponsive')(() => this.onWindowError(WindowError.UNRESPONSIVE)));
this._register(Event.fromNodeEventEmitter(this._win.webContents, 'render-process-gone', (event, details) => details)(details => this.onWindowError(WindowError.PROCESS_GONE, { ...details })));
this._register(Event.fromNodeEventEmitter(this._win.webContents, 'did-fail-load', (event, exitCode, reason) => ({ exitCode, reason }))(({ exitCode, reason }) => this.onWindowError(WindowError.LOAD, { reason, exitCode })));
// Prevent windows/iframes from blocking the unload
// through DOM events. We have our own logic for
// unloading a window that should not be confused
// with the DOM way.
// (https://github.com/microsoft/vscode/issues/122736)
this._win.webContents.on('will-prevent-unload', event => {
event.preventDefault();
});
this._register(Event.fromNodeEventEmitter<ElectronEvent>(this._win.webContents, 'will-prevent-unload')(event => event.preventDefault()));
// Remember that we loaded
this._win.webContents.on('did-finish-load', () => {
this._register(Event.fromNodeEventEmitter(this._win.webContents, 'did-finish-load')(() => {
// Associate properties from the load request if provided
if (this.pendingLoadConfig) {
@ -704,7 +702,7 @@ export class CodeWindow extends BaseWindow implements ICodeWindow {
this.pendingLoadConfig = undefined;
}
});
}));
// Window (Un)Maximize
this._register(this.onDidMaximize(() => {

View file

@ -1506,7 +1506,7 @@ export class WindowsMainService extends Disposable implements IWindowsMainServic
const webContents = assertIsDefined(createdWindow.win?.webContents);
webContents.removeAllListeners('devtools-reload-page'); // remove built in listener so we can handle this on our own
webContents.on('devtools-reload-page', () => this.lifecycleMainService.reload(createdWindow));
disposables.add(Event.fromNodeEventEmitter(webContents, 'devtools-reload-page')(() => this.lifecycleMainService.reload(createdWindow)));
// Lifecycle
this.lifecycleMainService.registerWindow(createdWindow);