/** * @since 3.8.0 * @experimental */ import type { Cause, NoSuchElementException } from "./Cause.js"; import type { Channel } from "./Channel.js"; import type { Chunk } from "./Chunk.js"; import type { Effect } from "./Effect.js"; import type { Exit } from "./Exit.js"; import type { Inspectable } from "./Inspectable.js"; import type { Option } from "./Option.js"; import type { Scope } from "./Scope.js"; import type { Stream } from "./Stream.js"; /** * @since 3.8.0 * @experimental * @category type ids */ export declare const TypeId: unique symbol; /** * @since 3.8.0 * @experimental * @category type ids */ export type TypeId = typeof TypeId; /** * @since 3.8.0 * @experimental * @category type ids */ export declare const ReadonlyTypeId: unique symbol; /** * @since 3.8.0 * @experimental * @category type ids */ export type ReadonlyTypeId = typeof ReadonlyTypeId; /** * @since 3.8.0 * @experimental * @category guards */ export declare const isMailbox: (u: unknown) => u is Mailbox; /** * @since 3.8.0 * @experimental * @category guards */ export declare const isReadonlyMailbox: (u: unknown) => u is ReadonlyMailbox; /** * A `Mailbox` is a queue that can be signaled to be done or failed. * * @since 3.8.0 * @experimental * @category models */ export interface Mailbox extends ReadonlyMailbox { readonly [TypeId]: TypeId; /** * Add a message to the mailbox. Returns `false` if the mailbox is done. */ readonly offer: (message: A) => Effect; /** * Add a message to the mailbox. Returns `false` if the mailbox is done. */ readonly unsafeOffer: (message: A) => boolean; /** * Add multiple messages to the mailbox. Returns the remaining messages that * were not added. */ readonly offerAll: (messages: Iterable) => Effect>; /** * Add multiple messages to the mailbox. Returns the remaining messages that * were not added. */ readonly unsafeOfferAll: (messages: Iterable) => Chunk; /** * Fail the mailbox with an error. If the mailbox is already done, `false` is * returned. */ readonly fail: (error: E) => Effect; /** * Fail the mailbox with a cause. If the mailbox is already done, `false` is * returned. */ readonly failCause: (cause: Cause) => Effect; /** * Signal that the mailbox is complete. If the mailbox is already done, `false` is * returned. */ readonly end: Effect; /** * Signal that the mailbox is done. If the mailbox is already done, `false` is * returned. */ readonly done: (exit: Exit) => Effect; /** * Signal that the mailbox is done. If the mailbox is already done, `false` is * returned. */ readonly unsafeDone: (exit: Exit) => boolean; /** * Shutdown the mailbox, canceling any pending operations. * If the mailbox is already done, `false` is returned. */ readonly shutdown: Effect; } /** * A `ReadonlyMailbox` represents a mailbox that can only be read from. * * @since 3.8.0 * @experimental * @category models */ export interface ReadonlyMailbox extends Effect, done: boolean], E>, Inspectable { readonly [ReadonlyTypeId]: ReadonlyTypeId; /** * Take all messages from the mailbox, returning an empty Chunk if the mailbox * is empty or done. */ readonly clear: Effect, E>; /** * Take all messages from the mailbox, or wait for messages to be available. * * If the mailbox is done, the `done` flag will be `true`. If the mailbox * fails, the Effect will fail with the error. */ readonly takeAll: Effect, done: boolean], E>; /** * Take a specified number of messages from the mailbox. It will only take * up to the capacity of the mailbox. * * If the mailbox is done, the `done` flag will be `true`. If the mailbox * fails, the Effect will fail with the error. */ readonly takeN: (n: number) => Effect, done: boolean], E>; /** * Take a single message from the mailbox, or wait for a message to be * available. * * If the mailbox is done, it will fail with `NoSuchElementException`. If the * mailbox fails, the Effect will fail with the error. */ readonly take: Effect; /** Wait for the mailbox to be done. */ readonly await: Effect; /** * Check the size of the mailbox. * * If the mailbox is complete, it will return `None`. */ readonly size: Effect>; /** * Check the size of the mailbox. * * If the mailbox is complete, it will return `None`. */ readonly unsafeSize: () => Option; } /** * A `Mailbox` is a queue that can be signaled to be done or failed. * * @since 3.8.0 * @experimental * @category constructors * @example * ```ts * import * as assert from "node:assert" * import { Effect, Mailbox } from "effect" * * Effect.gen(function*() { * const mailbox = yield* Mailbox.make() * * // 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") * }) * ``` */ export declare const make: (capacity?: number | { readonly capacity?: number; readonly strategy?: "suspend" | "dropping" | "sliding"; } | undefined) => Effect>; /** * Run an `Effect` into a `Mailbox`, where success ends the mailbox and failure * fails the mailbox. * * @since 3.8.0 * @experimental * @category combinators */ export declare const into: { /** * Run an `Effect` into a `Mailbox`, where success ends the mailbox and failure * fails the mailbox. * * @since 3.8.0 * @experimental * @category combinators */ (self: Mailbox): (effect: Effect) => Effect; /** * Run an `Effect` into a `Mailbox`, where success ends the mailbox and failure * fails the mailbox. * * @since 3.8.0 * @experimental * @category combinators */ (effect: Effect, self: Mailbox): Effect; }; /** * Create a `Channel` from a `Mailbox`. * * @since 3.8.0 * @experimental * @category conversions */ export declare const toChannel: (self: ReadonlyMailbox) => Channel, unknown, E>; /** * Create a `Stream` from a `Mailbox`. * * @since 3.8.0 * @experimental * @category conversions */ export declare const toStream: (self: ReadonlyMailbox) => Stream; /** * Create a `ReadonlyMailbox` from a `Stream`. * * @since 3.11.0 * @experimental * @category conversions */ export declare const fromStream: { /** * Create a `ReadonlyMailbox` from a `Stream`. * * @since 3.11.0 * @experimental * @category conversions */ (options?: { readonly capacity?: number | undefined; readonly strategy?: "suspend" | "dropping" | "sliding" | undefined; }): (self: Stream) => Effect, never, R | Scope>; /** * Create a `ReadonlyMailbox` from a `Stream`. * * @since 3.11.0 * @experimental * @category conversions */ (self: Stream, options?: { readonly capacity?: number | undefined; readonly strategy?: "suspend" | "dropping" | "sliding" | undefined; }): Effect, never, R | Scope>; }; //# sourceMappingURL=Mailbox.d.ts.map