namespace / @typed/fx/Fx

fn

Callable contracts implemented by fn.

Package version
2.0.0-beta.13
Category
Generator composition
Since
1.0.0

Follow one execution

Invoke the function to construct an Fx; its generator Effects run only when that Fx is subscribed, then its returned Fx runs.

Fx timelinefn(function* (id) { ... })fn
Operatorfn(function* (id) { ... })
Read this diagram

Follow each lane from left to right. Events stacked vertically share a tick; the green cursor marks the current time across every lane.

  • A value
    The text inside the pill is the emitted value.
  • Work starts
    The raised chevron starts an inner run (^ in the source).
  • The run returns
    The vertical bar ends this lane’s run.
  • A cause is delivered
    The exclamation mark belongs to this lane.
  • Work is interrupted
    The cross marks cancellation of this run.
  • Current time
    The line and diamond move together across all lanes.
  • Happening now
    A highlighted event is at the current tick.
  • Still ahead
    Muted, dashed values have not happened yet.
  • Time continues
    The lane’s arrow is not a return marker. An empty stretch can be quiet work that is still running.

Illustrated ticks start at 0. At 1×, one illustrated tick takes one second; captions specify real durations when timing matters. A cause or interruption belongs to its lane, and other work may continue. Scroll horizontally to inspect the rest of a long timeline.

Creating the function and invoking it start no stream. For generator bodies, each run evaluates the body and yielded Effects, then runs the returned Fx in the same structured lifetime. Interruption stops both acquisition and production. Direct bodies are evaluated lazily through the same unwrap boundary.

Source implementation · Learn the surrounding model

Compare all Fx timelines →

Import

import { fn } from "@typed/fx/Fx";

Signatures

