提交 e97a77c3 编写于 作者: B Benjamin Pasero

add config.reload()

上级 a2b0f0ca
......@@ -11,11 +11,12 @@ import {IDisposable, dispose, toDisposable} from 'vs/base/common/lifecycle';
import Event, {Emitter} from 'vs/base/common/event';
import * as json from 'vs/base/common/json';
export interface IConfigurationServiceEvent<T> {
export interface IConfigurationChangeEvent<T> {
config: T;
}
export interface IConfigWatcher<T> {
reload(callback: (config: T) => void): void;
getConfig(): T;
getValue<V>(key: string, fallback?: V): V;
}
......@@ -43,14 +44,14 @@ export class ConfigWatcher<T> implements IConfigWatcher<T>, IDisposable {
constructor(private path: string, private options: IConfigOptions<T> = { changeBufferDelay: 0, defaultConfig: Object.create(null) }) {
this.disposables = [];
this._onDidUpdateConfiguration = new Emitter<IConfigurationServiceEvent<T>>();
this._onDidUpdateConfiguration = new Emitter<IConfigurationChangeEvent<T>>();
this.disposables.push(this._onDidUpdateConfiguration);
this.registerWatcher();
this.initAsync();
}
public get onDidUpdateConfiguration(): Event<IConfigurationServiceEvent<T>> {
public get onDidUpdateConfiguration(): Event<IConfigurationChangeEvent<T>> {
return this._onDidUpdateConfiguration.event;
}
......@@ -144,16 +145,24 @@ export class ConfigWatcher<T> implements IConfigWatcher<T>, IDisposable {
}
this.timeoutHandle = global.setTimeout(() => {
this.loadAsync(currentConfig => {
if (!objects.equals(currentConfig, this.cache)) {
this.updateCache(currentConfig);
this._onDidUpdateConfiguration.fire({ config: this.cache });
}
});
this.reload();
}, this.options.changeBufferDelay);
}
public reload(callback?: (config: T) => void): void {
this.loadAsync(currentConfig => {
if (!objects.equals(currentConfig, this.cache)) {
this.updateCache(currentConfig);
this._onDidUpdateConfiguration.fire({ config: this.cache });
}
if (callback) {
return callback(currentConfig);
}
});
}
public getConfig(): T {
this.ensureLoaded();
......
......@@ -95,7 +95,7 @@ suite('Config', () => {
let watcher = new ConfigWatcher<{ foo: string; }>(testFile);
setTimeout(function() {
setTimeout(function () {
fs.writeFileSync(testFile, '// my comment\n{ "foo": "changed" }');
}, 50);
......@@ -111,4 +111,34 @@ suite('Config', () => {
});
});
test('reload', function (done: () => void) {
const id = uuid.generateUuid();
const parentDir = path.join(os.tmpdir(), 'vsctests', id);
const newDir = path.join(parentDir, 'config', id);
const testFile = path.join(newDir, 'config.json');
extfs.mkdirp(newDir, 493, (error) => {
fs.writeFileSync(testFile, '// my comment\n{ "foo": "bar" }');
let watcher = new ConfigWatcher<{ foo: string; }>(testFile, { changeBufferDelay: 100 });
setTimeout(function () {
fs.writeFileSync(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');
extfs.del(parentDir, os.tmpdir(), () => { }, done);
});
}, 50);
});
});
});
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册