-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
preference-provider.ts
231 lines (206 loc) · 9.05 KB
/
preference-provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/********************************************************************************
* Copyright (C) 2018 Ericsson and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
/* eslint-disable @typescript-eslint/no-explicit-any */
import debounce = require('p-debounce');
import { injectable } from 'inversify';
import { JSONExt, JSONValue } from '@phosphor/coreutils/lib/json';
import URI from '../../common/uri';
import { Disposable, DisposableCollection, Emitter, Event } from '../../common';
import { Deferred } from '../../common/promise-util';
import { PreferenceScope } from './preference-scope';
export interface PreferenceProviderDataChange {
readonly preferenceName: string;
readonly newValue?: any;
readonly oldValue?: any;
readonly scope: PreferenceScope;
readonly domain?: string[];
}
export namespace PreferenceProviderDataChange {
export function affects(change: PreferenceProviderDataChange, resourceUri?: string): boolean {
const resourcePath = resourceUri && new URI(resourceUri).path;
const domain = change.domain;
return !resourcePath || !domain || domain.some(uri => new URI(uri).path.relativity(resourcePath) >= 0);
}
}
export interface PreferenceProviderDataChanges {
[preferenceName: string]: PreferenceProviderDataChange;
}
export interface PreferenceResolveResult<T> {
configUri?: URI
value?: T
}
/**
* The {@link PreferenceProvider} is used to store and retrieve preference values. A {@link PreferenceProvider} does not operate in a global scope but is
* configured for one or more {@link PreferenceScope}s. The (default implementation for the) {@link PreferenceService} aggregates all {@link PreferenceProvider}s and
* serves as a common facade for manipulating preference values.
*/
@injectable()
export abstract class PreferenceProvider implements Disposable {
protected readonly onDidPreferencesChangedEmitter = new Emitter<PreferenceProviderDataChanges>();
readonly onDidPreferencesChanged: Event<PreferenceProviderDataChanges> = this.onDidPreferencesChangedEmitter.event;
protected readonly toDispose = new DisposableCollection();
protected readonly _ready = new Deferred<void>();
constructor() {
this.toDispose.push(this.onDidPreferencesChangedEmitter);
}
dispose(): void {
this.toDispose.dispose();
}
protected deferredChanges: PreferenceProviderDataChanges | undefined;
protected _pendingChanges: Promise<boolean> = Promise.resolve(false);
get pendingChanges(): Promise<boolean> {
return this._pendingChanges;
}
/**
* Informs the listeners that one or more preferences of this provider are changed.
* The listeners are able to find what was changed from the emitted event.
*/
protected emitPreferencesChangedEvent(changes: PreferenceProviderDataChanges | PreferenceProviderDataChange[]): Promise<boolean> {
if (Array.isArray(changes)) {
for (const change of changes) {
this.mergePreferenceProviderDataChange(change);
}
} else {
for (const preferenceName of Object.keys(changes)) {
this.mergePreferenceProviderDataChange(changes[preferenceName]);
}
}
return this._pendingChanges = this.fireDidPreferencesChanged();
}
protected mergePreferenceProviderDataChange(change: PreferenceProviderDataChange): void {
if (!this.deferredChanges) {
this.deferredChanges = {};
}
const current = this.deferredChanges[change.preferenceName];
const { newValue, scope, domain } = change;
if (!current) {
// new
this.deferredChanges[change.preferenceName] = change;
} else if (current.oldValue === newValue) {
// delete
delete this.deferredChanges[change.preferenceName];
} else {
// update
Object.assign(current, { newValue, scope, domain });
}
}
protected fireDidPreferencesChanged = debounce(() => {
const changes = this.deferredChanges;
this.deferredChanges = undefined;
if (changes && Object.keys(changes).length) {
this.onDidPreferencesChangedEmitter.fire(changes);
return true;
}
return false;
}, 0);
/**
* Retrieve the stored value for the given preference and resource URI.
*
* @param preferenceName the preference identifier.
* @param resourceUri the uri of the resource for which the preference is stored. This is used to retrieve
* a potentially different value for the same preference for different resources, for example `files.encoding`.
*
* @returns the value stored for the given preference and resourceUri if it exists, otherwise `undefined`.
*/
get<T>(preferenceName: string, resourceUri?: string): T | undefined {
return this.resolve<T>(preferenceName, resourceUri).value;
}
/**
* Resolve the value for the given preference and resource URI.
*
* @param preferenceName the preference identifier.
* @param resourceUri the URI of the resource for which this provider should resolve the preference. This is used to retrieve
* a potentially different value for the same preference for different resources, for example `files.encoding`.
*
* @returns an object containing the value stored for the given preference and resourceUri if it exists,
* otherwise `undefined`.
*/
resolve<T>(preferenceName: string, resourceUri?: string): PreferenceResolveResult<T> {
const value = this.getPreferences(resourceUri)[preferenceName];
if (value !== undefined) {
return {
value,
configUri: this.getConfigUri(resourceUri)
};
}
return {};
}
abstract getPreferences(resourceUri?: string): { [p: string]: any };
/**
* Stores a new value for the given preference key in the provider.
* @param key the preference key (typically the name).
* @param value the new preference value.
* @param resourceUri the URI of the resource for which the preference is stored.
*
* @returns a promise that only resolves if all changes were delivered.
* If changes were made then implementation must either
* await on `this.emitPreferencesChangedEvent(...)` or
* `this.pendingChanges` if changes are fired indirectly.
*/
abstract setPreference(key: string, value: any, resourceUri?: string): Promise<boolean>;
/**
* Resolved when the preference provider is ready to provide preferences
* It should be resolved by subclasses.
*/
get ready(): Promise<void> {
return this._ready.promise;
}
/**
* Retrieve the domain for this provider.
*
* @returns the domain or `undefined` if this provider is suitable for all domains.
*/
getDomain(): string[] | undefined {
return undefined;
}
/**
* Retrieve the configuration URI for the given resource URI.
* @param resourceUri the uri of the resource or `undefined`.
* @param sectionName the section to return the URI for, e.g. `tasks` or `launch`. Defaults to settings.
*
* @returns the corresponding resource URI or `undefined` if there is no valid URI.
*/
getConfigUri(resourceUri?: string, sectionName?: string): URI | undefined {
return undefined;
}
/**
* Retrieves the first valid configuration URI contained by the given resource.
* @param resourceUri the uri of the container resource or `undefined`.
*
* @returns the first valid configuration URI contained by the given resource `undefined`
* if there is no valid configuration URI at all.
*/
getContainingConfigUri?(resourceUri?: string, sectionName?: string): URI | undefined;
static merge(source: JSONValue | undefined, target: JSONValue): JSONValue {
if (source === undefined || !JSONExt.isObject(source)) {
return JSONExt.deepCopy(target);
}
if (JSONExt.isPrimitive(target)) {
return {};
}
for (const key of Object.keys(target)) {
const value = (target as any)[key];
if (key in source) {
if (JSONExt.isObject(source[key]) && JSONExt.isObject(value)) {
this.merge(source[key], value);
continue;
}
}
source[key] = JSONExt.deepCopy(value);
}
return source;
}
}