Skip to content

Commit

Permalink
observableValue: Emit change signal if value is unchanged, but a delt…
Browse files Browse the repository at this point in the history
…a value was provided. (#212075)
  • Loading branch information
hediet committed May 6, 2024
1 parent ccba2fc commit 3af934a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/vs/base/common/observableInternal/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ export class ObservableValue<T, TChange = void>
}

public set(value: T, tx: ITransaction | undefined, change: TChange): void {
if (this._equalityComparator(this._value, value)) {
if (change === undefined && this._equalityComparator(this._value, value)) {
return;
}

Expand Down
32 changes: 32 additions & 0 deletions src/vs/base/test/common/observable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,38 @@ suite('observables', () => {
]);
});
});

test('observableValue', () => {
const log = new Log();
const myObservable1 = observableValue<number>('myObservable1', 0);
const myObservable2 = observableValue<number, { message: string }>('myObservable2', 0);

const d = autorun(reader => {
/** @description update */
const v1 = myObservable1.read(reader);
const v2 = myObservable2.read(reader);
log.log('autorun, myObservable1:' + v1 + ', myObservable2:' + v2);
});

assert.deepStrictEqual(log.getAndClearEntries(), [
'autorun, myObservable1:0, myObservable2:0'
]);

// Doesn't trigger the autorun, because no delta was provided and the value did not change
myObservable1.set(0, undefined);

assert.deepStrictEqual(log.getAndClearEntries(), [
]);

// Triggers the autorun. The value did not change, but a delta value was provided
myObservable2.set(0, undefined, { message: 'change1' });

assert.deepStrictEqual(log.getAndClearEntries(), [
'autorun, myObservable1:0, myObservable2:0'
]);

d.dispose();
});
});

export class LoggingObserver implements IObserver {
Expand Down

0 comments on commit 3af934a

Please sign in to comment.