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