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.
/*---------------------------------------------------------------------------------------------
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 { EqualityComparer } from '../commonFacade/deps.js';
import { IObserver, ISettableObservable, ITransaction } from '../base.js';
import { TransactionImpl } from '../transaction.js';
import { DebugNameData } from '../debugName.js';
import { getLogger } from '../logging/logging.js';
import { BaseObservable } from './baseObservable.js';
import { DebugLocation } from '../debugLocation.js';
/**
* Holds off updating observers until the value is actually read.
*/
export class LazyObservableValue<T, TChange = void>
extends BaseObservable<T, TChange>
implements ISettableObservable<T, TChange> {
protected _value: T;
private _isUpToDate = true;
private readonly _deltas: TChange[] = [];
get debugName() {
return this._debugNameData.getDebugName(this) ?? 'LazyObservableValue';
}
constructor(
private readonly _debugNameData: DebugNameData,
initialValue: T,
private readonly _equalityComparator: EqualityComparer<T>,
debugLocation: DebugLocation
) {
super(debugLocation);
this._value = initialValue;
}
public override get(): T {
this._update();
return this._value;
}
private _update(): void {
if (this._isUpToDate) {
return;
}
this._isUpToDate = true;
if (this._deltas.length > 0) {
for (const change of this._deltas) {
getLogger()?.handleObservableUpdated(this, { change, didChange: true, oldValue: '(unknown)', newValue: this._value, hadValue: true });
for (const observer of this._observers) {
observer.handleChange(this, change);
}
}
this._deltas.length = 0;
} else {
getLogger()?.handleObservableUpdated(this, { change: undefined, didChange: true, oldValue: '(unknown)', newValue: this._value, hadValue: true });
for (const observer of this._observers) {
observer.handleChange(this, undefined);
}
}
}
private _updateCounter = 0;
private _beginUpdate(): void {
this._updateCounter++;
if (this._updateCounter === 1) {
for (const observer of this._observers) {
observer.beginUpdate(this);
}
}
}
private _endUpdate(): void {
this._updateCounter--;
if (this._updateCounter === 0) {
this._update();
// End update could change the observer list.
const observers = [...this._observers];
for (const r of observers) {
r.endUpdate(this);
}
}
}
public override addObserver(observer: IObserver): void {
const shouldCallBeginUpdate = !this._observers.has(observer) && this._updateCounter > 0;
super.addObserver(observer);
if (shouldCallBeginUpdate) {
observer.beginUpdate(this);
}
}
public override removeObserver(observer: IObserver): void {
const shouldCallEndUpdate = this._observers.has(observer) && this._updateCounter > 0;
super.removeObserver(observer);
if (shouldCallEndUpdate) {
// Calling end update after removing the observer makes sure endUpdate cannot be called twice here.
observer.endUpdate(this);
}
}
public set(value: T, tx: ITransaction | undefined, change: TChange): void {
if (change === undefined && this._equalityComparator(this._value, value)) {
return;
}
let _tx: TransactionImpl | undefined;
if (!tx) {
tx = _tx = new TransactionImpl(() => { }, () => `Setting ${this.debugName}`);
}
try {
this._isUpToDate = false;
this._setValue(value);
if (change !== undefined) {
this._deltas.push(change);
}
tx.updateObserver({
beginUpdate: () => this._beginUpdate(),
endUpdate: () => this._endUpdate(),
handleChange: (observable, change) => { },
handlePossibleChange: (observable) => { },
}, this);
if (this._updateCounter > 1) {
// We already started begin/end update, so we need to manually call handlePossibleChange
for (const observer of this._observers) {
observer.handlePossibleChange(this);
}
}
} finally {
if (_tx) {
_tx.finish();
}
}
}
override toString(): string {
return `${this.debugName}: ${this._value}`;
}
protected _setValue(newValue: T): void {
this._value = newValue;
}