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

152 LOC · 49 covered · 103 uncovered · 11 ranges · 6771 concepts · 1 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 { EqualityComparer } from '../commonFacade/deps.js';
7 > import { IObserver, ISettableObservable, ITransaction } from '../base.js';
8 > import { TransactionImpl } from '../transaction.js';
9 > import { DebugNameData } 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 > * Holds off updating observers until the value is actually read.
16 > */
17 > export class LazyObservableValue<T, TChange = void>
18 > extends BaseObservable<T, TChange>
19 > implements ISettableObservable<T, TChange> {
20 > protected _value: T;
21 > private _isUpToDate = true;
22 > private readonly _deltas: TChange[] = [];
23 >
24 > get debugName() {
25 > return this._debugNameData.getDebugName(this) ?? 'LazyObservableValue';
26 > }
27 >
28 > constructor(
29 private readonly _debugNameData: DebugNameData,
30 initialValue: T,
31 private readonly _equalityComparator: EqualityComparer<T>,
32 debugLocation: DebugLocation
33 ) {
34 super(debugLocation);
35 this._value = initialValue;
36 }
38 > public override get(): T {
39 this._update();
40 return this._value;
41 }
43 > private _update(): void {
44 if (this._isUpToDate) {
45 return;
46 }
47 this._isUpToDate = true;
48
49 if (this._deltas.length > 0) {
50 for (const change of this._deltas) {
51 getLogger()?.handleObservableUpdated(this, { change, didChange: true, oldValue: '(unknown)', newValue: this._value, hadValue: true });
52 for (const observer of this._observers) {
53 observer.handleChange(this, change);
54 }
55 }
56 this._deltas.length = 0;
57 } else {
58 getLogger()?.handleObservableUpdated(this, { change: undefined, didChange: true, oldValue: '(unknown)', newValue: this._value, hadValue: true });
59 for (const observer of this._observers) {
60 observer.handleChange(this, undefined);
61 }
62 }
63 }
65 > private _updateCounter = 0;
66 >
67 > private _beginUpdate(): void {
68 this._updateCounter++;
69 if (this._updateCounter === 1) {
70 for (const observer of this._observers) {
71 observer.beginUpdate(this);
72 }
73 }
74 }
76 > private _endUpdate(): void {
77 this._updateCounter--;
78 if (this._updateCounter === 0) {
79 this._update();
80
81 // End update could change the observer list.
82 const observers = [...this._observers];
83 for (const r of observers) {
84 r.endUpdate(this);
85 }
86 }
87 }
89 > public override addObserver(observer: IObserver): void {
90 const shouldCallBeginUpdate = !this._observers.has(observer) && this._updateCounter > 0;
91 super.addObserver(observer);
92
93 if (shouldCallBeginUpdate) {
94 observer.beginUpdate(this);
95 }
96 }
98 > public override removeObserver(observer: IObserver): void {
99 const shouldCallEndUpdate = this._observers.has(observer) && this._updateCounter > 0;
100 super.removeObserver(observer);
101
102 if (shouldCallEndUpdate) {
103 // Calling end update after removing the observer makes sure endUpdate cannot be called twice here.
104 observer.endUpdate(this);
105 }
106 }
108 > public set(value: T, tx: ITransaction | undefined, change: TChange): void {
109 if (change === undefined && this._equalityComparator(this._value, value)) {
110 return;
111 }
112
113 let _tx: TransactionImpl | undefined;
114 if (!tx) {
115 tx = _tx = new TransactionImpl(() => { }, () => `Setting ${this.debugName}`);
116 }
117 try {
118 this._isUpToDate = false;
119 this._setValue(value);
120 if (change !== undefined) {
121 this._deltas.push(change);
122 }
123
124 tx.updateObserver({
125 beginUpdate: () => this._beginUpdate(),
126 endUpdate: () => this._endUpdate(),
127 handleChange: (observable, change) => { },
128 handlePossibleChange: (observable) => { },
129 }, this);
130
131 if (this._updateCounter > 1) {
132 // We already started begin/end update, so we need to manually call handlePossibleChange
133 for (const observer of this._observers) {
134 observer.handlePossibleChange(this);
135 }
136 }
137
138 } finally {
139 if (_tx) {
140 _tx.finish();
141 }
142 }
143 }
145 > override toString(): string {
146 return `${this.debugName}: ${this._value}`;
147 }
149 > protected _setValue(newValue: T): void {
150 this._value = newValue;
151 }