mirror of
https://github.com/Microsoft/vscode
synced 2024-08-26 20:39:37 +00:00
Introduces Cache helper class.
This commit is contained in:
parent
3aafd130ec
commit
bfda62fd81
|
@ -24,11 +24,6 @@ export interface IAudioCueService {
|
|||
export class AudioCueService extends Disposable implements IAudioCueService {
|
||||
readonly _serviceBrand: undefined;
|
||||
|
||||
private readonly audioCueEnabledObservables = new Map<
|
||||
AudioCue,
|
||||
IObservable<boolean>
|
||||
>();
|
||||
|
||||
private readonly screenReaderAttached = fromEvent(
|
||||
this.accessibilityService.onDidChangeScreenReaderOptimized,
|
||||
() => this.accessibilityService.isScreenReaderOptimized()
|
||||
|
@ -65,16 +60,14 @@ export class AudioCueService extends Disposable implements IAudioCueService {
|
|||
}
|
||||
}
|
||||
|
||||
public isEnabled(cue: AudioCue): IObservable<boolean> {
|
||||
let observable = this.audioCueEnabledObservables.get(cue);
|
||||
if (!observable) {
|
||||
private readonly isEnabledCache = new Cache((cue: AudioCue) => {
|
||||
const settingObservable = fromEvent(
|
||||
Event.filter(this.configurationService.onDidChangeConfiguration, (e) =>
|
||||
e.affectsConfiguration(cue.settingsKey)
|
||||
),
|
||||
() => this.configurationService.getValue<'on' | 'off' | 'auto'>(cue.settingsKey)
|
||||
);
|
||||
observable = new LazyDerived(reader => {
|
||||
return new LazyDerived(reader => {
|
||||
const setting = settingObservable.read(reader);
|
||||
if (setting === 'auto') {
|
||||
return this.screenReaderAttached.read(reader);
|
||||
|
@ -83,9 +76,26 @@ export class AudioCueService extends Disposable implements IAudioCueService {
|
|||
}
|
||||
return false;
|
||||
}, 'audio cue enabled');
|
||||
this.audioCueEnabledObservables.set(cue, observable);
|
||||
});
|
||||
|
||||
public isEnabled(cue: AudioCue): IObservable<boolean> {
|
||||
return this.isEnabledCache.get(cue);
|
||||
}
|
||||
return observable;
|
||||
}
|
||||
|
||||
class Cache<TArg, TValue> {
|
||||
private readonly map = new Map<TArg, TValue>();
|
||||
constructor(private readonly getValue: (value: TArg) => TValue) {
|
||||
}
|
||||
|
||||
public get(arg: TArg): TValue {
|
||||
if (this.map.has(arg)) {
|
||||
return this.map.get(arg)!;
|
||||
}
|
||||
|
||||
const value = this.getValue(arg);
|
||||
this.map.set(arg, value);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue