SharedProcess: Create returns a promise resolved when it is available to connect

This commit is contained in:
Sandeep Somavarapu
2016-09-22 08:15:39 +02:00
parent 148c8d079d
commit 32c6a6a2f2
3 changed files with 33 additions and 20 deletions

View File

@@ -8,6 +8,7 @@ import URI from 'vs/base/common/uri';
import { IDisposable } from 'vs/base/common/lifecycle';
import { assign } from 'vs/base/common/objects';
import { ParsedArgs } from 'vs/platform/environment/node/argv';
import { TPromise } from 'vs/base/common/winjs.base';
export interface ISharedProcessInitData {
args: ParsedArgs;
@@ -36,34 +37,40 @@ function _spawnSharedProcess(initData: ISharedProcessInitData, options: ISharedP
const result = cp.fork(boostrapPath, ['--type=SharedProcess'], { env, execArgv });
// handshake
result.once('message', () => result.send(initData));
return result;
}
export function spawnSharedProcess(initData: ISharedProcessInitData, options: ISharedProcessOptions = {}): IDisposable {
export function spawnSharedProcess(initData: ISharedProcessInitData, options: ISharedProcessOptions = {}): TPromise<IDisposable> {
let spawnCount = 0;
let child: cp.ChildProcess;
let promise: TPromise<IDisposable>;
const spawn = () => {
if (++spawnCount > 10) {
return;
}
child = _spawnSharedProcess(initData, options);
promise = new TPromise<IDisposable>((c, e) => {
// handshake
child.once('message', () => {
child.send(initData);
c({
dispose: () => {
if (child) {
child.removeListener('exit', spawn);
child.kill();
child = null;
}
}
});
});
});
child.on('exit', spawn);
};
spawn();
return {
dispose: () => {
if (child) {
child.removeListener('exit', spawn);
child.kill();
child = null;
}
}
};
return promise;
}

View File

@@ -137,7 +137,7 @@ function setupIPC(hook: string): TPromise<Server> {
} catch (e) {
return TPromise.wrapError(new Error('Error deleting the shared ipc hook.'));
}
return setup(false);
}
);
@@ -155,7 +155,8 @@ function handshake(): TPromise<ISharedProcessInitData> {
});
}
TPromise.join<any>([setupIPC(process.env['VSCODE_SHARED_IPC_HOOK']), handshake()])
.then(r => main(r[0], r[1]))
.then(() => setupPlanB(process.env['VSCODE_PID']))
.done(null, quit);
setupIPC(process.env['VSCODE_SHARED_IPC_HOOK'])
.then(server => handshake()
.then(data => main(server, data))
.then(() => setupPlanB(process.env['VSCODE_PID']))
.done(null, quit));