src/vs/base/common/observableInternal/observables/observableFromEvent.ts
181 LOC · 155 covered · 26 uncovered · 30 ranges · 6771 concepts · 10 introducers · 3467 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
devToolsLogger.ts ×24
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { IObservable, ITransaction } from '../base.js';
import { subtransaction } from '../transaction.js';
import { EqualityComparer, Event, IDisposable, strictEquals } from '../commonFacade/deps.js';
import { DebugOwner, DebugNameData, IDebugNameData } from '../debugName.js';
import { getLogger } from '../logging/logging.js';
import { BaseObservable } from './baseObservable.js';
import { DebugLocation } from '../debugLocation.js';
export function observableFromEvent<T, TArgs = unknown>(
owner: DebugOwner,
event: Event<TArgs>,
getValue: (args: TArgs | undefined) => T,
debugLocation?: DebugLocation,
): IObservable<T>;
export function observableFromEvent<T, TArgs = unknown>(
event: Event<TArgs>,
getValue: (args: TArgs | undefined) => T,
): IObservable<T>;
export function observableFromEvent(...args:
[owner: DebugOwner, event: Event<any>, getValue: (args: any | undefined) => any, debugLocation?: DebugLocation] |
observableFromEvent.ts ×3
[event: Event<any>, getValue: (args: any | undefined) => any]
): IObservable<any> {
let owner;
let event;
let getValue;
let debugLocation;
if (args.length === 2) {
}
new DebugNameData(owner, undefined, getValue),
event,
getValue,
() => FromEventObservable.globalTransaction,
strictEquals,
debugLocation ?? DebugLocation.ofCaller()
);
}
export function observableFromEventOpts<T, TArgs = unknown>(
equalsFn?: EqualityComparer<T>;
getTransaction?: () => ITransaction | undefined;
},
event: Event<TArgs>,
getValue: (args: TArgs | undefined) => T,
debugLocation = DebugLocation.ofCaller()
): IObservable<T> {
return new FromEventObservable(
new DebugNameData(options.owner, options.debugName, options.debugReferenceFn ?? getValue),
event,
getValue,
() => options.getTransaction?.() ?? FromEventObservable.globalTransaction,
options.equalsFn ?? strictEquals,
debugLocation
);
}
export class FromEventObservable<TArgs, T> extends BaseObservable<T> {
public static globalTransaction: ITransaction | undefined;
private _value: T | undefined;
private _hasValue = false;
private _subscription: IDisposable | undefined;
constructor(
private readonly event: Event<TArgs>,
public readonly _getValue: (args: TArgs | undefined) => T,
private readonly _getTransaction: () => ITransaction | undefined,
private readonly _equalityComparator: EqualityComparer<T>,
debugLocation: DebugLocation
) {
super(debugLocation);
}
private getDebugName(): string | undefined {
return this._debugNameData.getDebugName(this);
}
public get debugName(): string {
const name = this.getDebugName();
return 'From Event' + (name ? `: ${name}` : '');
}
protected override onFirstObserverAdded(): void {
}
private readonly handleEvent = (args: TArgs | undefined) => {
const oldValue = this._value;
const didChange = !this._hasValue || !(this._equalityComparator(oldValue!, newValue));
let didRunTransaction = false;
if (didChange) {
this._value = newValue;
if (this._hasValue) {
subtransaction(
this._getTransaction(),
(tx) => {
getLogger()?.handleObservableUpdated(this, { oldValue, newValue, change: undefined, didChange, hadValue: this._hasValue });
for (const o of this._observers) {
tx.updateObserver(o, this);
o.handleChange(this, undefined);
}
},
() => {
const name = this.getDebugName();
return 'Event fired' + (name ? `: ${name}` : '');
}
}
}
if (!didRunTransaction) {
getLogger()?.handleObservableUpdated(this, { oldValue, newValue, change: undefined, didChange, hadValue: this._hasValue });
}
};
protected override onLastObserverRemoved(): void {
this._subscription = undefined;
this._hasValue = false;
this._value = undefined;
}
public get(): T {
this.handleEvent(undefined);
}
return this._value!;
const value = this._getValue(undefined);
return value;
}
public debugSetValue(value: unknown): void {
// eslint-disable-next-line local/code-no-any-casts
this._value = value as any;
}
public debugGetState() {
return { value: this._value, hasValue: this._hasValue };
}
export namespace observableFromEvent {
export const Observer = FromEventObservable;
export function batchEventsGlobally(tx: ITransaction, fn: () => void): void {
let didSet = false;
if (FromEventObservable.globalTransaction === undefined) {
FromEventObservable.globalTransaction = tx;
didSet = true;
}
try {
fn();
} finally {
if (didSet) {
FromEventObservable.globalTransaction = undefined;
}
}
}