export declare namespace fn {
    type Gen = {
        <Eff extends Effect.Effect<any, any, any>, ReturnFx extends Fx.Any, Args extends Array<any>>(body: (this: unassigned, ...args: Args) => Generator<Eff, ReturnFx>): (...args: Args) => Fx<Fx.Success<ReturnFx>, Fx.Error<ReturnFx> | Effect.Error<Eff>, Fx.Services<ReturnFx> | Effect.Services<Eff>>;
        <Self, Eff extends Effect.Effect<any, any, any>, ReturnFx extends Fx.Any, Args extends Array<any>>(body: (this: Self, ...args: Args) => Generator<Eff, ReturnFx>): (this: Self, ...args: Args) => Fx<Fx.Success<ReturnFx>, Fx.Error<ReturnFx> | Effect.Error<Eff>, Fx.Services<ReturnFx> | Effect.Services<Eff>>;
        <Eff extends Effect.Effect<any, any, any>, ReturnFx extends Fx.Any, Args extends Array<any>, A>(body: (this: unassigned, ...args: Args) => Generator<Eff, ReturnFx>, a: (_: Fx<Fx.Success<ReturnFx>, Fx.Error<ReturnFx> | Effect.Error<Eff>, Fx.Services<ReturnFx> | Effect.Services<Eff>>, ...args: Args) => A): (...args: Args) => A;
        <Self, Eff extends Effect.Effect<any, any, any>, ReturnFx extends Fx.Any, Args extends Array<any>, A>(body: (this: Self, ...args: Args) => Generator<Eff, ReturnFx>, a: (_: Fx<Fx.Success<ReturnFx>, Fx.Error<ReturnFx> | Effect.Error<Eff>, Fx.Services<ReturnFx> | Effect.Services<Eff>>, ...args: Args) => A): (this: Self, ...args: Args) => A;
        <Eff extends Effect.Effect<any, any, any>, ReturnFx extends Fx.Any, Args extends Array<any>, A, B>(body: (this: unassigned, ...args: Args) => Generator<Eff, ReturnFx>, a: (_: Fx<Fx.Success<ReturnFx>, Fx.Error<ReturnFx> | Effect.Error<Eff>, Fx.Services<ReturnFx> | Effect.Services<Eff>>, ...args: Args) => A, b: (_: A, ...args: Args) => B): (...args: Args) => B;
        <Self, Eff extends Effect.Effect<any, any, any>, ReturnFx extends Fx.Any, Args extends Array<any>, A, B>(body: (this: Self, ...args: Args) => Generator<Eff, ReturnFx>, a: (_: Fx<Fx.Success<ReturnFx>, Fx.Error<ReturnFx> | Effect.Error<Eff>, Fx.Services<ReturnFx> | Effect.Services<Eff>>, ...args: Args) => A, b: (_: A, ...args: Args) => B): (this: Self, ...args: Args) => B;
        <Eff extends Effect.Effect<any, any, any>, ReturnFx extends Fx.Any, Args extends Array<any>, A, B, C>(body: (this: unassigned, ...args: Args) => Generator<Eff, ReturnFx>, a: (_: Fx<Fx.Success<ReturnFx>, Fx.Error<ReturnFx> | Effect.Error<Eff>, Fx.Services<ReturnFx> | Effect.Services<Eff>>, ...args: Args) => A, b: (_: A, ...args: Args) => B, c: (_: B, ...args: Args) => C): (...args: Args) => C;
        <Self, Eff extends Effect.Effect<any, any, any>, ReturnFx extends Fx.Any, Args extends Array<any>, A, B, C>(body: (this: Self, ...args: Args) => Generator<Eff, ReturnFx>, a: (_: Fx<Fx.Success<ReturnFx>, Fx.Error<ReturnFx> | Effect.Error<Eff>, Fx.Services<ReturnFx> | Effect.Services<Eff>>, ...args: Args) => A, b: (_: A, ...args: Args) => B, c: (_: B, ...args: Args) => C): (this: Self, ...args: Args) => C;
    };
    type NonGen = {
        <Args extends Array<any>, ReturnFx extends Fx.Any>(body: (this: unassigned, ...args: Args) => ReturnFx): (...args: Args) => ReturnFx;
        <Self, Args extends Array<any>, ReturnFx extends Fx.Any>(body: (this: Self, ...args: Args) => ReturnFx): (this: Self, ...args: Args) => ReturnFx;
        <Args extends Array<any>, ReturnFx extends Fx.Any, A>(body: (this: unassigned, ...args: Args) => ReturnFx, a: (_: ReturnFx, ...args: Args) => A): (...args: Args) => A;
        <Self, Args extends Array<any>, ReturnFx extends Fx.Any, A>(body: (this: Self, ...args: Args) => ReturnFx, a: (_: ReturnFx, ...args: Args) => A): (this: Self, ...args: Args) => A;
        <Args extends Array<any>, ReturnFx extends Fx.Any, A, B>(body: (this: unassigned, ...args: Args) => ReturnFx, a: (_: ReturnFx, ...args: Args) => A, b: (_: A, ...args: Args) => B): (...args: Args) => B;
        <Self, Args extends Array<any>, ReturnFx extends Fx.Any, A, B>(body: (this: Self, ...args: Args) => ReturnFx, a: (_: ReturnFx, ...args: Args) => A, b: (_: A, ...args: Args) => B): (this: Self, ...args: Args) => B;
        <Args extends Array<any>, ReturnFx extends Fx.Any, A, B, C>(body: (this: unassigned, ...args: Args) => ReturnFx, a: (_: ReturnFx, ...args: Args) => A, b: (_: A, ...args: Args) => B, c: (_: B, ...args: Args) => C): (...args: Args) => C;
        <Self, Args extends Array<any>, ReturnFx extends Fx.Any, A, B, C>(body: (this: Self, ...args: Args) => ReturnFx, a: (_: ReturnFx, ...args: Args) => A, b: (_: A, ...args: Args) => B, c: (_: B, ...args: Args) => C): (this: Self, ...args: Args) => C;
    };
}
export declare const fn: fn.Gen & fn.NonGen & {
    (name: string, options?: SpanOptionsNoTrace): fn.Gen & fn.NonGen;
};

Why

The namespace keeps generator and non-generator overload families addressable even though one callable value implements both.

Ownership and lifetime

These are type-level contracts. They start no Effect or Fx run.

Examples

import { Effect } from "effect"
import { Fx } from "@typed/fx"

const request = Fx.fn("request")(
  function* (id: number) {
    const token = yield* Effect.succeed("token")
    return Fx.succeed({ id, token })
  },
  (source) => Fx.map(source, ({ id }) => id)
)
const program = Fx.collectAll(request(42))

Members

  • fn.Gen

    Contract for functions whose body yields Effects and returns an Fx.

  • fn.NonGen

    Contract for functions whose body returns an Fx directly.

Other public imports

These import paths expose the same declaration. Each page retains its own public name and signature.

Source