Skip to content

Commit

Permalink
fix default refresh for .map\*
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Sep 25, 2023
1 parent c45e5df commit a53ffad
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-cats-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect-rx/rx": patch
---

fix default refresh for .map\*
24 changes: 10 additions & 14 deletions packages/rx/src/Rx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,10 @@ const WritableProto = {
[WritableTypeId]: WritableTypeId
} as const

function defaultRefresh(this: Rx<any>, f: any) {
f(this)
function defaultRefresh(rx: Rx<any>) {
return function(f: any) {
f(rx)
}
}

/**
Expand All @@ -244,12 +246,12 @@ export const isWritable = <R, W>(rx: Rx<R>): rx is Writable<R, W> => WritableTyp
*/
export const readable = <A>(
read: Rx.Read<A>,
refresh: Rx.Refresh = defaultRefresh
refresh?: Rx.Refresh
): Rx<A> => {
const rx = Object.create(RxProto)
rx.keepAlive = false
rx.read = read
rx.refresh = refresh
rx.refresh = refresh ?? defaultRefresh(rx)
return rx
}

Expand All @@ -260,13 +262,13 @@ export const readable = <A>(
export const writable = <R, W>(
read: Rx.Read<R>,
write: Rx.Write<R, W>,
refresh: Rx.Refresh = defaultRefresh
refresh?: Rx.Refresh
): Writable<R, W> => {
const rx = Object.create(WritableProto)
rx.keepAlive = false
rx.read = read
rx.write = write
rx.refresh = refresh
rx.refresh = refresh ?? defaultRefresh(rx)
return rx
}

Expand Down Expand Up @@ -799,14 +801,8 @@ export const map = dual<
2,
(<A, B>(self: Rx<A>, f: (_: A) => B): Rx<B> =>
isWritable(self)
? writable((get) => f(get(self)), function(ctx, value) {
ctx.set(self, value)
}, function(refresh) {
refresh(self)
})
: readable((get) => f(get(self)), function(refresh) {
refresh(self)
})) as any
? writable((get) => f(get(self)), self.write as any, self.refresh)
: readable((get) => f(get(self)), self.refresh)) as any
)

/**
Expand Down
38 changes: 38 additions & 0 deletions packages/rx/test/Rx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,44 @@ describe("Rx", () => {
expect(result.value).toEqual(3)
})

it("effect double mapResult", async () => {
const seed = Rx.state(0)
const count = Rx.effect((get) => Effect.succeed(get(seed) + 1)).pipe(
Rx.mapResult((_) => _ + 10),
Rx.mapResult((_) => _ + 100)
)
const r = Registry.make()
let result = r.get(count)
assert(Result.isSuccess(result))
expect(result.value).toEqual(111)
r.set(seed, 1)
result = r.get(count)
assert(Result.isSuccess(result))
expect(result.value).toEqual(112)
})

it("effect double mapResult refresh", async () => {
let rebuilds = 0
const count = Rx.effect(() => {
rebuilds++
return Effect.succeed(1)
}).pipe(
Rx.mapResult((_) => _ + 10),
Rx.mapResult((_) => _ + 100),
Rx.refreshable
)
const r = Registry.make()
let result = r.get(count)
assert(Result.isSuccess(result))
expect(result.value).toEqual(111)
expect(rebuilds).toEqual(1)
r.refresh(count)
result = r.get(count)
assert(Result.isSuccess(result))
expect(result.value).toEqual(111)
expect(rebuilds).toEqual(2)
})

it("scopedFn", async () => {
let finalized = 0
const count = Rx.scopedFn((n: number) =>
Expand Down

0 comments on commit a53ffad

Please sign in to comment.