Skip to content

Fiber

55 exports Added in v2.0.0 Source

Alternatives

orElse

Added in v2.0.0 Source

Returns a fiber that prefers this fiber, but falls back to the that one when this one fails. Interrupting the returned fiber will interrupt both fibers, sequentially, from left to right.

Signature

declare const orElse: {
  <A2, E2>(that: Fiber<A2, E2>): <A, E>(self: Fiber<A, E>) => Fiber<A2 | A, E2 | E>;
  <A, E, A2, E2>(self: Fiber<A, E>, that: Fiber<A2, E2>): Fiber<A | A2, E | E2>;
};

orElseEither

Added in v2.0.0 Source

Returns a fiber that prefers this fiber, but falls back to the that one when this one fails. Interrupting the returned fiber will interrupt both fibers, sequentially, from left to right.

Signature

declare const orElseEither: {
  <A2, E2>(that: Fiber<A2, E2>): <A, E>(self: Fiber<A, E>) => Fiber<Either<A2, A>, E2 | E>;
  <A, E, A2, E2>(self: Fiber<A, E>, that: Fiber<A2, E2>): Fiber<Either<A2, A>, E | E2>;
};

Constructors

all

Added in v2.0.0 Source

Collects all fibers into a single fiber producing an in-order list of the results.

Signature

declare const all: <A, E>(fibers: Iterable<Fiber<A, E>>) => Fiber<ReadonlyArray<A>, E>;

done

Added in v2.0.0 Source

A fiber that is done with the specified Exit value.

Signature

declare const done: <A, E>(exit: Exit.Exit<A, E>) => Fiber<A, E>;

fail

Added in v2.0.0 Source

A fiber that has already failed with the specified value.

Signature

declare const fail: <E>(error: E) => Fiber<never, E>;

failCause

Added in v2.0.0 Source

Creates a Fiber that has already failed with the specified cause.

Signature

declare const failCause: <E>(cause: Cause.Cause<E>) => Fiber<never, E>;

interrupted

Added in v2.0.0 Source

Constructrs a Fiber that is already interrupted.

Signature

declare const interrupted: (fiberId: FiberId.FiberId) => Fiber<never>;

never

Added in v2.0.0 Source

A fiber that never fails or succeeds.

Signature

declare const never: Fiber<never>;

roots

Added in v2.0.0 Source

Returns a chunk containing all root fibers.

Signature

declare const roots: Effect.Effect<Array<RuntimeFiber<any, any>>>;

succeed

Added in v2.0.0 Source

Returns a fiber that has already succeeded with the specified value.

Signature

declare const succeed: <A>(value: A) => Fiber<A>;

unsafeRoots

Added in v2.0.0 Source

Returns a chunk containing all root fibers.

Signature

declare const unsafeRoots: (_: void) => Array<RuntimeFiber<any, any>>;

Conversions

fromEffect

Added in v2.0.0 Source

Lifts an Effect into a Fiber.

Signature

declare const fromEffect: <A, E>(effect: Effect.Effect<A, E>) => Effect.Effect<Fiber<A, E>>;

Destructors

awaitAll

Added in v2.0.0 Source

Awaits on all fibers to be completed, successfully or not.

Signature

declare const awaitAll: <T extends Iterable<Fiber<any, any>>>(
  fibers: T,
) => Effect.Effect<
  [T] extends [ReadonlyArray<infer U>]
    ? number extends T["length"]
      ? Array<U extends Fiber<infer A, infer E> ? Exit.Exit<A, E> : never>
      : { [K in keyof T]: T[K] extends Fiber<infer A, infer E> ? Exit.Exit<A, E> : never }
    : Array<
        T extends Iterable<infer U>
          ? U extends Fiber<infer A, infer E>
            ? Exit.Exit<A, E>
            : never
          : never
      >
>;

dump

Added in v2.0.0 Source

Signature

declare const dump: <A, E>(self: RuntimeFiber<A, E>) => Effect.Effect<Fiber.Dump>;

dumpAll

Added in v2.0.0 Source

Signature

declare const dumpAll: (
  fibers: Iterable<RuntimeFiber<unknown, unknown>>,
) => Effect.Effect<Array<Fiber.Dump>>;

inheritAll

Added in v2.0.0 Source

Inherits values from all FiberRef instances into current fiber. This will resume immediately.

