Hyperlinkv0.8.0-beta.28

Queue

Queue.makeconsteffect/Queue.ts:441
<A, E = never>(
  options?:
    | {
        readonly capacity?: number | undefined
        readonly strategy?: "suspend" | "dropping" | "sliding" | undefined
      }
    | undefined
): Effect<Queue<A, E>>

Creates a Queue with optional capacity and overflow strategy.

Details

By default the queue is unbounded and uses the "suspend" strategy. Provide capacity for a bounded queue and choose "suspend", "dropping", or "sliding" to control what happens when the queue is full. The returned queue can be offered to, taken from, failed, ended, interrupted, or shut down.

Example (Creating queues)

import { Cause, Effect, Queue } from "effect"

Effect.gen(function*() {
  const queue = yield* Queue.make<number, string | Cause.Done>()

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

  // take messages from the queue
  const messages = yield* Queue.takeAll(queue)
  console.log(messages) // [1, 2, 3, 4, 5]

  // signal that the queue is done
  yield* Queue.end(queue)
  const done = yield* Effect.flip(Queue.take(queue))
  console.log(Cause.isDone(done)) // true

  // signal that another queue has failed
  const failedQueue = yield* Queue.make<number, string>()
  const failed = yield* Queue.fail(failedQueue, "boom")
  console.log(failed) // true
})
constructors
Source effect/Queue.ts:44121 lines
export const make = <A, E = never>(
  options?: {
    readonly capacity?: number | undefined
    readonly strategy?: "suspend" | "dropping" | "sliding" | undefined
  } | undefined
): Effect<Queue<A, E>> =>
  core.withFiber((fiber) => {
    const self = Object.create(QueueProto)
    self.dispatcher = fiber.currentDispatcher
    self.capacity = options?.capacity ?? Number.POSITIVE_INFINITY
    self.strategy = options?.strategy ?? "suspend"
    self.messages = MutableList.make()
    self.scheduleRunning = false
    self.state = {
      _tag: "Open",
      takers: new Set(),
      offers: new Set(),
      awaiters: new Set()
    }
    return internalEffect.succeed(self)
  })
Referenced by 10 symbols