Skip to content

Commit

Permalink
support arrays in RxRef.prop
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Jun 1, 2024
1 parent 4675e2e commit 919339f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-points-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect-rx/rx": patch
---

support arrays in RxRef.prop
28 changes: 20 additions & 8 deletions packages/rx/src/RxRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,29 @@ class PropRefImpl<A, K extends keyof A> implements RxRef<A[K]> {
return new PropRefImpl(this, prop)
}
set(value: A[K]): RxRef<A[K]> {
this.parent.set({
...this.parent.value,
[this._prop]: value
})
if (Array.isArray(this.parent.value)) {
const newArray = this.parent.value.slice()
newArray[this._prop as number] = value
this.parent.set(newArray as A)
} else {
this.parent.set({
...this.parent.value,
[this._prop]: value
})
}
return this
}
update(f: (value: A[K]) => A[K]): RxRef<A[K]> {
this.parent.set({
...this.parent.value,
[this._prop]: f(this.parent.value[this._prop])
})
if (Array.isArray(this.parent.value)) {
const newArray = this.parent.value.slice()
newArray[this._prop as number] = f(this.parent.value[this._prop])
this.parent.set(newArray as A)
} else {
this.parent.set({
...this.parent.value,
[this._prop]: f(this.parent.value[this._prop])
})
}
return this
}
}
Expand Down
62 changes: 46 additions & 16 deletions packages/rx/test/RxRef.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,60 @@ describe("RxRef", () => {
ref.set(0)
assert.deepEqual(updates, [-1, -2, -3])
})
})

test("prop", () => {
const parent = RxRef.make({
a: 0,
b: 1,
c: 2
})
const ref = parent.prop("a")
test("prop", () => {
const parent = RxRef.make({
a: 0,
b: 1,
c: 2
})
const ref = parent.prop("a")

const updates: Array<number> = []
const cancel = ref.subscribe((_) => updates.push(_))
const updates: Array<number> = []
const cancel = ref.subscribe((_) => updates.push(_))

parent.update((_) => ({ ..._, a: -1 }))
parent.update((_) => ({ ..._, b: -2 }))
parent.update((_) => ({ ..._, a: -1 }))
parent.update((_) => ({ ..._, b: -2 }))

assert.deepEqual(updates, [-1])
assert.deepEqual(updates, [-1])

ref.set(0)
ref.set(0)

assert.deepEqual(updates, [-1, 0])
assert.deepEqual(parent.value, { a: 0, b: -2, c: 2 })
assert.deepEqual(updates, [-1, 0])
assert.deepEqual(parent.value, { a: 0, b: -2, c: 2 })

cancel()
cancel()
})

test("prop nested array", () => {
const parent = RxRef.make({
a: 0,
b: 1,
c: {
d: [{
e: 2
}]
}
})
const ref = parent.prop("c").prop("d").prop(0).prop("e")

const updates: Array<any> = []
const cancel = parent.subscribe((_) => updates.push(_))

ref.set(3)

assert.deepStrictEqual(updates, [{
a: 0,
b: 1,
c: {
d: [{
e: 3
}]
}
}])

cancel()
})

describe("collection", () => {
Expand Down

0 comments on commit 919339f

Please sign in to comment.