Skip to content

Commit

Permalink
feat: add isEqual to ValueRef
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Oct 16, 2019
1 parent e28ff79 commit 5e52bc5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
7 changes: 0 additions & 7 deletions src/DOM/LiveSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,13 +435,6 @@ export class LiveSelector<T, SingleMode extends boolean = false> {
if (this.isSingleMode) return (arr.filter(nonNull) as T[])[0] as any
return (arr.filter(nonNull) as T[]) as any
}
/**
* {@inheritdoc LiveSelector.evaluate}
* @deprecated Use `evaluate()` instead, it's shorter. Will removed at 0.6.0
*/
evaluateOnce(): SingleMode extends true ? (T | undefined) : T[] {
return this.evaluate()
}
//#endregion
/**
* Call this function to enhance the debug experience in the Chrome DevTools
Expand Down
6 changes: 6 additions & 0 deletions src/util/ValueRef.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ describe('ValueRef', () => {
ref.value = ref.value
done()
})
it('isEqual should work', done => {
const ref = new ValueRef({ a: 1 }, (a, b) => JSON.stringify(a) === JSON.stringify(b))
ref.addListener(() => done('bad call'))
ref.value = { a: 1 }
done()
})
it('remove the listener', done => {
const ref = new ValueRef(symb)
const f = () => done('bad call')
Expand Down
4 changes: 2 additions & 2 deletions src/util/ValueRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class ValueRef<T> {
/** Set current value */
set value(newVal: T) {
const oldVal = this._value
if (newVal === oldVal) return
if (this.isEqual(newVal, oldVal)) return
this._value = newVal
for (const fn of this.watcher) {
try {
Expand All @@ -36,7 +36,7 @@ export class ValueRef<T> {
}
/** All watchers */
private watcher = new Set<(newVal: T, oldVal: T) => void>()
constructor(private _value: T) {}
constructor(private _value: T, private isEqual: (a: T, b: T) => boolean = (a, b) => a === b) {}
/**
* Add a listener. This will return a remover.
* @example
Expand Down

0 comments on commit 5e52bc5

Please sign in to comment.