Skip to content

Commit

Permalink
add useRxUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Sep 19, 2023
1 parent ca6da96 commit b288bb4
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
22 changes: 22 additions & 0 deletions docs/rx-react/index.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ Added in v1.0.0
- [RegistryContext](#registrycontext)
- [hooks](#hooks)
- [useRx](#userx)
- [useRxUpdate](#userxupdate)
- [useRxValue](#userxvalue)
- [useSetRx](#usesetrx)
- [useUpdateRx](#useupdaterx)

---

Expand Down Expand Up @@ -45,6 +47,16 @@ export declare const useRx: <R, W>(rx: Rx.Writeable<R, W>) => readonly [R, (_: W
Added in v1.0.0
## useRxUpdate
**Signature**
```ts
export declare const useRxUpdate: <R, W>(rx: Rx.Writeable<R, W>) => readonly [R, (f: (_: R) => W) => void]
```
Added in v1.0.0
## useRxValue
**Signature**
Expand All @@ -64,3 +76,13 @@ export declare const useSetRx: <R, W>(rx: Rx.Writeable<R, W>) => (_: W) => void
```
Added in v1.0.0
## useUpdateRx
**Signature**
```ts
export declare const useUpdateRx: <R, W>(rx: Rx.Writeable<R, W>) => (f: (_: R) => W) => void
```
Added in v1.0.0
5 changes: 0 additions & 5 deletions packages/rx-react/src/Registry.ts

This file was deleted.

5 changes: 0 additions & 5 deletions packages/rx-react/src/Result.ts

This file was deleted.

5 changes: 0 additions & 5 deletions packages/rx-react/src/Rx.ts

This file was deleted.

32 changes: 28 additions & 4 deletions packages/rx-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import * as Registry from "@effect-rx/rx/Registry"
import type * as Rx from "@effect-rx/rx/Rx"
import * as React from "react"

export * as Registry from "@effect-rx/rx/Registry"
export * as Result from "@effect-rx/rx/Result"
export * as Rx from "@effect-rx/rx/Rx"

/**
* @since 1.0.0
* @category context
Expand Down Expand Up @@ -56,7 +60,27 @@ export const useSetRx = <R, W>(rx: Rx.Writeable<R, W>): (_: W) => void => {
* @since 1.0.0
* @category hooks
*/
export const useRx = <R, W>(rx: Rx.Writeable<R, W>): readonly [R, (_: W) => void] => [
useRxValue(rx),
useSetRx(rx)
]
export const useUpdateRx = <R, W>(rx: Rx.Writeable<R, W>): (f: (_: R) => W) => void => {
const registry = React.useContext(RegistryContext)
return React.useCallback((f) => registry.set(rx, f(registry.get(rx))), [registry, rx])
}

/**
* @since 1.0.0
* @category hooks
*/
export const useRx = <R, W>(rx: Rx.Writeable<R, W>): readonly [R, (_: W) => void] =>
[
useRxValue(rx),
useSetRx(rx)
] as const

/**
* @since 1.0.0
* @category hooks
*/
export const useRxUpdate = <R, W>(rx: Rx.Writeable<R, W>): readonly [R, (f: (_: R) => W) => void] =>
[
useRxValue(rx),
useUpdateRx(rx)
] as const

0 comments on commit b288bb4

Please sign in to comment.