Signature

declare const inheritAll: <A, E>(self: Fiber<A, E>) => Effect.Effect<void>;

join

Added in v2.0.0 Source

Joins the fiber, which suspends the joining fiber until the result of the fiber has been determined. Attempting to join a fiber that has erred will result in a catchable error. Joining an interrupted fiber will result in an "inner interruption" of this fiber, unlike interruption triggered by another fiber, "inner interruption" can be caught and recovered.

Signature

declare const join: <A, E>(self: Fiber<A, E>) => Effect.Effect<A, E>;

joinAll

Added in v2.0.0 Source

Joins all fibers, awaiting their _successful_ completion. Attempting to join a fiber that has erred will result in a catchable error, _if_ that error does not result from interruption.

Signature

declare const joinAll: <A, E>(fibers: Iterable<Fiber<A, E>>) => Effect.Effect<Array<A>, E>;

pretty

Added in v2.0.0 Source

Pretty-prints a RuntimeFiber.

Signature

declare const pretty: <A, E>(self: RuntimeFiber<A, E>) => Effect.Effect<string>;

scoped

Added in v2.0.0 Source

Converts this fiber into a scoped effect. The fiber is interrupted when the scope is closed.

Signature

declare const scoped: <A, E>(self: Fiber<A, E>) => Effect.Effect<Fiber<A, E>, never, Scope.Scope>;

Folding

match

Added in v2.0.0 Source

Folds over the Fiber or RuntimeFiber.

Signature

declare const match: {
  <A, E, Z>(options: {
    readonly onFiber: (fiber: Fiber<A, E>) => Z;
    readonly onRuntimeFiber: (fiber: RuntimeFiber<A, E>) => Z;
  }): (self: Fiber<A, E>) => Z;
  <A, E, Z>(
    self: Fiber<A, E>,
    options: {
      readonly onFiber: (fiber: Fiber<A, E>) => Z;
      readonly onRuntimeFiber: (fiber: RuntimeFiber<A, E>) => Z;
    },
  ): Z;
};

Getters

children

Added in v2.0.0 Source

Retrieves the immediate children of the fiber.

Signature

declare const children: <A, E>(self: Fiber<A, E>) => Effect.Effect<Array<RuntimeFiber<any, any>>>;

id

Added in v2.0.0 Source

The identity of the fiber.

Signature

declare const id: <A, E>(self: Fiber<A, E>) => FiberId.FiberId;

poll

Added in v2.0.0 Source

Tentatively observes the fiber, but returns immediately if it is not already done.

Signature

declare const poll: <A, E>(self: Fiber<A, E>) => Effect.Effect<Option.Option<Exit.Exit<A, E>>>;

status

Added in v2.0.0 Source

Returns the FiberStatus of a RuntimeFiber.

Signature

declare const status: <A, E>(self: RuntimeFiber<A, E>) => Effect.Effect<FiberStatus.FiberStatus>;

Instances

Order

Added in v2.0.0 Source

Signature

declare const Order: order.Order<RuntimeFiber<unknown, unknown>>;

Interruption

interrupt

Added in v2.0.0 Source

Interrupts the fiber from whichever fiber is calling this method. If the fiber has already exited, the returned effect will resume immediately. Otherwise, the effect will resume when the fiber exits.

Signature

declare const interrupt: <A, E>(self: Fiber<A, E>) => Effect.Effect<Exit.Exit<A, E>>;

interruptAll

Added in v2.0.0 Source

Interrupts all fibers, awaiting their interruption.

Signature

declare const interruptAll: (fibers: Iterable<Fiber<any, any>>) => Effect.Effect<void>;

Interrupts all fibers as by the specified fiber, awaiting their interruption.

Signature

declare const interruptAllAs: {
  (fiberId: FiberId): (fibers: Iterable<Fiber<any, any>>) => Effect<void>;
  (fibers: Iterable<Fiber<any, any>>, fiberId: FiberId): Effect<void>;
};

interruptAs

Added in v2.0.0 Source

Interrupts the fiber as if interrupted from the specified fiber. If the fiber has already exited, the returned effect will resume immediately. Otherwise, the effect will resume when the fiber exits.

Signature

