提交 1800eaf8 编写于 作者: J Johannes Rieken

restart worker when defaults change

上级 a80db720
......@@ -10,21 +10,15 @@ import * as ts from 'vs/languages/typescript/common/lib/typescriptServices';
export class LanguageServiceDefaults {
private _onDidChange = new Emitter<LanguageServiceDefaults>();
private _compilerOptions: ts.CompilerOptions = { allowNonTsExtensions: true, target: ts.ScriptTarget.Latest };
private _onDidChangeCompilerOptions = new Emitter<ts.CompilerOptions>();
private _extraLibs: { [path: string]: string } = Object.create(null);
private _onDidAddExtraLib = new Emitter<string>();
private _onDidRemoveExtraLib = new Emitter<string>();
get onDidAddExtraLibs(): Event<string> {
return this._onDidAddExtraLib.event;
get onDidChange(): Event<LanguageServiceDefaults>{
return this._onDidChange.event;
}
get onDidRemoveExtraLib(): Event<string> {
return this._onDidRemoveExtraLib.event;
}
getExtraLibs(): { [path: string]: string } {
get extraLibs(): { [path: string]: string } {
return Object.freeze(this._extraLibs);
}
......@@ -38,28 +32,24 @@ export class LanguageServiceDefaults {
}
this._extraLibs[filePath] = content;
this._onDidAddExtraLib.fire(filePath);
this._onDidChange.fire(this);
return {
dispose: () => {
if (delete this._extraLibs[filePath]) {
this._onDidRemoveExtraLib.fire(filePath);
this._onDidChange.fire(this);
}
}
};
}
get onDidChangeCompilerOptions(): Event<ts.CompilerOptions> {
return this._onDidChangeCompilerOptions.event;
}
getCompilerOptions(): ts.CompilerOptions {
get compilerOptions(): ts.CompilerOptions {
return this._compilerOptions;
}
setCompilerOptions(options: ts.CompilerOptions): void {
this._compilerOptions = options || Object.create(null);
this._onDidChangeCompilerOptions.fire(this._compilerOptions);
this._onDidChange.fire(this);
}
}
......
......@@ -39,6 +39,10 @@ abstract class AbstractWorker implements IRequestHandler {
throw notImplemented();
}
acceptExtraLib(fileName: string, content: string): TPromise<void>{
throw notImplemented();
}
// --- language features
getCompletionsAtPosition(uri: string, offset: number): TPromise<ts.CompletionInfo> {
......
......@@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/
'use strict';
import {forEach} from 'vs/base/common/collections';
import {TPromise} from 'vs/base/common/winjs.base';
import {IDisposable, disposeAll} from 'vs/base/common/lifecycle';
import {DefaultWorkerFactory} from 'vs/base/worker/defaultWorkerFactory';
......@@ -26,28 +27,41 @@ class Client {
}
private _createClient(): TPromise<AbstractWorker> {
let client = new SimpleWorkerClient<AbstractWorker>(
this._factory,
this._clientDispose = [];
let client = new SimpleWorkerClient<AbstractWorker>(this._factory,
'vs/languages/typescript/common/worker/workerImpl',
AbstractWorker);
this._clientDispose.push(client);
this._clientDispose = [];
const stopWorker = () => {
this._clientDispose = disposeAll(this._clientDispose);
this._client = null;
client = null;
};
// stop worker after being idle
const handle = setInterval(() => {
if (Date.now() - client.getLastRequestTimestamp() > 1000 * 60) {
this._clientDispose = disposeAll(this._clientDispose);
this._client = null;
client = null;
stopWorker();
}
}, 1000 * 60);
this._clientDispose.push({ dispose() { clearInterval(handle); } });
this._clientDispose.push(client);
// stop worker when defaults change
this._clientDispose.push(Defaults.onDidChange(() => stopWorker()));
// send default to worker right away
const worker = client.get();
this._clientDispose.push(Defaults.onDidChangeCompilerOptions(options => worker.acceptCompilerOptions(options)));
const promises: TPromise<any>[] = [];
promises.push(worker.acceptCompilerOptions(Defaults.compilerOptions));
forEach(Defaults.extraLibs, entry => promises.push(worker.acceptExtraLib(entry.key, entry.value)));
return worker.acceptCompilerOptions(Defaults.getCompilerOptions()).then(() => worker);
return TPromise.join(promises).then(() => {
promises.length = 0;
return worker;
});
}
dispose(): void {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册