Skip to content

Commit ffbc488

Browse files
author
Benjamin Pasero
committed
debt - add whenClosedOrLoaded into window class
1 parent 86a4239 commit ffbc488

4 files changed

Lines changed: 34 additions & 32 deletions

File tree

src/vs/code/electron-main/window.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ export class CodeWindow extends Disposable implements ICodeWindow {
7070
private readonly _onDestroy = this._register(new Emitter<void>());
7171
readonly onDestroy: CommonEvent<void> = this._onDestroy.event;
7272

73+
private readonly _onLoad = this._register(new Emitter<void>());
74+
readonly onLoad: CommonEvent<void> = this._onLoad.event;
75+
7376
private hiddenTitleBarStyle: boolean;
7477
private showTimeoutHandle: NodeJS.Timeout;
7578
private _id: number;
@@ -324,6 +327,21 @@ export class CodeWindow extends Disposable implements ICodeWindow {
324327
return this._readyState === ReadyState.READY;
325328
}
326329

330+
get whenClosedOrLoaded(): Promise<void> {
331+
return new Promise<void>(resolve => {
332+
333+
function handle() {
334+
closeListener.dispose();
335+
loadListener.dispose();
336+
337+
resolve();
338+
}
339+
340+
const closeListener = this.onClose(() => handle());
341+
const loadListener = this.onLoad(() => handle());
342+
});
343+
}
344+
327345
private handleMarketplaceRequests(): void {
328346

329347
// Resolve marketplace headers
@@ -349,7 +367,11 @@ export class CodeWindow extends Disposable implements ICodeWindow {
349367
this._win.on('unresponsive', () => this.onWindowError(WindowError.UNRESPONSIVE));
350368

351369
// Window close
352-
this._win.on('closed', () => this._onClose.fire());
370+
this._win.on('closed', () => {
371+
this._onClose.fire();
372+
373+
this.dispose();
374+
});
353375

354376
// Prevent loading of svgs
355377
this._win.webContents.session.webRequest.onBeforeRequest(null!, (details, callback) => {
@@ -606,6 +628,9 @@ export class CodeWindow extends Disposable implements ICodeWindow {
606628
}
607629
}, 10000);
608630
}
631+
632+
// Event
633+
this._onLoad.fire();
609634
}
610635

611636
reload(configurationIn?: IWindowConfiguration, cli?: ParsedArgs): void {

src/vs/code/electron-main/windows.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,6 @@ export class WindowsManager extends Disposable implements IWindowsMainService {
166166
private readonly _onWindowClose = this._register(new Emitter<number>());
167167
readonly onWindowClose: CommonEvent<number> = this._onWindowClose.event;
168168

169-
private readonly _onWindowLoad = this._register(new Emitter<number>());
170-
readonly onWindowLoad: CommonEvent<number> = this._onWindowLoad.event;
171-
172169
private readonly _onWindowsCountChanged = this._register(new Emitter<IWindowsCountChangedEvent>());
173170
readonly onWindowsCountChanged: CommonEvent<IWindowsCountChangedEvent> = this._onWindowsCountChanged.event;
174171

@@ -502,7 +499,7 @@ export class WindowsManager extends Disposable implements IWindowsMainService {
502499
// process can continue. We do this by deleting the waitMarkerFilePath.
503500
const waitMarkerFileURI = openConfig.waitMarkerFileURI;
504501
if (openConfig.context === OpenContext.CLI && waitMarkerFileURI && usedWindows.length === 1 && usedWindows[0]) {
505-
this.waitForWindowCloseOrLoad(usedWindows[0].id).then(() => fs.unlink(waitMarkerFileURI.fsPath, _error => undefined));
502+
usedWindows[0].whenClosedOrLoaded.then(() => fs.unlink(waitMarkerFileURI.fsPath, _error => undefined));
506503
}
507504

508505
return usedWindows;
@@ -1456,9 +1453,6 @@ export class WindowsManager extends Disposable implements IWindowsMainService {
14561453

14571454
// Load it
14581455
window.load(configuration);
1459-
1460-
// Signal event
1461-
this._onWindowLoad.fire(window.id);
14621456
}
14631457

14641458
private getNewWindowState(configuration: IWindowConfiguration): INewWindowState {
@@ -1642,22 +1636,6 @@ export class WindowsManager extends Disposable implements IWindowsMainService {
16421636
return this.open({ context, cli, forceEmpty: true, forceNewWindow, forceReuseWindow });
16431637
}
16441638

1645-
waitForWindowCloseOrLoad(windowId: number): Promise<void> {
1646-
return new Promise<void>(resolve => {
1647-
function handler(id: number) {
1648-
if (id === windowId) {
1649-
closeListener.dispose();
1650-
loadListener.dispose();
1651-
1652-
resolve();
1653-
}
1654-
}
1655-
1656-
const closeListener = this.onWindowClose(id => handler(id));
1657-
const loadListener = this.onWindowLoad(id => handler(id));
1658-
});
1659-
}
1660-
16611639
sendToFocused(channel: string, ...args: any[]): void {
16621640
const focusedWindow = this.getFocusedWindow() || this.getLastActiveWindow();
16631641

@@ -1700,9 +1678,6 @@ export class WindowsManager extends Disposable implements IWindowsMainService {
17001678

17011679
private onWindowClosed(win: ICodeWindow): void {
17021680

1703-
// Tell window
1704-
win.dispose();
1705-
17061681
// Remove from our list so that Electron can clean it up
17071682
const index = WindowsManager.WINDOWS.indexOf(win);
17081683
WindowsManager.WINDOWS.splice(index, 1);

src/vs/platform/launch/electron-main/launchMainService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export class LaunchMainService implements ILaunchMainService {
188188
// In addition, we poll for the wait marker file to be deleted to return.
189189
if (waitMarkerFileURI && usedWindows.length === 1 && usedWindows[0]) {
190190
return Promise.race([
191-
this.windowsMainService.waitForWindowCloseOrLoad(usedWindows[0].id),
191+
usedWindows[0].whenClosedOrLoaded,
192192
whenDeleted(waitMarkerFileURI.fsPath)
193193
]).then(() => undefined, () => undefined);
194194
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export interface ICodeWindow extends IDisposable {
3535
readonly onClose: Event<void>;
3636
readonly onDestroy: Event<void>;
3737

38+
readonly whenClosedOrLoaded: Promise<void>;
39+
3840
readonly id: number;
3941
readonly win: BrowserWindow;
4042
readonly config: IWindowConfiguration;
@@ -96,12 +98,15 @@ export interface IWindowsMainService {
9698

9799
readonly onWindowReady: Event<ICodeWindow>;
98100
readonly onWindowsCountChanged: Event<IWindowsCountChangedEvent>;
99-
readonly onWindowClose: Event<number>;
100101

101102
open(openConfig: IOpenConfiguration): ICodeWindow[];
102103
openEmptyWindow(context: OpenContext, options?: IOpenEmptyWindowOptions): ICodeWindow[];
103104
openExtensionDevelopmentHostWindow(extensionDevelopmentPath: string[], openConfig: IOpenConfiguration): ICodeWindow[];
104105

106+
closeWorkspace(win: ICodeWindow): void;
107+
108+
reload(win: ICodeWindow, cli?: ParsedArgs): void;
109+
105110
sendToFocused(channel: string, ...args: any[]): void;
106111
sendToAll(channel: string, payload: any, windowIdsToIgnore?: number[]): void;
107112

@@ -111,9 +116,6 @@ export interface IWindowsMainService {
111116
getWindows(): ICodeWindow[];
112117
getWindowCount(): number;
113118

114-
waitForWindowCloseOrLoad(windowId: number): Promise<void>;
115-
reload(win: ICodeWindow, cli?: ParsedArgs): void;
116-
closeWorkspace(win: ICodeWindow): void;
117119
quit(): void;
118120
}
119121

0 commit comments

Comments
 (0)