sharedProcess.ts 1.7 KB
Newer Older
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { assign } from 'vs/base/common/objects';
7
import { memoize } from 'vs/base/common/decorators';
8 9 10 11 12 13 14 15
import { ParsedArgs } from 'vs/platform/environment/common/environment';
import { TPromise } from 'vs/base/common/winjs.base';
import { BrowserWindow, ipcMain } from 'electron';

export interface ISharedProcessInitData {
	args: ParsedArgs;
}

16
export class SharedProcess {
17

18
	private window: Electron.BrowserWindow;
19

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
	@memoize
	get onReady(): TPromise<void> {
		this.window = new BrowserWindow();
		const config = assign({ appRoot: this.appRoot, nodeCachedDataDir: this.nodeCachedDataDir });

		const url = `${require.toUrl('vs/code/electron-browser/sharedProcess.html')}?config=${encodeURIComponent(JSON.stringify(config))}`;
		this.window.loadURL(url);
		this.window.webContents.openDevTools();
		// this.window.hide();

		// Prevent the window from dying
		this.window.on('close', e => {
			if (this.window.isVisible()) {
				e.preventDefault();
				this.window.hide();
			}
		});
37

38 39 40 41 42
		return new TPromise<void>((c, e) => {
			ipcMain.once('handshake', ({ sender }) => {
				sender.send('handshake', this.initData);
				c(null);
			});
43
		});
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	}

	constructor(
		private initData: ISharedProcessInitData,
		private appRoot: string,
		private nodeCachedDataDir: string
	) { }

	dispose(): void {
		if (this.window) {
			this.window.close();
			this.window = null;
		}
	}
}