QueueType

Queue

Queue<'a> hands each value from producer fibers to exactly one taker, in FIFO order, with an overflow strategy chosen when it is created. Several fibers can take from one queue and share its values between them. This module creates queues and offers to them; take values, inspect, and shut a queue down with the Dequeue functions, which accept a Queue.

Create a queue with make and a QueueStrategy, or with a shorthand: bounded suspends producers while full, dropping discards the new value, sliding evicts the oldest, and unbounded never refuses. makeScoped shuts the queue down when the current scope closes.

Interrupting a suspended offer never enqueues its value, and interrupting a suspended take never loses one. When zero or many consumers must each see every value, use a Hub instead.

tryOffer returns immediately and can be called from a synchronous callback. Its QueueTryOfferResult distinguishes acceptance, a full back-pressure queue, discard, eviction, and shutdown. A Full or Shutdown result leaves the offered value outside the queue.

Read the Queue guide for strategies, batching, shutdown, and draining on stop.

Specification

Kind
Type
Members
12
Examples
0
droppingNowSignature
Queue.droppingNow capacity
slidingNowSignature
Queue.slidingNow capacity
shutdownNowSignature
Queue.shutdownNow queue
makeSignature
Queue.make strategy
makeScopedSignature
Queue.makeScoped strategy

Summary

NameSignatureSynopsis
Create queues
makeQueue.make strategyCreates a queue with the given overflow strategy.
makeScopedQueue.makeScoped strategyCreates a queue that is shut down when the current scope closes.
boundedQueue.bounded capacityCreates a lossless queue whose offer suspends while it holds capacity values.
droppingQueue.dropping capacityCreates a queue whose offer discards the new value and returns false when full.
slidingQueue.sliding capacityCreates a queue whose offer evicts the oldest value when full and returns true.
unboundedQueue.unbounded ()Creates a queue that never rejects or suspends an offer.
Offer values
offerQueue.offer value queueAdds a value according to the queue's strategy.
tryOfferQueue.tryOffer value queueTries to add a value immediately, without starting a Flow or suspending the caller.
offerAllQueue.offerAll values queueAdds values in order according to the queue's strategy.
Use from callbacks
droppingNowQueue.droppingNow capacityCreates a dropping queue synchronously for synchronous producers such as UI event handlers.
shutdownNowQueue.shutdownNow queueShuts down a queue synchronously, waking any suspended consumers.
Operations
slidingNowQueue.slidingNow capacityCreates a sliding queue synchronously for synchronous producers such as UI event handlers.

Create queues

kind:membertask:create

make

Queue.make strategy
Member
Creates a queue with the given overflow strategy.

Parameters

NameTypeDescription
strategyQueueStrategy

Returns

Flow<'env, 'error, Queue<'a>>

Verification Examples

Queue.make (QueueStrategy.Sliding 1)
kind:membertask:create

makeScoped

Queue.makeScoped strategy
Member
Creates a queue that is shut down when the current scope closes.

Parameters

NameTypeDescription
strategyQueueStrategy

Returns

Flow<'env, 'error, Queue<'a>>

Verification Examples

flow {
    let! (jobs: Queue<string>) = Queue.makeScoped (QueueStrategy.BackPressure 64)
    do! jobs |> Queue.offer "first job" |> Flow.ignore
}
|> Flow.scoped
kind:membertask:create

bounded

Queue.bounded capacity
Member
Creates a lossless queue whose offer suspends while it holds capacity values.

Parameters

NameTypeDescription
capacityint

Returns

Flow<'env, 'error, Queue<'a>>

Verification Examples

Queue.bounded 64
kind:membertask:create

dropping

Queue.dropping capacity
Member
Creates a queue whose offer discards the new value and returns false when full.

Parameters

NameTypeDescription
capacityint

Returns

Flow<'env, 'error, Queue<'a>>
kind:membertask:create

sliding

Queue.sliding capacity
Member
Creates a queue whose offer evicts the oldest value when full and returns true.

Parameters

NameTypeDescription
capacityint

Returns

Flow<'env, 'error, Queue<'a>>
kind:membertask:create

unbounded

Queue.unbounded ()
Member
Creates a queue that never rejects or suspends an offer.

Parameters

NameTypeDescription
unit

Returns

Flow<'env, 'error, Queue<'a>>

Offer values

kind:membertask:offer

offer

Queue.offer value queue
Member
Adds a value according to the queue's strategy.

Parameters

NameTypeDescription
value'a
queueQueue<'a>

Returns

Flow<'env, 'error, bool>

Verification Examples

flow {
    let! (jobs: Queue<string>) = Queue.bounded 8
    return! jobs |> Queue.offer "job"
}
kind:membertask:offer

tryOffer

Queue.tryOffer value queue
Member
Tries to add a value immediately, without starting a Flow or suspending the caller.

Parameters

NameTypeDescription
value'a
queueQueue<'a>

Returns

QueueTryOfferResult
kind:membertask:offer

offerAll

Queue.offerAll values queue
Member
Adds values in order according to the queue's strategy.

Parameters

NameTypeDescription
values'a seq
queueQueue<'a>

Returns

Flow<'env, 'error, bool>

Verification Examples

flow {
    let! (samples: Queue<float>) = Queue.unbounded ()
    do! samples |> Queue.offerAll [ 1.0; 2.0; 3.0 ] |> Flow.ignore
    return! samples |> Dequeue.takeUpTo 500
}

Use from callbacks

kind:membertask:callbacks

droppingNow

Queue.droppingNow capacity
Member
Creates a dropping queue synchronously for synchronous producers such as UI event handlers.

Parameters

NameTypeDescription
capacityint

Returns

Queue<'a>
kind:membertask:callbacks

shutdownNow

Queue.shutdownNow queue
Member
Shuts down a queue synchronously, waking any suspended consumers.

Parameters

NameTypeDescription
queueQueue<'a>

Returns

unit

Operations

kind:member

slidingNow

Queue.slidingNow capacity
Member
Creates a sliding queue synchronously for synchronous producers such as UI event handlers.

Parameters

NameTypeDescription
capacityint

Returns

Queue<'a>