src/vs/base/common/observableInternal/observables/observableValue.ts

129 LOC · 120 covered · 9 uncovered · 38 ranges · 6771 concepts · 14 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 { ISettableObservable, ITransaction } from '../base.js';
7 > import { TransactionImpl } from '../transaction.js';
8 > import { BaseObservable } from './baseObservable.js';
9 > import { EqualityComparer, IDisposable, strictEquals } from '../commonFacade/deps.js';
10 > import { DebugNameData } from '../debugName.js';
11 > import { getLogger } from '../logging/logging.js';
12 > import { DebugLocation } from '../debugLocation.js';
13 >
14 > /**
15 > * Creates an observable value.
16 > * Observers get informed when the value changes.
17 > * @template TChange An arbitrary type to describe how or why the value changed. Defaults to `void`.
18 > * Observers will receive every single change value.
19 > */
20 >
21 > export function observableValue<T, TChange = void>(name: string, initialValue: T): ISettableObservable<T, TChange>;
22 > export function observableValue<T, TChange = void>(owner: object, initialValue: T): ISettableObservable<T, TChange>;
23 > export function observableValue<T, TChange = void>(nameOrOwner: string | object, initialValue: T, debugLocation = DebugLocation.ofCaller()): ISettableObservable<T, TChange> {
24 > let debugNameData: DebugNameData; observableValue.ts ×3
25 > if (typeof nameOrOwner === 'string') {
26 > debugNameData = new DebugNameData(undefined, nameOrOwner, undefined); observableValue.ts ×1
27 > } else { observableValue.ts ×3
28 > debugNameData = new DebugNameData(nameOrOwner, undefined, undefined); observableValue.ts ×1
29 > }
30 > return new ObservableValue(debugNameData, initialValue, strictEquals, debugLocation); observableValue.ts ×3
31 > }
33 > export class ObservableValue<T, TChange = void>
34 > extends BaseObservable<T, TChange>
35 > implements ISettableObservable<T, TChange> {
36 > protected _value: T;
37 >
38 > get debugName() {
39 > return this._debugNameData.getDebugName(this) ?? 'ObservableValue';
40 > }
41 >
42 > constructor(
43 > private readonly _debugNameData: DebugNameData, observableValue.ts ×1
44 > initialValue: T,
45 > private readonly _equalityComparator: EqualityComparer<T>,
46 > debugLocation: DebugLocation
47 > ) {
48 > super(debugLocation);
49 > this._value = initialValue;
50 >
51 > getLogger()?.handleObservableUpdated(this, { hadValue: false, newValue: initialValue, change: undefined, didChange: true, oldValue: undefined });
52 > }
53 > public override get(): T { devToolsLogger.ts ×24
54 > return this._value; observableValue.ts ×1
55 > }
57 > public set(value: T, tx: ITransaction | undefined, change: TChange): void {
58 > if (change === undefined && this._equalityComparator(this._value, value)) { observableValue.ts ×3
59 > return; observableValue.ts ×1
60 > }
62 > let _tx: TransactionImpl | undefined;
63 > if (!tx) {
64 > tx = _tx = new TransactionImpl(() => { }, () => `Setting ${this.debugName}`); observableValue.ts ×2
65 > }
67 > const oldValue = this._value;
68 > this._setValue(value);
69 > getLogger()?.handleObservableUpdated(this, { oldValue, newValue: value, change, didChange: true, hadValue: true }); observableValue.ts ×3
70 >
71 > for (const observer of this._observers) {
72 > tx.updateObserver(observer, this); observableValue.ts ×1
73 > observer.handleChange(this, change);
74 > }
75 > } finally { observableValue.ts ×5
76 > if (_tx) {
77 > _tx.finish(); observableValue.ts ×2
78 > }
82 > override toString(): string {
83 return `${this.debugName}: ${this._value}`;
84 }
86 > protected _setValue(newValue: T): void {
87 > this._value = newValue; observableValue.ts ×5
88 > }
90 > public debugGetState() {
92 > value: this._value,
93 > };
94 > }
96 > public debugSetValue(value: unknown) {
97 this._value = value as T;
98 }
100 > /**
101 > * A disposable observable. When disposed, its value is also disposed.
102 > * When a new value is set, the previous value is disposed.
103 > */
104 >
105 > export function disposableObservableValue<T extends IDisposable | undefined, TChange = void>(nameOrOwner: string | object, initialValue: T, debugLocation = DebugLocation.ofCaller()): ISettableObservable<T, TChange> & IDisposable {
106 > let debugNameData: DebugNameData; mcpServer.ts ×23
107 > if (typeof nameOrOwner === 'string') {
108 debugNameData = new DebugNameData(undefined, nameOrOwner, undefined);
109 > } else { mcpServer.ts ×23
110 > debugNameData = new DebugNameData(nameOrOwner, undefined, undefined);
111 > }
112 > return new DisposableObservableValue(debugNameData, initialValue, strictEquals, debugLocation);
113 > }
115 > export class DisposableObservableValue<T extends IDisposable | undefined, TChange = void> extends ObservableValue<T, TChange> implements IDisposable {
116 > protected override _setValue(newValue: T): void {
117 > if (this._value === newValue) { mcpServer.ts ×36
118 return;
119 }
120 > if (this._value) { mcpServer.ts ×36
121 this._value.dispose();
122 }
123 > this._value = newValue; mcpServer.ts ×36
124 > }
126 > public dispose(): void {
127 > this._value?.dispose(); mcpServer.ts ×23
128 > }