提交 c4bbbe7e 编写于 作者: S Sandeep Somavarapu

Fix #62821

上级 c2a379f5
......@@ -735,7 +735,8 @@
"./vs/workbench/services/themes/common/workbenchThemeService.ts",
"./vs/workbench/services/title/common/titleService.ts",
"./vs/workbench/services/workspace/common/workspaceEditing.ts",
"./vs/workbench/test/electron-browser/api/mock.ts"
"./vs/workbench/test/electron-browser/api/mock.ts",
"./vs/base/node/config.ts"
],
"exclude": [
"./typings/require-monaco.d.ts"
......
......@@ -22,12 +22,11 @@ export interface IConfigWatcher<T> {
reload(callback: (config: T) => void): void;
getConfig(): T;
getValue<V>(key: string, fallback?: V): V;
}
export interface IConfigOptions<T> {
onError: (error: Error | string) => void;
defaultConfig?: T;
defaultConfig: T;
changeBufferDelay?: number;
parse?: (content: string, errors: any[]) => T;
initCallback?: (config: T) => void;
......@@ -51,7 +50,7 @@ export class ConfigWatcher<T> implements IConfigWatcher<T>, IDisposable {
private readonly _onDidUpdateConfiguration: Emitter<IConfigurationChangeEvent<T>>;
private configName: string;
constructor(private _path: string, private options: IConfigOptions<T> = { changeBufferDelay: 0, defaultConfig: Object.create(null), onError: error => console.error(error) }) {
constructor(private _path: string, private options: IConfigOptions<T> = { defaultConfig: Object.create(null), onError: error => console.error(error) }) {
this.disposables = [];
this.configName = basename(this._path);
......@@ -113,11 +112,11 @@ export class ConfigWatcher<T> implements IConfigWatcher<T>, IDisposable {
try {
this.parseErrors = [];
res = this.options.parse ? this.options.parse(raw, this.parseErrors) : json.parse(raw, this.parseErrors);
return res || this.options.defaultConfig;
} catch (error) {
// Ignore parsing errors
return this.options.defaultConfig;
}
return res || this.options.defaultConfig;
}
private registerWatcher(): void {
......@@ -156,7 +155,7 @@ export class ConfigWatcher<T> implements IConfigWatcher<T>, IDisposable {
));
}
private onConfigFileChange(eventType: string, filename: string, isParentFolder: boolean): void {
private onConfigFileChange(eventType: string, filename: string | undefined, isParentFolder: boolean): void {
if (isParentFolder) {
// Windows: in some cases the filename contains artifacts from the absolute path
......@@ -177,7 +176,7 @@ export class ConfigWatcher<T> implements IConfigWatcher<T>, IDisposable {
}
// we can get multiple change events for one change, so we buffer through a timeout
this.timeoutHandle = global.setTimeout(() => this.reload(), this.options.changeBufferDelay);
this.timeoutHandle = global.setTimeout(() => this.reload(), this.options.changeBufferDelay || 0);
}
public reload(callback?: (config: T) => void): void {
......@@ -200,18 +199,6 @@ export class ConfigWatcher<T> implements IConfigWatcher<T>, IDisposable {
return this.cache;
}
public getValue<V>(key: string, fallback?: V): V {
this.ensureLoaded();
if (!key) {
return fallback;
}
const value = this.cache ? (this.cache as any)[key] : void 0;
return typeof value !== 'undefined' ? value : fallback;
}
private ensureLoaded(): void {
if (!this.loaded) {
this.updateCache(this.loadSync());
......
......@@ -46,9 +46,6 @@ suite('Config', () => {
let config = watcher.getConfig();
assert.ok(config);
assert.equal(config.foo, 'bar');
assert.equal(watcher.getValue('foo'), 'bar');
assert.equal(watcher.getValue('bar'), void 0);
assert.equal(watcher.getValue('bar', 'fallback'), 'fallback');
assert.ok(!watcher.hasParseErrors);
watcher.dispose();
......@@ -144,20 +141,18 @@ suite('Config', () => {
testFile('config', 'config.json').then(res => {
fs.writeFileSync(res.testFile, '// my comment\n{ "foo": "bar" }');
let watcher = new ConfigWatcher<{ foo: string; }>(res.testFile, { changeBufferDelay: 100, onError: console.error });
let watcher = new ConfigWatcher<{ foo: string; }>(res.testFile, { changeBufferDelay: 100, onError: console.error, defaultConfig: void 0 });
watcher.getConfig(); // ensure we are in sync
fs.writeFileSync(res.testFile, '// my comment\n{ "foo": "changed" }');
// still old values because change is not bubbling yet
assert.equal(watcher.getConfig().foo, 'bar');
assert.equal(watcher.getValue('foo'), 'bar');
// force a load from disk
watcher.reload(config => {
assert.equal(config.foo, 'changed');
assert.equal(watcher.getConfig().foo, 'changed');
assert.equal(watcher.getValue('foo'), 'changed');
watcher.dispose();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册