Hyperlinkv0.8.0-beta.28

Channel

Channel.toQueueconsteffect/Channel.ts:8224
(
  options:
    | { readonly capacity: "unbounded" }
    | {
        readonly capacity: number
        readonly strategy?: "dropping" | "sliding" | "suspend" | undefined
      }
): <OutElem, OutErr, OutDone, Env>(
  self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>
) => Effect.Effect<
  Queue.Dequeue<OutElem, OutErr | Cause.Done>,
  never,
  Env | Scope.Scope
>
<OutElem, OutErr, OutDone, Env>(
  self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
  options:
    | { readonly capacity: "unbounded" }
    | {
        readonly capacity: number
        readonly strategy?: "dropping" | "sliding" | "suspend" | undefined
      }
): Effect.Effect<
  Queue.Dequeue<OutElem, OutErr | Cause.Done>,
  never,
  Env | Scope.Scope
>

Creates a scoped queue and forks the channel to feed it for concurrent consumption.

Details

Output elements are offered to the queue. Channel completion and failure are signaled through the queue. The queue is shut down when the surrounding scope closes.

Example (Converting channels to queues)

import { Channel, Data } from "effect"

class QueueError extends Data.TaggedError("QueueError")<{
  readonly operation: string
}> {}

// Create a channel with data
const dataChannel = Channel.fromIterable([1, 2, 3, 4, 5])

// Convert to queue for concurrent processing
const queueEffect = Channel.toQueue(dataChannel, { capacity: 32 })

// The queue can be used for concurrent consumption
// Multiple consumers can read from the queue
destructors
Source effect/Channel.ts:822441 lines
export const toQueue: {
  (
    options: {
      readonly capacity: "unbounded"
    } | {
      readonly capacity: number
      readonly strategy?: "dropping" | "sliding" | "suspend" | undefined
    }
  ): <OutElem, OutErr, OutDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>
  ) => Effect.Effect<Queue.Dequeue<OutElem, OutErr | Cause.Done>, never, Env | Scope.Scope>
  <OutElem, OutErr, OutDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
    options: {
      readonly capacity: "unbounded"
    } | {
      readonly capacity: number
      readonly strategy?: "dropping" | "sliding" | "suspend" | undefined
    }
  ): Effect.Effect<Queue.Dequeue<OutElem, OutErr | Cause.Done>, never, Env | Scope.Scope>
} = dual(
  (args) => isChannel(args[0]),
  Effect.fnUntraced(function*<OutElem, OutErr, OutDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, unknown, unknown, unknown, Env>,
    options: {
      readonly capacity: "unbounded"
    } | {
      readonly capacity: number
      readonly strategy?: "dropping" | "sliding" | "suspend" | undefined
    }
  ) {
    const scope = yield* Effect.scope
    const queue = yield* Queue.make<OutElem, OutErr | Cause.Done>({
      capacity: typeof options.capacity === "number" ? options.capacity : undefined,
      strategy: typeof options.capacity === "number" ? options.strategy : undefined
    })
    yield* Scope.addFinalizer(scope, Queue.shutdown(queue))
    yield* Effect.forkIn(runIntoQueue(self, queue), scope)
    return queue
  })
)