declare const interruptAs: {
  (fiberId: FiberId): <A, E>(self: Fiber<A, E>) => Effect<Exit<A, E>>;
  <A, E>(self: Fiber<A, E>, fiberId: FiberId): Effect<Exit<A, E>>;
};

Interrupts the fiber as if interrupted from the specified fiber. If the fiber has already exited, the returned effect will resume immediately. Otherwise, the effect will resume when the fiber exits.

Signature

declare const interruptAsFork: {
  (fiberId: FiberId): <A, E>(self: Fiber<A, E>) => Effect<void>;
  <A, E>(self: Fiber<A, E>, fiberId: FiberId): Effect<void>;
};

Interrupts the fiber from whichever fiber is calling this method. The interruption will happen in a separate daemon fiber, and the returned effect will always resume immediately without waiting.

Signature

declare const interruptFork: <A, E>(self: Fiber<A, E>) => Effect.Effect<void>;

Mapping

map

Added in v2.0.0 Source

Maps over the value the Fiber computes.

Signature

declare const map: {
  <A, B>(f: (a: A) => B): <E>(self: Fiber<A, E>) => Fiber<B, E>;
  <A, E, B>(self: Fiber<A, E>, f: (a: A) => B): Fiber<B, E>;
};

mapEffect

Added in v2.0.0 Source

Effectually maps over the value the fiber computes.

Signature

declare const mapEffect: {
  <A, A2, E2>(f: (a: A) => Effect<A2, E2>): <E>(self: Fiber<A, E>) => Fiber<A2, E2 | E>;
  <A, E, A2, E2>(self: Fiber<A, E>, f: (a: A) => Effect<A2, E2>): Fiber<A2, E | E2>;
};

mapFiber

Added in v2.0.0 Source

Passes the success of this fiber to the specified callback, and continues with the fiber that it returns.

Signature

declare const mapFiber: {
  <E, E2, A, B>(f: (a: A) => Fiber<B, E2>): (self: Fiber<A, E>) => Effect<Fiber<B, E | E2>>;
  <A, E, E2, B>(self: Fiber<A, E>, f: (a: A) => Fiber<B, E2>): Effect<Fiber<B, E | E2>>;
};

Models

Fiber interface

Added in v2.0.0 Source

A fiber is a lightweight thread of execution that never consumes more than a whole thread (but may consume much less, depending on contention and asynchronicity). Fibers are spawned by forking effects, which run concurrently with the parent effect.

Fibers can be joined, yielding their result to other fibers, or interrupted, which terminates the fiber, safely releasing all resources.

Signature

interface Fiber<out A, out E = never> extends Effect<A, E>, Variance<A, E> {
  readonly [ignoreSymbol]?: FiberUnifyIgnore;
  readonly [typeSymbol]?: unknown;
  readonly [unifySymbol]?: FiberUnify<Fiber<A, E>>;
  readonly await: Effect<Exit<A, E>>;
  readonly children: Effect<Array<Runtime<any, any>>>;
  readonly inheritAll: Effect<void>;
  readonly poll: Effect<Option<Exit<A, E>>>;
  id(): FiberId;
  interruptAsFork(fiberId: FiberId): Effect<void>;
}

FiberUnify interface

Added in v3.8.0 Source

Signature

interface FiberUnify<
  A extends {
    [typeSymbol]?: any;
  },
> extends EffectUnify<A> {
  Fiber?: () => A[typeof typeSymbol] extends Fiber<A0, E0> | _ ? Fiber<A0, E0> : never;
}

FiberUnifyIgnore interface

Added in v3.8.0 Source

Signature

interface FiberUnifyIgnore extends EffectUnifyIgnore {
  Effect?: true;
}

RuntimeFiber interface

Added in v2.0.0 Source

A runtime fiber that is executing an effect. Runtime fibers have an identity and a trace.

Signature

