configurationService.ts 4.2 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
import { TPromise } from 'vs/base/common/winjs.base';
import { ConfigWatcher } from 'vs/base/node/config';
9
import { Registry } from 'vs/platform/registry/common/platform';
J
Johannes Rieken 已提交
10
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, IConfigurationOverrides, Configuration, IConfigurationValues, IConfigurationData } 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: Configuration<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
	}

S
Sandeep Somavarapu 已提交
46
	public configuration(): Configuration<any> {
47 48 49
		return this._configuration || (this._configuration = this.consolidateConfigurations());
	}

50
	private onConfigurationChange(source: ConfigurationSource): void {
51
		this.reset(); // reset our caches
52

S
Sandeep Somavarapu 已提交
53
		const cache = this.configuration();
54

55 56
		this._onDidUpdateConfiguration.fire({
			source,
57
			sourceConfig: source === ConfigurationSource.Default ? cache.defaults.contents : cache.user.contents
58
		});
59 60
	}

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

70 71
	public getConfiguration<C>(section?: string, options?: IConfigurationOverrides): C {
		return this.configuration().getValue<C>(section, options);
72 73
	}

S
Sandeep Somavarapu 已提交
74 75
	public lookup<C>(key: string, options?: IConfigurationOverrides): IConfigurationValue<C> {
		return this.configuration().lookup<C>(key, options);
B
Benjamin Pasero 已提交
76 77
	}

B
Benjamin Pasero 已提交
78
	public keys(): IConfigurationKeys {
S
Sandeep Somavarapu 已提交
79
		return this.configuration().keys();
80
	}
81

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

86
	public getConfigurationData(): IConfigurationData<T> {
S
Sandeep Somavarapu 已提交
87
		return this.configuration().toData();
B
Benjamin Pasero 已提交
88 89
	}

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

94
	private consolidateConfigurations(): Configuration<T> {
95
		const defaults = new DefaultConfigurationModel<T>();
96
		const user = this.userConfigModelWatcher.getConfig();
97
		return new Configuration(defaults, user);
98 99
	}
}