Skip to content

Commit

Permalink
cast undefined to void for .fn arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Mar 14, 2024
1 parent 4d30ce0 commit b40779d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-dodos-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect-rx/rx": patch
---

cast undefined to void for .fn arguments
31 changes: 25 additions & 6 deletions docs/rx/Rx.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ Added in v1.0.0
- [TypeId (type alias)](#typeid-type-alias)
- [WritableTypeId](#writabletypeid)
- [WritableTypeId (type alias)](#writabletypeid-type-alias)
- [utils](#utils)
- [RxResultFn (namespace)](#rxresultfn-namespace)
- [ArgToVoid (type alias)](#argtovoid-type-alias)

---

Expand Down Expand Up @@ -243,11 +246,11 @@ export declare const fn: {
<Arg, E, A>(
fn: Rx.ReadFn<Arg, Effect.Effect<A, E, Scope.Scope>>,
options?: { readonly initialValue?: A | undefined } | undefined
): RxResultFn<Types.Equals<Arg, unknown> extends true ? void : Arg, A, E>
): RxResultFn<RxResultFn.ArgToVoid<Arg>, A, E>
<Arg, E, A>(
fn: Rx.ReadFn<Arg, Stream.Stream<A, E, never>>,
options?: { readonly initialValue?: A | undefined } | undefined
): RxResultFn<Types.Equals<Arg, unknown> extends true ? void : Arg, A, E | NoSuchElementException>
): RxResultFn<RxResultFn.ArgToVoid<Arg>, A, E | NoSuchElementException>
}
```
Expand All @@ -259,8 +262,8 @@ Added in v1.0.0
```ts
export declare const fnSync: {
<Arg, A>(f: Rx.ReadFn<Arg, A>): Writable<Option.Option<A>, Arg>
<Arg, A>(f: Rx.ReadFn<Arg, A>, options: { readonly initialValue: A }): Writable<A, Arg>
<Arg, A>(f: Rx.ReadFn<Arg, A>): Writable<Option.Option<A>, RxResultFn.ArgToVoid<Arg>>
<Arg, A>(f: Rx.ReadFn<Arg, A>, options: { readonly initialValue: A }): Writable<A, RxResultFn.ArgToVoid<Arg>>
}
```
Expand Down Expand Up @@ -661,13 +664,13 @@ export interface RxRuntime<R, ER> extends Rx<Result.Result<Runtime.Runtime<R>, E
options?: {
readonly initialValue?: A
}
): RxResultFn<Types.Equals<Arg, unknown> extends true ? void : Arg, A, E | ER>
): RxResultFn<RxResultFn.ArgToVoid<Arg>, A, E | ER>
<Arg, E, A>(
fn: Rx.ReadFn<Arg, Stream.Stream<A, E, R>>,
options?: {
readonly initialValue?: A
}
): RxResultFn<Types.Equals<Arg, unknown> extends true ? void : Arg, A, E | ER | NoSuchElementException>
): RxResultFn<RxResultFn.ArgToVoid<Arg>, A, E | ER | NoSuchElementException>
}

readonly pull: <A, E>(
Expand Down Expand Up @@ -796,3 +799,19 @@ export type WritableTypeId = typeof WritableTypeId
```
Added in v1.0.0
# utils
## RxResultFn (namespace)
Added in v1.0.0
### ArgToVoid (type alias)
**Signature**
```ts
export type ArgToVoid<Arg> = Arg extends infer A ? (unknown extends A ? void : A extends undefined ? void : A) : never
```
Added in v1.0.0
29 changes: 19 additions & 10 deletions packages/rx/src/Rx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import * as Runtime from "effect/Runtime"
import * as Scope from "effect/Scope"
import * as Stream from "effect/Stream"
import * as SubscriptionRef from "effect/SubscriptionRef"
import type * as Types from "effect/Types"
import * as internalRegistry from "./internal/registry.js"
import { runCallbackSync } from "./internal/runtime.js"
import * as Result from "./Result.js"
Expand Down Expand Up @@ -521,10 +520,10 @@ export interface RxRuntime<R, ER> extends Rx<Result.Result<Runtime.Runtime<R>, E
readonly fn: {
<Arg, E, A>(fn: Rx.ReadFn<Arg, Effect.Effect<A, E, Scope.Scope | R>>, options?: {
readonly initialValue?: A
}): RxResultFn<Types.Equals<Arg, unknown> extends true ? void : Arg, A, E | ER>
}): RxResultFn<RxResultFn.ArgToVoid<Arg>, A, E | ER>
<Arg, E, A>(fn: Rx.ReadFn<Arg, Stream.Stream<A, E, R>>, options?: {
readonly initialValue?: A
}): RxResultFn<Types.Equals<Arg, unknown> extends true ? void : Arg, A, E | ER | NoSuchElementException>
}): RxResultFn<RxResultFn.ArgToVoid<Arg>, A, E | ER | NoSuchElementException>
}

