Hyperlinkv0.8.0-beta.28

Queue

Queue.offerAllUnsafeconsteffect/Queue.ts:795
<A, E>(self: Enqueue<A, E>, messages: Iterable<A>): Array<A>

Adds multiple messages to the queue synchronously. Returns the remaining messages that were not added.

When to use

Use when queue internals or a performance boundary need a synchronous batch offer and can handle any messages that do not fit.

Gotchas

This is an unsafe operation that directly modifies the queue without Effect wrapping.

Example (Offering multiple values synchronously)

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

// Create a bounded queue and use unsafe API
const program = Effect.gen(function*() {
  const queue = yield* Queue.bounded<number>(3)

  // Try to add 5 messages to capacity-3 queue using unsafe API
  const remaining = Queue.offerAllUnsafe(queue, [1, 2, 3, 4, 5])
  console.log(remaining) // [4, 5] - couldn't fit the last 2

  // Check what's in the queue
  const size = Queue.sizeUnsafe(queue)
  console.log(size) // 3
})
Offering
Source effect/Queue.ts:79533 lines
export const offerAllUnsafe = <A, E>(self: Enqueue<A, E>, messages: Iterable<A>): Array<A> => {
  if (self.state._tag !== "Open") {
    return Arr.fromIterable(messages)
  } else if (
    self.capacity === Number.POSITIVE_INFINITY ||
    self.strategy === "sliding"
  ) {
    MutableList.appendAll(self.messages, messages)
    if (self.strategy === "sliding") {
      MutableList.takeN(self.messages, self.messages.length - self.capacity)
    }
    scheduleReleaseTaker(self as Queue<A, E>)
    return []
  }
  const free = self.capacity <= 0
    ? self.state.takers.size
    : self.capacity - self.messages.length
  if (free === 0) {
    return Arr.fromIterable(messages)
  }
  const remaining: Array<A> = []
  let i = 0
  for (const message of messages) {
    if (i < free) {
      MutableList.append(self.messages, message)
    } else {
      remaining.push(message)
    }
    i++
  }
  scheduleReleaseTaker(self as Queue<A, E>)
  return remaining
}
Referenced by 2 symbols