Skip to content

Commit

Permalink
feat: add missing operators
Browse files Browse the repository at this point in the history
  • Loading branch information
TylorS committed Feb 29, 2024
1 parent 2a84e8b commit 5393bd7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/forty-feet-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@typed/fx": patch
---

Add some missing operators
70 changes: 68 additions & 2 deletions packages/fx/src/Fx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,24 @@ export type MatchErrorOptions<E, A, B, E2, R2, C, E3, R3> = {
readonly executionStrategy?: ExecutionStrategy.ExecutionStrategy | undefined
}

/**
* @since 1.20.0
*/
export type MatchCauseOptionsEffect<E, A, B, E2, R2, C, E3, R3> = {
readonly onFailure: (cause: Cause.Cause<E>) => Effect.Effect<B, E2, R2>
readonly onSuccess: (a: A) => Effect.Effect<C, E3, R3>
readonly executionStrategy?: ExecutionStrategy.ExecutionStrategy | undefined
}

/**
* @since 1.20.0
*/
export type MatchErrorOptionsEffect<E, A, B, E2, R2, C, E3, R3> = {
readonly onFailure: (e: E) => Effect.Effect<B, E2, R2>
readonly onSuccess: (a: A) => Effect.Effect<C, E3, R3>
readonly executionStrategy?: ExecutionStrategy.ExecutionStrategy | undefined
}

/**
* @since 1.20.0
*/
Expand Down Expand Up @@ -1809,7 +1827,7 @@ export const matchError: {
): <R>(fx: Fx<A, E, R>) => Fx<B | C, E2 | E3, R | R2 | R3 | Scope.Scope>
<A, E, R, B, E2, R2, C, E3, R3>(
fx: Fx<A, E, R>,
opts: core.MatchErrorOptions<E, A, B, E2, R2, C, E3, R3>
opts: MatchErrorOptions<E, A, B, E2, R2, C, E3, R3>
): Fx<B | C, E2 | E3, R | R2 | R3 | Scope.Scope>
} = dual(2, core.matchError)

Expand Down Expand Up @@ -1857,6 +1875,23 @@ export const switchMatchCause: {
): Fx<B | C, E2 | E3, Scope.Scope | R | R2 | R3>
} = dual(2, core.switchMatchCause)

/**
* @since 2.0.0
*/
export const switchMatchCauseEffect: {
<E, A, B, E2, R2, C, E3, R3>(
opts: MatchCauseOptionsEffect<E, A, B, E2, R2, C, E3, R3>
): <R>(fx: Fx<A, E, R>) => Fx<B | C, E2 | E3, R | R2 | R3 | Scope.Scope>
<A, E, R, B, E2, R2, C, E3, R3>(
fx: Fx<A, E, R>,
opts: MatchCauseOptionsEffect<E, A, B, E2, R2, C, E3, R3>
): Fx<B | C, E2 | E3, R | R2 | R3 | Scope.Scope>
} = dual(2, (fx, opts) =>
switchMatchCause(fx, {
onFailure: (e) => core.fromEffect(opts.onFailure(e)),
onSuccess: (a) => core.fromEffect(opts.onSuccess(a))
}))

/**
* @since 1.20.0
*/
Expand All @@ -1866,10 +1901,27 @@ export const switchMatchError: {
): <R>(fx: Fx<A, E, R>) => Fx<B | C, E2 | E3, R | R2 | R3 | Scope.Scope>
<A, E, R, B, E2, R2, C, E3, R3>(
fx: Fx<A, E, R>,
opts: core.MatchErrorOptions<E, A, B, E2, R2, C, E3, R3>
opts: MatchErrorOptions<E, A, B, E2, R2, C, E3, R3>
): Fx<B | C, E2 | E3, R | R2 | R3 | Scope.Scope>
} = dual(2, core.switchMatchError)

/**
* @since 2.0.0
*/
export const switchMatchErrorEffect: {
<E, A, B, E2, R2, C, E3, R3>(
opts: MatchErrorOptionsEffect<E, A, B, E2, R2, C, E3, R3>
): <R>(fx: Fx<A, E, R>) => Fx<B | C, E2 | E3, R | R2 | R3 | Scope.Scope>
<A, E, R, B, E2, R2, C, E3, R3>(
fx: Fx<A, E, R>,
opts: MatchErrorOptionsEffect<E, A, B, E2, R2, C, E3, R3>
): Fx<B | C, E2 | E3, R | R2 | R3 | Scope.Scope>
} = dual(2, (fx, opts) =>
switchMatchError(fx, {
onFailure: (e) => core.fromEffect(opts.onFailure(e)),
onSuccess: (a) => core.fromEffect(opts.onSuccess(a))
}))

/**
* @since 1.20.0
*/
Expand Down Expand Up @@ -2500,3 +2552,17 @@ export abstract class FxEffectBase<A, E, R, B, E2, R2> extends protos.FxEffectBa
*/
abstract toEffect(): Effect.Effect<B, E2, R2>
}

/**
* @since 2.0.0
*/
export function promise<A>(f: (signal: AbortSignal) => Promise<A>) {
return fromEffect(Effect.promise(f))
}

/**
* @since 2.0.0
*/
export function promiseFx<A, E = never, R = never>(f: (signal: AbortSignal) => Promise<Fx<A, E, R>>) {
return fromFxEffect(Effect.promise(f))
}

0 comments on commit 5393bd7

Please sign in to comment.