configurationService.ts 4.3 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.
 *--------------------------------------------------------------------------------------------*/
'use strict';

J
Johannes Rieken 已提交
7 8 9 10
import { TPromise } from 'vs/base/common/winjs.base';
import { ConfigWatcher } from 'vs/base/node/config';
import { Registry } from 'vs/platform/platform';
import { IConfigurationRegistry, Extensions } from 'vs/platform/configuration/common/configurationRegistry';
S
Sandeep Somavarapu 已提交
11
import { IDisposable, toDisposable, Disposable } from 'vs/base/common/lifecycle';
12
import { ConfigurationSource, IConfigurationService, IConfigurationServiceEvent, IConfigurationValue, IConfigurationKeys, ConfigurationModel, IConfigurationOptions, ConfigurationData, IConfigurationValues } from 'vs/platform/configuration/common/configuration';
13
import { CustomConfigurationModel, DefaultConfigurationModel } from 'vs/platform/configuration/common/model';
J
Johannes Rieken 已提交
14 15
import Event, { Emitter } from 'vs/base/common/event';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
16

17
export class ConfigurationService<T> extends Disposable implements IConfigurationService, IDisposable {
18 19 20

	_serviceBrand: any;

21
	private _configuration: ConfigurationData<T>;
22
	private userConfigModelWatcher: ConfigWatcher<ConfigurationModel<T>>;
23

24 25
	private _onDidUpdateConfiguration: Emitter<IConfigurationServiceEvent> = this._register(new Emitter<IConfigurationServiceEvent>());
	public readonly onDidUpdateConfiguration: Event<IConfigurationServiceEvent> = this._onDidUpdateConfiguration.event;
26 27 28 29

	constructor(
		@IEnvironmentService environmentService: IEnvironmentService
	) {
30 31 32
		super();

		this.userConfigModelWatcher = new ConfigWatcher(environmentService.appSettingsPath, {
33 34
			changeBufferDelay: 300, defaultConfig: new CustomConfigurationModel<T>(null, environmentService.appSettingsPath), parse: (content: string, parseErrors: any[]) => {
				const userConfigModel = new CustomConfigurationModel<T>(content, environmentService.appSettingsPath);
35 36 37 38 39
				parseErrors = [...userConfigModel.errors];
				return userConfigModel;
			}
		});
		this._register(toDisposable(() => this.userConfigModelWatcher.dispose()));
40

B
Benjamin Pasero 已提交
41
		// Listeners
42 43
		this._register(this.userConfigModelWatcher.onDidUpdateConfiguration(() => this.onConfigurationChange(ConfigurationSource.User)));
		this._register(Registry.as<IConfigurationRegistry>(Extensions.Configuration).onDidRegisterConfiguration(() => this.onConfigurationChange(ConfigurationSource.Default)));
44 45
	}

46
	private onConfigurationChange(source: ConfigurationSource): void {
47
		this.reset(); // reset our caches
48

49
		const cache = this.getConfigurationData();
50

51 52
		this._onDidUpdateConfiguration.fire({
			source,
53
			sourceConfig: source === ConfigurationSource.Default ? cache.defaults.contents : cache.user.contents
54
		});
55 56
	}

57
	public reloadConfiguration<C>(section?: string): TPromise<C> {
B
Benjamin Pasero 已提交
58
		return new TPromise<C>(c => {
59
			this.userConfigModelWatcher.reload(() => {
60
				this.reset(); // reset our caches
B
Benjamin Pasero 已提交
61
				c(this.getConfiguration<C>(section));
62 63 64 65
			});
		});
	}

66 67 68
	public getConfiguration<C>(section?: string): C
	public getConfiguration<C>(options?: IConfigurationOptions): C
	public getConfiguration<C>(arg?: any): C {
69
		return this.getConfigurationData().getValue<C>(this.toOptions(arg));
70 71
	}

72
	public lookup<C>(key: string, overrideIdentifier?: string): IConfigurationValue<C> {
73
		return this.getConfigurationData().lookup<C>(key, overrideIdentifier);
B
Benjamin Pasero 已提交
74 75
	}

B
Benjamin Pasero 已提交
76
	public keys(): IConfigurationKeys {
77
		return this.getConfigurationData().keys();
78
	}
79

80 81 82 83
	public values<V>(): IConfigurationValues {
		return this._configuration.values();
	}

84
	public getConfigurationData(): ConfigurationData<T> {
85
		return this._configuration || (this._configuration = this.consolidateConfigurations());
B
Benjamin Pasero 已提交
86 87
	}

88 89
	private reset(): void {
		this._configuration = this.consolidateConfigurations();
90
	}
91

92 93 94 95 96 97 98 99
	private toOptions(arg: any): IConfigurationOptions {
		if (typeof arg === 'string') {
			return { section: arg };
		}
		if (typeof arg === 'object') {
			return arg;
		}
		return {};
100 101
	}

102
	private consolidateConfigurations(): ConfigurationData<T> {
103
		const defaults = new DefaultConfigurationModel<T>();
104
		const user = this.userConfigModelWatcher.getConfig();
105
		return new ConfigurationData(defaults, user);
106 107
	}
}