Skip to content

Commit

Permalink
add function constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Sep 19, 2023
1 parent 9419ca3 commit d5e5e03
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 54 deletions.
6 changes: 6 additions & 0 deletions .changeset/long-falcons-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@effect-rx/rx-react": patch
"@effect-rx/rx": patch
---

add function constructors
25 changes: 8 additions & 17 deletions docs/rx-react/index.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ Added in v1.0.0
- [context](#context)
- [RegistryContext](#registrycontext)
- [hooks](#hooks)
- [useRefreshRx](#userefreshrx)
- [useRx](#userx)
- [useRxUpdate](#userxupdate)
- [useRxValue](#userxvalue)
- [useSetRx](#usesetrx)
- [useUpdateRx](#useupdaterx)

---

Expand All @@ -37,22 +36,24 @@ Added in v1.0.0
# hooks
## useRx
## useRefreshRx
**Signature**
```ts
export declare const useRx: <R, W>(rx: Rx.Writeable<R, W>) => readonly [R, (_: W) => void]
export declare const useRefreshRx: <A>(rx: Rx.Rx<A> & Rx.Refreshable) => () => void
```
Added in v1.0.0
## useRxUpdate
## useRx
**Signature**
```ts
export declare const useRxUpdate: <R, W>(rx: Rx.Writeable<R, W>) => readonly [R, (f: (_: R) => W) => void]
export declare const useRx: <R, W>(
rx: Rx.Writeable<R, W>
) => readonly [value: R, setOrUpdate: (_: W | ((_: R) => W)) => void]
```
Added in v1.0.0
Expand All @@ -72,17 +73,7 @@ Added in v1.0.0
**Signature**
```ts
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
export declare const useSetRx: <R, W>(rx: Rx.Writeable<R, W>) => (_: W | ((_: R) => W)) => void
```
Added in v1.0.0
1 change: 1 addition & 0 deletions docs/rx/Registry.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface Registry {
readonly refresh: Rx.Rx.Refresh
readonly set: Rx.Rx.Set
readonly subscribe: Rx.Rx.Subscribe
readonly subscribeGetter: Rx.Rx.SubscribeGetter
}
```

Expand Down
83 changes: 63 additions & 20 deletions docs/rx/Rx.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ Added in v1.0.0
- [refreshable](#refreshable)
- [constructors](#constructors)
- [effect](#effect)
- [effectFn](#effectfn)
- [fn](#fn)
- [readable](#readable)
- [runtime](#runtime)
- [scoped](#scoped)
- [scopedFn](#scopedfn)
- [state](#state)
- [writable](#writable)
- [context](#context)
Expand All @@ -37,12 +40,12 @@ Added in v1.0.0
- [Rx (namespace)](#rx-namespace)
- [Get (type alias)](#get-type-alias)
- [Mount (type alias)](#mount-type-alias)
- [Queue (type alias)](#queue-type-alias)
- [Refresh (type alias)](#refresh-type-alias)
- [Set (type alias)](#set-type-alias)
- [Subscribe (type alias)](#subscribe-type-alias)
- [SubscribeWithPrevious (type alias)](#subscribewithprevious-type-alias)
- [SubscribeGetter (type alias)](#subscribegetter-type-alias)
- [RxResult (interface)](#rxresult-interface)
- [RxResultFn (interface)](#rxresultfn-interface)
- [RxRuntime (interface)](#rxruntime-interface)
- [Writeable (interface)](#writeable-interface)
- [type ids](#type-ids)
Expand Down Expand Up @@ -117,6 +120,32 @@ export declare const effect: {
Added in v1.0.0
## effectFn
**Signature**
```ts
export declare const effectFn: {
<Args extends any[], E, A>(fn: (...args: Args) => Effect.Effect<RxContext, E, A>): RxResultFn<E, A, Args>
<Args extends any[], RR, R extends RxContext | RR, E, A, RE>(
fn: (...args: Args) => Effect.Effect<R, E, A>,
runtime: RxRuntime<RE, RR>
): RxResultFn<E | RE, A, Args>
}
```
Added in v1.0.0
## fn
**Signature**
```ts
export declare const fn: <A, Args extends any[]>(initialValue: A, fn: (...args: Args) => A) => Writeable<A, Args>
```
Added in v1.0.0
## readable
**Signature**
Expand Down Expand Up @@ -156,6 +185,26 @@ export declare const scoped: {
Added in v1.0.0
## scopedFn
**Signature**
```ts
export declare const scopedFn: {
<Args extends any[], E, A>(fn: (...args: Args) => Effect.Effect<RxContext | Scope.Scope, E, A>): RxResultFn<
E,
A,
Args
>
<Args extends any[], RR, R extends RxContext | Scope.Scope | RR, E, A, RE>(
fn: (...args: Args) => Effect.Effect<R, E, A>,
runtime: RxRuntime<RE, RR>
): RxResultFn<E | RE, A, Args>
}
```
Added in v1.0.0
## state
**Signature**
Expand Down Expand Up @@ -303,16 +352,6 @@ export type Mount = <A>(rx: Rx<A>) => () => void
Added in v1.0.0
### Queue (type alias)
**Signature**
```ts
export type Queue = <A>(rx: Rx<A>) => Effect.Effect<Scope.Scope, never, Queue_.Dequeue<A>>
```
Added in v1.0.0
### Refresh (type alias)
**Signature**
Expand Down Expand Up @@ -349,18 +388,12 @@ export type Subscribe = <A>(
Added in v1.0.0
### SubscribeWithPrevious (type alias)
### SubscribeGetter (type alias)
**Signature**
```ts
export type SubscribeWithPrevious = <A>(
rx: Rx<A>,
f: (prev: Option.Option<A>, value: A) => void,
options?: {
readonly immediate?: boolean
}
) => () => void
export type SubscribeGetter = <A>(rx: Rx<A>, f: () => void) => readonly [get: () => A, unmount: () => void]
```
Added in v1.0.0
Expand All @@ -375,6 +408,16 @@ export interface RxResult<E, A> extends Rx<Result.Result<E, A>> {}

Added in v1.0.0

## RxResultFn (interface)

**Signature**

```ts
export interface RxResultFn<E, A, Args extends Array<any>> extends Writeable<Result.Result<E, A>, Args> {}
```

Added in v1.0.0

## RxRuntime (interface)

**Signature**
Expand Down
29 changes: 14 additions & 15 deletions packages/rx-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,35 @@ export const useRxValue = <A>(rx: Rx.Rx<A>): A => {
* @since 1.0.0
* @category hooks
*/
export const useSetRx = <R, W>(rx: Rx.Writeable<R, W>): (_: W) => void => {
export const useSetRx = <R, W>(rx: Rx.Writeable<R, W>): (_: W | ((_: R) => W)) => void => {
const registry = React.useContext(RegistryContext)
return React.useCallback((value) => registry.set(rx, value), [registry, rx])
return React.useCallback((value) => {
if (typeof value === "function") {
registry.set(rx, (value as any)(registry.get(rx)))
return
} else {
registry.set(rx, value)
}
}, [registry, rx])
}

/**
* @since 1.0.0
* @category hooks
*/
export const useUpdateRx = <R, W>(rx: Rx.Writeable<R, W>): (f: (_: R) => W) => void => {
export const useRefreshRx = <A>(rx: Rx.Rx<A> & Rx.Refreshable): () => void => {
const registry = React.useContext(RegistryContext)
return React.useCallback((f) => registry.set(rx, f(registry.get(rx))), [registry, rx])
return React.useCallback(() => {
registry.refresh(rx)
}, [registry, rx])
}

/**
* @since 1.0.0
* @category hooks
*/
export const useRx = <R, W>(rx: Rx.Writeable<R, W>): readonly [R, (_: W) => void] =>
export const useRx = <R, W>(rx: Rx.Writeable<R, W>): readonly [value: R, setOrUpdate: (_: W | ((_: 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
88 changes: 86 additions & 2 deletions packages/rx/src/Rx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,11 @@ export const effect: {
): RxResult<RE | E, A>
} = <R, E, A, RE>(
effect: Effect.Effect<R, E, A>,
runtime?: RxRuntime<RE, Exclude<R, RxContext>>
runtime?: RxRuntime<RE, R>
) =>
readable<Result.Result<E, A>>(function(ctx) {
if (runtime !== undefined) {
return makeEffectRuntime(ctx, effect, runtime as any)
return makeEffectRuntime(ctx, effect, runtime)
}
return makeEffect(ctx, effect as Effect.Effect<RxContext, E, A>)
})
Expand Down Expand Up @@ -363,6 +363,90 @@ export const scoped: {
return makeEffect(ctx, scopedEffect)
})

/**
* @since 1.0.0
* @category models
*/
export interface RxResultFn<E, A, Args extends Array<any>> extends Writeable<Result.Result<E, A>, Args> {}

/**
* @since 1.0.0
* @category constructors
*/
export const fn = <A, Args extends Array<any>>(
initialValue: A,
fn: (...args: Args) => A
) =>
writable<A, Args>(function(_ctx) {
return initialValue
}, function(_get, _set, setSelf, args) {
setSelf(fn(...args))
})

/**
* @since 1.0.0
* @category constructors
*/
export const effectFn: {
<Args extends Array<any>, E, A>(fn: (...args: Args) => Effect.Effect<RxContext, E, A>): RxResultFn<E, A, Args>
<Args extends Array<any>, RR, R extends (RR | RxContext), E, A, RE>(
fn: (...args: Args) => Effect.Effect<R, E, A>,
runtime: RxRuntime<RE, RR>
): RxResultFn<RE | E, A, Args>
} = <Args extends Array<any>, R, E, A, RE>(
f: (...args: Args) => Effect.Effect<R, E, A>,
runtime?: RxRuntime<RE, R>
) => {
const effectRx = state<Effect.Effect<R, E, A> | undefined>(undefined)
return writable<Result.Result<E, A>, Args>(function(ctx) {
const effect = ctx.get(effectRx)
if (effect === undefined) {
return Result.initial()
}
return runtime ? makeEffectRuntime(ctx, effect, runtime) : makeEffect(ctx, effect as Effect.Effect<RxContext, E, A>)
}, function(_get, set, _setSelf, args) {
set(effectRx, f(...args))
})
}

/**
* @since 1.0.0
* @category constructors
*/
export const scopedFn: {
<Args extends Array<any>, E, A>(
fn: (...args: Args) => Effect.Effect<RxContext | Scope.Scope, E, A>
): RxResultFn<E, A, Args>
<Args extends Array<any>, RR, R extends (RR | RxContext | Scope.Scope), E, A, RE>(
fn: (...args: Args) => Effect.Effect<R, E, A>,
runtime: RxRuntime<RE, RR>
): RxResultFn<RE | E, A, Args>
} = <Args extends Array<any>, R, E, A, RE>(
f: (...args: Args) => Effect.Effect<R, E, A>,
runtime?: RxRuntime<RE, R>
) => {
const effectRx = state<Effect.Effect<R, E, A> | undefined>(undefined)
return writable<Result.Result<E, A>, Args>(function(ctx) {
const effect = ctx.get(effectRx)
if (effect === undefined) {
return Result.initial()
}
const scope = Effect.runSync(Scope.make())
ctx.addFinalizer(() => Effect.runFork(Scope.close(scope, Exit.unit)))

const scopedEffect = Effect.provideService(
effect as Effect.Effect<Scope.Scope | RxContext, E, A>,
Scope.Scope,
scope
)
return runtime
? makeEffectRuntime(ctx, scopedEffect, runtime)
: makeEffect(ctx, scopedEffect as Effect.Effect<RxContext, E, A>)
}, function(_get, set, _setSelf, args) {
set(effectRx, f(...args))
})
}

/**
* @since 1.0.0
* @category models
Expand Down
Loading

0 comments on commit d5e5e03

Please sign in to comment.