Skip to content

FiberHandle

17 exports Added in v2.0.0 Source

Other

awaitEmpty

Added in v3.13.0 Source

Wait for the fiber in the FiberHandle to complete.

Signature

declare function awaitEmpty<A, E>(self: FiberHandle<A, E>): Effect<void, E>;

clear

Added in v2.0.0 Source

Signature

declare function clear<A, E>(self: FiberHandle<A, E>): Effect<void>;

FiberHandle interface

Added in v2.0.0 Source

Signature

interface FiberHandle<out A = unknown, out E = unknown> extends Pipeable, Inspectable {
  readonly [TypeId]: typeof TypeId;
  readonly deferred: Deferred<void, unknown>;
}

get

Added in v2.0.0 Source

Retrieve the fiber from the FiberHandle.

Signature

declare function get<A, E>(
  self: FiberHandle<A, E>,
): Effect<RuntimeFiber<A, E>, NoSuchElementException>;

Signature

declare function isFiberHandle(u: unknown): u is FiberHandle<unknown, unknown>;

join

Added in v2.0.0 Source

If any of the Fiber's in the handle terminate with a failure, the returned Effect will terminate with the first failure that occurred.

Signature

declare function join<A, E>(self: FiberHandle<A, E>): Effect<void, E>;

make

Added in v2.0.0 Source

A FiberHandle can be used to store a single fiber. When the associated Scope is closed, the contained fiber will be interrupted.

You can add a fiber to the handle using FiberHandle.run, and the fiber will be automatically removed from the FiberHandle when it completes.

Signature

declare function make<A = unknown, E = unknown>(): Effect<FiberHandle<A, E>, never, Scope>;

makeRuntime

Added in v2.0.0 Source

Create an Effect run function that is backed by a FiberHandle.

Signature

declare function makeRuntime<R, E = unknown, A = unknown>(): Effect<
  <XE, XA>(
    effect: Effect<XA, XE, R>,
    options?: RunForkOptions & {
      readonly onlyIfMissing?: boolean;
    },
  ) => RuntimeFiber<XA, XE>,
  never,
  Scope | R
>;

Create an Effect run function that is backed by a FiberHandle.

Signature

declare function makeRuntimePromise<R = never, A = unknown, E = unknown>(): Effect<
  <XE, XA>(effect: Effect<XA, XE, R>, options?: RunForkOptions) => Promise<XA>,
  never,
  Scope | R
>;

run

Added in v2.0.0 Source

Run an Effect and add the forked fiber to the FiberHandle. When the fiber completes, it will be removed from the FiberHandle.

Signature

declare const run: {
  <A, E>(
    self: FiberHandle<A, E>,
    options?: {
      readonly onlyIfMissing?: boolean;
      readonly propagateInterruption?: boolean;
    },
  ): <R, XE, XA>(effect: Effect<XA, XE, R>) => Effect<RuntimeFiber<XA, XE>, never, R>;
  <A, E, R, XE, XA>(
    self: FiberHandle<A, E>,
    effect: Effect<XA, XE, R>,
    options?: {
      readonly onlyIfMissing?: boolean;
      readonly propagateInterruption?: boolean;
    },
  ): Effect<RuntimeFiber<XA, XE>, never, R>;
};

runtime

Added in v2.0.0 Source

Capture a Runtime and use it to fork Effect's, adding the forked fibers to the FiberHandle.

Signature

declare const runtime: <A, E>(
  self: FiberHandle<A, E>,
) => <R = never>() => Effect.Effect<
  <XE extends E, XA extends A>(
    effect: Effect.Effect<XA, XE, R>,
    options?: Runtime.RunForkOptions & {
      readonly onlyIfMissing?: boolean;
      readonly propagateInterruption?: boolean;
    },
  ) => Fiber.RuntimeFiber<XA, XE>,
  never,
  R
>;

Example

import { Context, Effect, FiberHandle } from "effect"

interface Users {
  readonly _: unique symbol
}
const Users = Context.GenericTag<
  Users,
  {
    getAll: Effect.Effect<Array<unknown>>
  }
>("Users")

Effect.gen(function* () {
  const handle = yield* FiberHandle.make()
  const run = yield* FiberHandle.runtime(handle)<Users>()

  // run an effect and set the fiber in the handle
  run(Effect.andThen(Users, (_) => _.getAll))

  // this will interrupt the previous fiber
  run(Effect.andThen(Users, (_) => _.getAll))
}).pipe(
  Effect.scoped, // The fiber will be interrupted when the scope is closed
)

runtimePromise

Added in v3.13.0 Source

Capture a Runtime and use it to fork Effect's, adding the forked fibers to the FiberHandle.

The returned run function will return Promise's that will resolve when the fiber completes.

Signature

declare function runtimePromise<A, E>(
  self: FiberHandle<A, E>,
): <R = never>() => Effect<
  <XE, XA>(
    effect: Effect<XA, XE, R>,
    options?: RunForkOptions & {
      readonly propagateInterruption?: boolean;
    },
  ) => Promise<XA>,
  never,
  R
>;

set

Added in v2.0.0 Source

Set the fiber in the FiberHandle. When the fiber completes, it will be removed from the FiberHandle. If a fiber already exists in the FiberHandle, it will be interrupted unless options.onlyIfMissing is set.

Signature

declare const set: {
  <A, E, XE, XA>(
    fiber: RuntimeFiber<XA, XE>,
    options?: {
      readonly onlyIfMissing?: boolean;
      readonly propagateInterruption?: boolean;
    },
  ): (self: FiberHandle<A, E>) => Effect<void>;
  <A, E, XE, XA>(
    self: FiberHandle<A, E>,
    fiber: RuntimeFiber<XA, XE>,
    options?: {
      readonly onlyIfMissing?: boolean;
      readonly propagateInterruption?: boolean;
    },
  ): Effect<void>;
};

TypeId

Added in v2.0.0 Source

Signature

declare const TypeId: unique symbol;

TypeId type

Added in v2.0.0 Source

Signature

type TypeId = typeof TypeId;

unsafeGet

Added in v2.0.0 Source

Retrieve the fiber from the FiberHandle.

Signature

declare function unsafeGet<A, E>(self: FiberHandle<A, E>): Option<RuntimeFiber<A, E>>;

unsafeSet

Added in v2.0.0 Source

Set the fiber in a FiberHandle. When the fiber completes, it will be removed from the FiberHandle. If a fiber is already running, it will be interrupted unless options.onlyIfMissing is set.

Signature

declare const unsafeSet: {
  <A, E, XE, XA>(
    fiber: RuntimeFiber<XA, XE>,
    options?: {
      readonly interruptAs?: FiberId.FiberId;
      readonly onlyIfMissing?: boolean;
      readonly propagateInterruption?: boolean;
    },
  ): (self: FiberHandle<A, E>) => void;
  <A, E, XE, XA>(
    self: FiberHandle<A, E>,
    fiber: RuntimeFiber<XA, XE>,
    options?: {
      readonly interruptAs?: FiberId.FiberId;
      readonly onlyIfMissing?: boolean;
      readonly propagateInterruption?: boolean;
    },
  ): void;
};