diff --git a/.changeset/tricky-lamps-appear.md b/.changeset/tricky-lamps-appear.md new file mode 100644 index 0000000..af60fb5 --- /dev/null +++ b/.changeset/tricky-lamps-appear.md @@ -0,0 +1,5 @@ +--- +"@effect-rx/rx": patch +--- + +cache previous value in RxRef.prop diff --git a/packages/rx/src/RxRef.ts b/packages/rx/src/RxRef.ts index 6a5160d..87672e6 100644 --- a/packages/rx/src/RxRef.ts +++ b/packages/rx/src/RxRef.ts @@ -164,8 +164,10 @@ class MapRefImpl implements ReadonlyRef { class PropRefImpl implements RxRef { readonly [TypeId]: TypeId readonly key = keyState.generate() + private previous: A[K] constructor(readonly parent: RxRef, readonly _prop: K) { this[TypeId] = TypeId + this.previous = parent.value[_prop] } [Equal.symbol](that: Equal.Equal) { return Equal.equals(this.value, (that as ReadonlyRef).value) @@ -174,11 +176,17 @@ class PropRefImpl implements RxRef { 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