Skip to content

Commit aedeb20

Browse files
committed
feat: add isEqual to ValueRef
1 parent 97a4ddc commit aedeb20

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed

src/DOM/LiveSelector.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -435,13 +435,6 @@ export class LiveSelector<T, SingleMode extends boolean = false> {
435435
if (this.isSingleMode) return (arr.filter(nonNull) as T[])[0] as any
436436
return (arr.filter(nonNull) as T[]) as any
437437
}
438-
/**
439-
* {@inheritdoc LiveSelector.evaluate}
440-
* @deprecated Use `evaluate()` instead, it's shorter. Will removed at 0.6.0
441-
*/
442-
evaluateOnce(): SingleMode extends true ? (T | undefined) : T[] {
443-
return this.evaluate()
444-
}
445438
//#endregion
446439
/**
447440
* Call this function to enhance the debug experience in the Chrome DevTools

src/util/ValueRef.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ describe('ValueRef', () => {
2727
ref.value = ref.value
2828
done()
2929
})
30+
it('isEqual should work', done => {
31+
const ref = new ValueRef({ a: 1 }, (a, b) => JSON.stringify(a) === JSON.stringify(b))
32+
ref.addListener(() => done('bad call'))
33+
ref.value = { a: 1 }
34+
done()
35+
})
3036
it('remove the listener', done => {
3137
const ref = new ValueRef(symb)
3238
const f = () => done('bad call')

src/util/ValueRef.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class ValueRef<T> {
2424
/** Set current value */
2525
set value(newVal: T) {
2626
const oldVal = this._value
27-
if (newVal === oldVal) return
27+
if (this.isEqual(newVal, oldVal)) return
2828
this._value = newVal
2929
for (const fn of this.watcher) {
3030
try {
@@ -36,7 +36,7 @@ export class ValueRef<T> {
3636
}
3737
/** All watchers */
3838
private watcher = new Set<(newVal: T, oldVal: T) => void>()
39-
constructor(private _value: T) {}
39+
constructor(private _value: T, private isEqual: (a: T, b: T) => boolean = (a, b) => a === b) {}
4040
/**
4141
* Add a listener. This will return a remover.
4242
* @example

0 commit comments

Comments
 (0)