Skip to content

Commit 09021f9

Browse files
committed
debt - simplify IContextKeyService#onDidChangeContext
1 parent 3f47356 commit 09021f9

1 file changed

Lines changed: 11 additions & 21 deletions

File tree

src/vs/platform/contextkey/browser/contextKeyService.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class ConfigAwareContextValuesContainer extends Context {
9595
constructor(
9696
id: number,
9797
private readonly _configurationService: IConfigurationService,
98-
emitter: Emitter<string | string[]>
98+
emitter: Emitter<IContextKeyChangeEvent>
9999
) {
100100
super(id, null);
101101

@@ -104,7 +104,7 @@ class ConfigAwareContextValuesContainer extends Context {
104104
// new setting, reset everything
105105
const allKeys = keys(this._values);
106106
this._values.clear();
107-
emitter.fire(allKeys);
107+
emitter.fire(new ArrayContextKeyChangeEvent(allKeys));
108108
} else {
109109
const changedKeys: string[] = [];
110110
for (const configKey of event.affectedKeys) {
@@ -114,7 +114,7 @@ class ConfigAwareContextValuesContainer extends Context {
114114
changedKeys.push(contextKey);
115115
}
116116
}
117-
emitter.fire(changedKeys);
117+
emitter.fire(new ArrayContextKeyChangeEvent(changedKeys));
118118
}
119119
});
120120
}
@@ -215,14 +215,12 @@ export abstract class AbstractContextKeyService implements IContextKeyService {
215215
public _serviceBrand: any;
216216

217217
protected _isDisposed: boolean;
218-
protected _onDidChangeContext: Event<IContextKeyChangeEvent>;
219-
protected _onDidChangeContextKey: Emitter<string | string[]>;
218+
protected _onDidChangeContext = new Emitter<IContextKeyChangeEvent>();
220219
protected _myContextId: number;
221220

222221
constructor(myContextId: number) {
223222
this._isDisposed = false;
224223
this._myContextId = myContextId;
225-
this._onDidChangeContextKey = new Emitter<string>();
226224
}
227225

228226
abstract dispose(): void;
@@ -235,21 +233,13 @@ export abstract class AbstractContextKeyService implements IContextKeyService {
235233
}
236234

237235
public get onDidChangeContext(): Event<IContextKeyChangeEvent> {
238-
if (!this._onDidChangeContext) {
239-
this._onDidChangeContext = Event.map(this._onDidChangeContextKey.event, ((changedKeyOrKeys): IContextKeyChangeEvent => {
240-
return typeof changedKeyOrKeys === 'string'
241-
? new SimpleContextKeyChangeEvent(changedKeyOrKeys)
242-
: new ArrayContextKeyChangeEvent(changedKeyOrKeys);
243-
}));
244-
}
245-
return this._onDidChangeContext;
236+
return this._onDidChangeContext.event;
246237
}
247-
248238
public createScoped(domNode: IContextKeyServiceTarget): IContextKeyService {
249239
if (this._isDisposed) {
250240
throw new Error(`AbstractContextKeyService has been disposed`);
251241
}
252-
return new ScopedContextKeyService(this, this._onDidChangeContextKey, domNode);
242+
return new ScopedContextKeyService(this, this._onDidChangeContext, domNode);
253243
}
254244

255245
public contextMatchesRules(rules: ContextKeyExpr | undefined): boolean {
@@ -280,7 +270,7 @@ export abstract class AbstractContextKeyService implements IContextKeyService {
280270
return;
281271
}
282272
if (myContext.setValue(key, value)) {
283-
this._onDidChangeContextKey.fire(key);
273+
this._onDidChangeContext.fire(new SimpleContextKeyChangeEvent(key));
284274
}
285275
}
286276

@@ -289,7 +279,7 @@ export abstract class AbstractContextKeyService implements IContextKeyService {
289279
return;
290280
}
291281
if (this.getContextValuesContainer(this._myContextId).removeValue(key)) {
292-
this._onDidChangeContextKey.fire(key);
282+
this._onDidChangeContext.fire(new SimpleContextKeyChangeEvent(key));
293283
}
294284
}
295285

@@ -319,7 +309,7 @@ export class ContextKeyService extends AbstractContextKeyService implements ICon
319309
this._lastContextId = 0;
320310
this._contexts = Object.create(null);
321311

322-
const myContext = new ConfigAwareContextValuesContainer(this._myContextId, configurationService, this._onDidChangeContextKey);
312+
const myContext = new ConfigAwareContextValuesContainer(this._myContextId, configurationService, this._onDidChangeContext);
323313
this._contexts[String(this._myContextId)] = myContext;
324314
this._toDispose.push(myContext);
325315

@@ -369,10 +359,10 @@ class ScopedContextKeyService extends AbstractContextKeyService {
369359
private _parent: AbstractContextKeyService;
370360
private _domNode: IContextKeyServiceTarget | undefined;
371361

372-
constructor(parent: AbstractContextKeyService, emitter: Emitter<string | string[]>, domNode?: IContextKeyServiceTarget) {
362+
constructor(parent: AbstractContextKeyService, emitter: Emitter<IContextKeyChangeEvent>, domNode?: IContextKeyServiceTarget) {
373363
super(parent.createChildContext());
374364
this._parent = parent;
375-
this._onDidChangeContextKey = emitter;
365+
this._onDidChangeContext = emitter;
376366

377367
if (domNode) {
378368
this._domNode = domNode;

0 commit comments

Comments
 (0)