configurationService.ts 4.0 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
import { ConfigWatcher } from 'vs/base/node/config';
8
import { Registry } from 'vs/platform/registry/common/platform';
9
import { IConfigurationRegistry, Extensions, IConfigurationNode } from 'vs/platform/configuration/common/configurationRegistry';
B
Benjamin Pasero 已提交
10
import { IDisposable, Disposable } from 'vs/base/common/lifecycle';
11 12
import { IConfigurationService, IConfigurationServiceEvent, IConfigurationOverrides, IConfiguration } from 'vs/platform/configuration/common/configuration2';
import { ConfigurationModel, Configuration } 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';
B
Benjamin Pasero 已提交
16
import { onUnexpectedError } from 'vs/base/common/errors';
17

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

	_serviceBrand: any;

22
	private _configuration: Configuration<T>;
23
	private userConfigModelWatcher: ConfigWatcher<ConfigurationModel<T>>;
24

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

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

		this.userConfigModelWatcher = new ConfigWatcher(environmentService.appSettingsPath, {
34
			changeBufferDelay: 300, onError: error => onUnexpectedError(error), defaultConfig: new CustomConfigurationModel<T>(null, environmentService.appSettingsPath), parse: (content: string, parseErrors: any[]) => {
35
				const userConfigModel = new CustomConfigurationModel<T>(content, environmentService.appSettingsPath);
36 37 38 39
				parseErrors = [...userConfigModel.errors];
				return userConfigModel;
			}
		});
B
Benjamin Pasero 已提交
40
		this._register(this.userConfigModelWatcher);
41

B
Benjamin Pasero 已提交
42
		// Listeners
43 44
		this._register(this.userConfigModelWatcher.onDidUpdateConfiguration(() => this.onDidUpdateConfigModel()));
		this._register(Registry.as<IConfigurationRegistry>(Extensions.Configuration).onDidRegisterConfiguration(configurationNodes => this.onDidRegisterConfiguration(configurationNodes)));
45 46
	}

47
	public get configuration(): Configuration<any> {
48 49 50
		return this._configuration || (this._configuration = this.consolidateConfigurations());
	}

51 52 53 54
	private onDidUpdateConfigModel(): void {
		// get the diff
		// reset and trigger
		this.onConfigurationChange([], []);
55 56
	}

57 58 59 60
	private onDidRegisterConfiguration(configurations: IConfigurationNode[]): void {
		// get the diff
		// reset and trigger
		this.onConfigurationChange([], []);
61 62
	}

63 64
	private onConfigurationChange(sections: string[], keys: string[]): void {
		this.reset(); // reset our caches
65

66
		this._onDidUpdateConfiguration.fire({ sections, keys });
B
Benjamin Pasero 已提交
67 68
	}

69 70
	public getConfiguration(section?: string, options?: IConfigurationOverrides): IConfiguration {
		return this.configuration.getValue(section, options);
71
	}
72

73 74 75 76 77 78 79 80
	public inspect<T>(key: string): {
		default: T,
		user: T,
		workspace: T,
		workspaceFolder: T
		value: T
	} {
		return this.configuration.lookup<T>(key);
81 82
	}

83 84 85 86 87 88 89
	public keys(): {
		default: string[];
		user: string[];
		workspace: string[];
		workspaceFolder: string[];
	} {
		return this.configuration.keys();
B
Benjamin Pasero 已提交
90 91
	}

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

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