Skip to content

Commit

Permalink
cache previous value in RxRef.prop
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Jun 5, 2024
1 parent 3e60476 commit 65974f0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tricky-lamps-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect-rx/rx": patch
---

cache previous value in RxRef.prop
10 changes: 9 additions & 1 deletion packages/rx/src/RxRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,10 @@ class MapRefImpl<A, B> implements ReadonlyRef<B> {
class PropRefImpl<A, K extends keyof A> implements RxRef<A[K]> {
readonly [TypeId]: TypeId
readonly key = keyState.generate()
private previous: A[K]
constructor(readonly parent: RxRef<A>, readonly _prop: K) {
this[TypeId] = TypeId
this.previous = parent.value[_prop]
}
[Equal.symbol](that: Equal.Equal) {
return Equal.equals(this.value, (that as ReadonlyRef<A>).value)
Expand All @@ -174,11 +176,17 @@ class PropRefImpl<A, K extends keyof A> implements RxRef<A[K]> {
return Hash.hash(this.value)
}
get value() {
return this.parent.value[this._prop]
if (this.parent.value && this._prop in (this.parent.value as any)) {
this.previous = this.parent.value[this._prop]
}
return this.previous
}
subscribe(f: (a: A[K]) => void): () => void {
let previous = this.value
return this.parent.subscribe((a) => {
if (!a || !(this._prop in (a as any))) {
return
}
const next = a[this._prop]
if (Equal.equals(next, previous)) {
return
Expand Down

0 comments on commit 65974f0

Please sign in to comment.