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.

1 > /*--------------------------------------------------------------------------------------------- devToolsLogger.ts ×24
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { IObservable, ITransaction } from '../base.js';
7 > import { subtransaction } from '../transaction.js';
8 > import { EqualityComparer, Event, IDisposable, strictEquals } from '../commonFacade/deps.js';
9 > import { DebugOwner, DebugNameData, IDebugNameData } from '../debugName.js';
10 > import { getLogger } from '../logging/logging.js';
11 > import { BaseObservable } from './baseObservable.js';
12 > import { DebugLocation } from '../debugLocation.js';
13 >
14 >
15 > export function observableFromEvent<T, TArgs = unknown>(
16 > owner: DebugOwner,
17 > event: Event<TArgs>,
18 > getValue: (args: TArgs | undefined) => T,
19 > debugLocation?: DebugLocation,
20 > ): IObservable<T>;
21 > export function observableFromEvent<T, TArgs = unknown>(
22 > event: Event<TArgs>,
23 > getValue: (args: TArgs | undefined) => T,
24 > ): IObservable<T>;
25 > export function observableFromEvent(...args:
26 > [owner: DebugOwner, event: Event<any>, getValue: (args: any | undefined) => any, debugLocation?: DebugLocation] | observableFromEvent.ts ×3
27 > [event: Event<any>, getValue: (args: any | undefined) => any]
28 > ): IObservable<any> {
29 > let owner;
30 > let event;
31 > let getValue;
32 > let debugLocation;
33 > if (args.length === 2) {
34 > [event, getValue] = args; observableFromEvent.ts ×1
36 > [owner, event, getValue, debugLocation] = args; observableFromEvent.ts ×1
37 > }
38 > return new FromEventObservable( observableFromEvent.ts ×3
39 > new DebugNameData(owner, undefined, getValue),
40 > event,
41 > getValue,
42 > () => FromEventObservable.globalTransaction,
43 > strictEquals,
44 > debugLocation ?? DebugLocation.ofCaller()
45 > );
46 > }
48 > export function observableFromEventOpts<T, TArgs = unknown>(
49 > options: IDebugNameData & { mcpService.ts ×13
50 > equalsFn?: EqualityComparer<T>;
51 > getTransaction?: () => ITransaction | undefined;
52 > },
53 > event: Event<TArgs>,
54 > getValue: (args: TArgs | undefined) => T,
55 > debugLocation = DebugLocation.ofCaller()
56 > ): IObservable<T> {
57 > return new FromEventObservable(
58 > new DebugNameData(options.owner, options.debugName, options.debugReferenceFn ?? getValue),
59 > event,
60 > getValue,
61 > () => options.getTransaction?.() ?? FromEventObservable.globalTransaction,
62 > options.equalsFn ?? strictEquals,
63 > debugLocation
64 > );
65 > }
67 > export class FromEventObservable<TArgs, T> extends BaseObservable<T> {
68 > public static globalTransaction: ITransaction | undefined;
69 >
70 > private _value: T | undefined;
71 > private _hasValue = false;
72 > private _subscription: IDisposable | undefined;
73 >
74 > constructor(
75 > private readonly _debugNameData: DebugNameData, observableFromEvent.ts ×4
76 > private readonly event: Event<TArgs>,
77 > public readonly _getValue: (args: TArgs | undefined) => T,
78 > private readonly _getTransaction: () => ITransaction | undefined,
79 > private readonly _equalityComparator: EqualityComparer<T>,
80 > debugLocation: DebugLocation
81 > ) {
82 > super(debugLocation);
83 > }
84 >
85 > private getDebugName(): string | undefined {
86 return this._debugNameData.getDebugName(this);
87 }
89 > public get debugName(): string {
90 const name = this.getDebugName();
91 return 'From Event' + (name ? `: ${name}` : '');
92 }
94 > protected override onFirstObserverAdded(): void {
95 > this._subscription = this.event(this.handleEvent); observableFromEvent.ts ×5
96 > }
98 > private readonly handleEvent = (args: TArgs | undefined) => {
99 > const newValue = this._getValue(args); observableFromEvent.ts ×5
100 > const oldValue = this._value;
101 >
102 > const didChange = !this._hasValue || !(this._equalityComparator(oldValue!, newValue));
103 > let didRunTransaction = false;
104 >
105 > if (didChange) {
106 > this._value = newValue;
107 >
108 > if (this._hasValue) {
109 > didRunTransaction = true; observableFromEvent.ts ×2
110 > subtransaction(
111 > this._getTransaction(),
112 > (tx) => {
113 > getLogger()?.handleObservableUpdated(this, { oldValue, newValue, change: undefined, didChange, hadValue: this._hasValue });
114 >
115 > for (const o of this._observers) {
116 > tx.updateObserver(o, this);
117 > o.handleChange(this, undefined);
118 > }
119 > },
120 > () => {
121 const name = this.getDebugName();
122 return 'Event fired' + (name ? `: ${name}` : '');
123 }
125 > }
126 > this._hasValue = true; observableFromEvent.ts ×5
127 > }
128 >
129 > if (!didRunTransaction) {
130 > getLogger()?.handleObservableUpdated(this, { oldValue, newValue, change: undefined, didChange, hadValue: this._hasValue });
131 > }
132 > };
134 > protected override onLastObserverRemoved(): void {
135 > this._subscription!.dispose(); observableFromEvent.ts ×5
136 > this._subscription = undefined;
137 > this._hasValue = false;
138 > this._value = undefined;
139 > }
141 > public get(): T {
142 > if (this._subscription) { observableFromEvent.ts ×3
143 > if (!this._hasValue) { observableFromEvent.ts ×5
144 > this.handleEvent(undefined);
145 > }
146 > return this._value!;
148 > // no cache, as there are no subscribers to keep it updated observableFromEvent.ts ×1
149 > const value = this._getValue(undefined);
150 > return value;
151 > }
154 > public debugSetValue(value: unknown): void {
155 // eslint-disable-next-line local/code-no-any-casts
156 this._value = value as any;
157 }
159 > public debugGetState() {
160 return { value: this._value, hasValue: this._hasValue };
161 }
163 >
164 > export namespace observableFromEvent {
165 > export const Observer = FromEventObservable;
166 >
167 > export function batchEventsGlobally(tx: ITransaction, fn: () => void): void {
168 let didSet = false;
169 if (FromEventObservable.globalTransaction === undefined) {
170 FromEventObservable.globalTransaction = tx;
171 didSet = true;
172 }
173 try {
174 fn();
175 } finally {
176 if (didSet) {
177 FromEventObservable.globalTransaction = undefined;
178 }
179 }
180 }