Skip to content

Mailbox

13 exports Added in v3.8.0 Source

Combinators

into

Added in v3.8.0 Source

Run an Effect into a Mailbox, where success ends the mailbox and failure fails the mailbox.

Signature

declare const into: {
  <A, E>(
    self: Mailbox<A, E>,
  ): <AX, EX, RX>(effect: Effect<AX, EX, RX>) => Effect<boolean, never, RX>;
  <AX, E, EX, RX, A>(effect: Effect<AX, EX, RX>, self: Mailbox<A, E>): Effect<boolean, never, RX>;
};

Constructors

make

Added in v3.8.0 Source

A Mailbox is a queue that can be signaled to be done or failed.

Signature

declare const make: <A, E = never>(
  capacity?:
    | number
    | {
        readonly capacity?: number;
        readonly strategy?: "suspend" | "dropping" | "sliding";
      },
) => Effect<Mailbox<A, E>>;

Example

import * as assert from "node:assert"
import { Effect, Mailbox } from "effect"

Effect.gen(function* () {
  const mailbox = yield* Mailbox.make<number, string>()

  // add messages to the mailbox
  yield* mailbox.offer(1)
  yield* mailbox.offer(2)
  yield* mailbox.offerAll([3, 4, 5])

  // take messages from the mailbox
  const [messages, done] = yield* mailbox.takeAll
  assert.deepStrictEqual(messages, [1, 2, 3, 4, 5])
  assert.strictEqual(done, false)

  // signal that the mailbox is done
  yield* mailbox.end
  const [messages2, done2] = yield* mailbox.takeAll
  assert.deepStrictEqual(messages2, [])
  assert.strictEqual(done2, true)

  // signal that the mailbox has failed
  yield* mailbox.fail("boom")
})

Conversions

fromStream

Added in v3.11.0 Source

Create a ReadonlyMailbox from a Stream.

Signature

declare const fromStream: {
  (options?: {
    readonly capacity?: number;
    readonly strategy?: "suspend" | "dropping" | "sliding";
  }): <A, E, R>(self: Stream<A, E, R>) => Effect<ReadonlyMailbox<A, E>, never, Scope | R>;
  <A, E, R>(
    self: Stream<A, E, R>,
    options?: {
      readonly capacity?: number;
      readonly strategy?: "suspend" | "dropping" | "sliding";
    },
  ): Effect<ReadonlyMailbox<A, E>, never, Scope | R>;
};

toChannel

Added in v3.8.0 Source

Create a Channel from a Mailbox.

Signature

declare const toChannel: <A, E>(self: ReadonlyMailbox<A, E>) => Channel<Chunk<A>, unknown, E>;

toStream

Added in v3.8.0 Source

Create a Stream from a Mailbox.

Signature

declare const toStream: <A, E>(self: ReadonlyMailbox<A, E>) => Stream<A, E>;

Guards

isMailbox

Added in v3.8.0 Source

Signature

declare function isMailbox<A = unknown, E = unknown>(u: unknown): u is Mailbox<A, E>;

Signature

declare function isReadonlyMailbox<A = unknown, E = unknown>(
  u: unknown,
): u is ReadonlyMailbox<A, E>;

Models

Mailbox interface

Added in v3.8.0 Source

A Mailbox is a queue that can be signaled to be done or failed.

Signature

interface Mailbox<in out A, in out E = never> extends ReadonlyMailbox<A, E> {
  readonly [TypeId]: typeof TypeId;
  readonly done: (exit: Exit<void, E>) => Effect<boolean>;
  readonly end: Effect<boolean>;
  readonly fail: (error: E) => Effect<boolean>;
  readonly failCause: (cause: Cause<E>) => Effect<boolean>;
  readonly offer: (message: A) => Effect<boolean>;
  readonly offerAll: (messages: Iterable<A>) => Effect<Chunk<A>>;
  readonly shutdown: Effect<boolean>;
  readonly unsafeDone: (exit: Exit<void, E>) => boolean;
  readonly unsafeOffer: (message: A) => boolean;
  readonly unsafeOfferAll: (messages: Iterable<A>) => Chunk<A>;
}

ReadonlyMailbox interface

Added in v3.8.0 Source

A ReadonlyMailbox represents a mailbox that can only be read from.

Signature

interface ReadonlyMailbox<out A, out E = never>
  extends Effect<readonly [messages: Chunk<A>, done: boolean], E>, Inspectable {
  readonly [ReadonlyTypeId]: typeof ReadonlyTypeId;
  readonly await: Effect<void, E>;
  readonly clear: Effect<Chunk<A>, E>;
  readonly size: Effect<Option<number>>;
  readonly take: Effect<A, NoSuchElementException | E>;
  readonly takeAll: Effect<readonly [Chunk<A>, boolean], E>;
  readonly takeN: (n: number) => Effect<readonly [Chunk<A>, boolean], E>;
  readonly unsafeSize: () => Option<number>;
}

Type Ids

Signature

declare const ReadonlyTypeId: unique symbol;

ReadonlyTypeId type

Added in v3.8.0 Source

Signature

type ReadonlyTypeId = typeof ReadonlyTypeId;

TypeId

Added in v3.8.0 Source

Signature

declare const TypeId: unique symbol;

TypeId type

Added in v3.8.0 Source

Signature

type TypeId = typeof TypeId;