interface RuntimeFiber<out A, out E = never> extends Fiber<A, E>, RuntimeVariance<A, E> {
  readonly [ignoreSymbol]?: RuntimeFiberUnifyIgnore;
  readonly [typeSymbol]?: unknown;
  readonly [unifySymbol]?: RuntimeFiberUnify<RuntimeFiber<A, E>>;
  readonly runtimeFlags: Effect<RuntimeFlags>;
  readonly status: Effect<FiberStatus>;
  currentContext: Context<never>;
  currentDefaultServices: Context<DefaultServices>;
  currentOpCount: number;
  currentScheduler: Scheduler;
  currentSpan: AnySpan | undefined;
  currentSupervisor: Supervisor<unknown>;
  currentTracer: Tracer;
  addObserver(observer: (exit: Exit<A, E>) => void): void;
  getFiberRef<X>(fiberRef: FiberRef<X>): X;
  getFiberRefs(): FiberRefs;
  id(): Runtime;
  removeObserver(observer: (exit: Exit<A, E>) => void): void;
  unsafeInterruptAsFork(fiberId: FiberId): void;
  unsafePoll(): Exit<A, E> | null;
}

RuntimeFiberUnify interface

Added in v3.8.0 Source

Signature

interface RuntimeFiberUnify<
  A extends {
    [typeSymbol]?: any;
  },
> extends FiberUnify<A> {
  RuntimeFiber?: () => A[typeof typeSymbol] extends RuntimeFiber<A0, E0> | _
    ? RuntimeFiber<A0, E0>
    : never;
}

RuntimeFiberUnifyIgnore interface

Added in v3.8.0 Source

Signature

interface RuntimeFiberUnifyIgnore extends FiberUnifyIgnore {
  Fiber?: true;
}

Other

Signature

declare const await: <A, E>(self: Fiber<A, E>) => Effect.Effect<Exit.Exit<A, E>>;

Fiber

Added in v2.0.0 Source

Signature

declare const void: Fiber<void>

Refinements

isFiber

Added in v2.0.0 Source

Returns true if the specified value is a Fiber, false otherwise.

Signature

declare const isFiber: (u: unknown) => u is Fiber<unknown, unknown>;

Returns true if the specified Fiber is a RuntimeFiber, false otherwise.

Signature

declare const isRuntimeFiber: <A, E>(self: Fiber<A, E>) => self is RuntimeFiber<A, E>;

Symbols

FiberTypeId

Added in v2.0.0 Source

Signature

declare const FiberTypeId: unique symbol;

FiberTypeId type

Added in v2.0.0 Source

Signature

type FiberTypeId = typeof FiberTypeId;

Signature

declare const RuntimeFiberTypeId: unique symbol;

RuntimeFiberTypeId type

Added in v2.0.0 Source

Signature

type RuntimeFiberTypeId = typeof RuntimeFiberTypeId;

Utilities

Gets the current fiber if one is running.

Signature

declare const getCurrentFiber: () => Option.Option<RuntimeFiber<any, any>>;

Zipping

zip

Added in v2.0.0 Source

Zips this fiber and the specified fiber together, producing a tuple of their output.

Signature

declare const zip: {
  <A2, E2>(that: Fiber<A2, E2>): <A, E>(self: Fiber<A, E>) => Fiber<[A, A2], E2 | E>;
  <A, E, A2, E2>(self: Fiber<A, E>, that: Fiber<A2, E2>): Fiber<[A, A2], E | E2>;
};

zipLeft

Added in v2.0.0 Source

Same as zip but discards the output of that Fiber.

Signature

declare const zipLeft: {
  <A2, E2>(that: Fiber<A2, E2>): <A, E>(self: Fiber<A, E>) => Fiber<A, E2 | E>;
  <A, E, A2, E2>(self: Fiber<A, E>, that: Fiber<A2, E2>): Fiber<A, E | E2>;
};

zipRight

Added in v2.0.0 Source

Same as zip but discards the output of this Fiber.

Signature

declare const zipRight: {
  <A2, E2>(that: Fiber<A2, E2>): <A, E>(self: Fiber<A, E>) => Fiber<A2, E2 | E>;
  <A, E, A2, E2>(self: Fiber<A, E>, that: Fiber<A2, E2>): Fiber<A2, E | E2>;
};

zipWith

Added in v2.0.0 Source

Zips this fiber with the specified fiber, combining their results using the specified combiner function. Both joins and interruptions are performed in sequential order from left to right.

Signature

declare const zipWith: {
  <B, E2, A, C>(
    that: Fiber<B, E2>,
    f: (a: A, b: B) => C,
  ): <E>(self: Fiber<A, E>) => Fiber<C, E2 | E>;
  <A, E, B, E2, C>(self: Fiber<A, E>, that: Fiber<B, E2>, f: (a: A, b: B) => C): Fiber<C, E | E2>;
};