Hyperlinkv0.8.0-beta.28

TxQueue

TxQueue.peekconsteffect/TxQueue.ts:1019
<A, E>(self: TxDequeue<A, E>): Effect.Effect<A, E>

Waits transactionally for the next item and returns it without removing it.

Details

If the queue is open but empty, the transaction retries until an item is available or the queue completes. If the queue is done, the queue's completion cause is propagated through the error channel.

Example (Peeking without removing values)

import { Effect, TxQueue } from "effect"

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

  // Peek at the next item without removing it
  const item = yield* TxQueue.peek(queue)
  console.log(item) // 42

  // Item is still in the queue
  const size = yield* TxQueue.size(queue)
  console.log(size) // 1
})

// Error handling example
const errorExample = Effect.gen(function*() {
  const queue = yield* TxQueue.bounded<number, string>(5)
  yield* TxQueue.fail(queue, "queue failed")

  // peek() propagates the queue error through E-channel
  const result = yield* Effect.flip(TxQueue.peek(queue))
  console.log(result) // "queue failed"
})
combinators
Source effect/TxQueue.ts:101915 lines
export const peek = <A, E>(self: TxDequeue<A, E>): Effect.Effect<A, E> =>
  Effect.gen(function*() {
    const state = yield* TxRef.get(self.stateRef)
    if (state._tag === "Done") {
      return yield* Effect.failCause(state.cause)
    }

    const chunk = yield* TxChunk.get(self.items)
    const head = Chunk.head(chunk)
    if (Option.isNone(head)) {
      return yield* Effect.txRetry
    }

    return head.value
  }).pipe(Effect.tx)