Hyperlinkv0.8.0-beta.28

TxQueue

TxQueue.offerconsteffect/TxQueue.ts:546
<A, E>(value: A): (self: TxEnqueue<A, E>) => Effect.Effect<boolean>
<A, E>(self: TxEnqueue<A, E>, value: A): Effect.Effect<boolean>

Offers an item to the queue and returns whether it was accepted.

Details

Open unbounded queues always accept; open bounded queues retry while full; dropping queues return false when full; sliding queues evict the oldest item when full. Closing or done queues return false. This function mutates the original TxQueue by adding the item according to the queue's strategy. It does not return a new TxQueue reference.

Example (Offering a value)

import { Effect, TxQueue } from "effect"

const program = Effect.gen(function*() {
  const queue = yield* TxQueue.bounded<number>(10)

  // Offer an item - returns true if accepted
  const accepted = yield* TxQueue.offer(queue, 42)
  console.log(accepted) // true
})
combinators
Source effect/TxQueue.ts:54641 lines
export const offer: {
  <A, E>(value: A): (self: TxEnqueue<A, E>) => Effect.Effect<boolean>
  <A, E>(self: TxEnqueue<A, E>, value: A): Effect.Effect<boolean>
} = dual(
  2,
  <A, E>(self: TxEnqueue<A, E>, value: A): Effect.Effect<boolean> =>
    Effect.gen(function*() {
      const state = yield* TxRef.get(self.stateRef)
      if (state._tag === "Done" || state._tag === "Closing") {
        return false
      }

      const currentSize = yield* size(self)

      // Unbounded - always accept
      if (self.strategy === "unbounded") {
        yield* TxChunk.append(self.items, value)
        return true
      }

      // For bounded queues, check capacity
      if (currentSize < self.capacity) {
        yield* TxChunk.append(self.items, value)
        return true
      }

      // Queue is at capacity, strategy-specific behavior
      if (self.strategy === "dropping") {
        return false // Drop the new item
      }

      if (self.strategy === "sliding") {
        yield* TxChunk.drop(self.items, 1) // Remove oldest item
        yield* TxChunk.append(self.items, value) // Add new item
        return true
      }

      // bounded strategy - block until space is available
      return yield* Effect.txRetry
    }).pipe(Effect.tx)
)
Referenced by 3 symbols