readonly pull: <A, E>(create: Rx.Read<Stream.Stream<A, E, R>> | Stream.Stream<A, E, R>, options?: {
Expand Down Expand Up @@ -711,14 +710,14 @@ export const subRef: {
export const fnSync: {
<Arg, A>(
f: Rx.ReadFn<Arg, A>
): Writable<Option.Option<A>, Arg>
): Writable<Option.Option<A>, RxResultFn.ArgToVoid<Arg>>
<Arg, A>(
f: Rx.ReadFn<Arg, A>,
options: { readonly initialValue: A }
): Writable<A, Arg>
): Writable<A, RxResultFn.ArgToVoid<Arg>>
} = <Arg, A>(f: Rx.ReadFn<Arg, A>, options?: {
readonly initialValue?: A
}): Writable<Option.Option<A> | A, Arg> => {
}): Writable<Option.Option<A> | A, RxResultFn.ArgToVoid<Arg>> => {
const argRx = state<[number, Arg]>([0, undefined as any])
const hasInitialValue = options?.initialValue !== undefined
return writable(function(get) {
Expand All @@ -728,7 +727,7 @@ export const fnSync: {
}
return hasInitialValue ? f(arg, get) : Option.some(f(arg, get))
}, function(ctx, arg) {
ctx.set(argRx, [ctx.get(argRx)[0] + 1, arg])
ctx.set(argRx, [ctx.get(argRx)[0] + 1, arg as Arg])
})
}

Expand All @@ -738,6 +737,16 @@ export const fnSync: {
*/
export interface RxResultFn<Arg, A, E = never> extends Writable<Result.Result<A, E>, Arg | Reset> {}

/**
* @since 1.0.0
*/
export declare namespace RxResultFn {
/**
* @since 1.0.0
*/
export type ArgToVoid<Arg> = Arg extends infer A ? unknown extends A ? void : A extends undefined ? void : A : never
}

/**
* @since 1.0.0
* @category symbols
Expand All @@ -757,13 +766,13 @@ export type Reset = typeof Reset
export const fn: {
<Arg, E, A>(fn: Rx.ReadFn<Arg, Effect.Effect<A, E, Scope.Scope>>, options?: {
readonly initialValue?: A
}): RxResultFn<Types.Equals<Arg, unknown> extends true ? void : Arg, A, E>
}): RxResultFn<RxResultFn.ArgToVoid<Arg>, A, E>
<Arg, E, A>(fn: Rx.ReadFn<Arg, Stream.Stream<A, E>>, options?: {
readonly initialValue?: A
}): RxResultFn<Types.Equals<Arg, unknown> extends true ? void : Arg, A, E | NoSuchElementException>
}): RxResultFn<RxResultFn.ArgToVoid<Arg>, A, E | NoSuchElementException>
} = <Arg, E, A>(f: Rx.ReadFn<Arg, Stream.Stream<A, E> | Effect.Effect<A, E, Scope.Scope>>, options?: {
readonly initialValue?: A
}): RxResultFn<Types.Equals<Arg, unknown> extends true ? void : Arg, A, E | NoSuchElementException> => {
}): RxResultFn<RxResultFn.ArgToVoid<Arg>, A, E | NoSuchElementException> => {
const [read, write] = makeResultFn(f, options)
return writable(read, write) as any
}
Expand Down

0 comments on commit b40779d

Please sign in to comment.