/**
* @since 1.0.0
*/
import type * as Chunk from "@effect/data/Chunk"
import type * as MutableQueue from "@effect/data/MutableQueue"
import type * as MutableRef from "@effect/data/MutableRef"
import type * as Option from "@effect/data/Option"
import type { Pipeable } from "@effect/data/Pipeable"
import type * as Deferred from "@effect/io/Deferred"
import type * as Effect from "@effect/io/Effect"
import * as internal from "@effect/io/internal/queue"
/**
* @since 1.0.0
* @category symbols
*/
export const EnqueueTypeId: unique symbol = internal.EnqueueTypeId
/**
* @since 1.0.0
* @category symbols
*/
export type EnqueueTypeId = typeof EnqueueTypeId
/**
* @since 1.0.0
* @category symbols
*/
export const DequeueTypeId: unique symbol = internal.DequeueTypeId
/**
* @since 1.0.0
* @category symbols
*/
export type DequeueTypeId = typeof DequeueTypeId
/**
* @since 1.0.0
* @category symbols
*/
export const QueueStrategyTypeId: unique symbol = internal.QueueStrategyTypeId
/**
* @since 1.0.0
* @category symbols
*/
export type QueueStrategyTypeId = typeof QueueStrategyTypeId
/**
* @since 1.0.0
* @category models
*/
export interface Queue extends Enqueue, Dequeue, Pipeable {
/** @internal */
readonly queue: BackingQueue
/** @internal */
readonly takers: MutableQueue.MutableQueue>
/** @internal */
readonly shutdownHook: Deferred.Deferred
/** @internal */
readonly shutdownFlag: MutableRef.MutableRef
/** @internal */
readonly strategy: Strategy
}
/**
* @since 1.0.0
* @category models
*/
export interface Enqueue extends Queue.EnqueueVariance, BaseQueue, Pipeable {
/**
* Places one value in the queue.
*/
offer(value: A): Effect.Effect
/**
* Places one value in the queue when possible without needing the fiber runtime.
*/
unsafeOffer(value: A): boolean
/**
* For Bounded Queue: uses the `BackPressure` Strategy, places the values in
* the queue and always returns true. If the queue has reached capacity, then
* the fiber performing the `offerAll` will be suspended until there is room
* in the queue.
*
* For Unbounded Queue: Places all values in the queue and returns true.
*
* For Sliding Queue: uses `Sliding` Strategy If there is room in the queue,
* it places the values otherwise it removes the old elements and enqueues the
* new ones. Always returns true.
*
* For Dropping Queue: uses `Dropping` Strategy, It places the values in the
* queue but if there is no room it will not enqueue them and return false.
*/
offerAll(iterable: Iterable): Effect.Effect
}
/**
* @since 1.0.0
* @category models
*/
export interface Dequeue extends Queue.DequeueVariance, BaseQueue, Pipeable {
/**
* Takes the oldest value in the queue. If the queue is empty, this will return
* a computation that resumes when an item has been added to the queue.
*/
take(): Effect.Effect
/**
* Takes all the values in the queue and returns the values. If the queue is
* empty returns an empty collection.
*/
takeAll(): Effect.Effect>
/**
* Takes up to max number of values from the queue.
*/
takeUpTo(max: number): Effect.Effect>
/**
* Takes a number of elements from the queue between the specified minimum and
* maximum. If there are fewer than the minimum number of elements available,
* suspends until at least the minimum number of elements have been collected.
*/
takeBetween(min: number, max: number): Effect.Effect>
}
/**
* The base interface that all `Queue`s must implement.
*
* @since 1.0.0
* @category models
*/
export interface BaseQueue {
/**
* Returns the number of elements the queue can hold.
*/
capacity(): number
/**
* Returns false if shutdown has been called.
*/
isActive(): boolean
/**
* Retrieves the size of the queue, which is equal to the number of elements
* in the queue. This may be negative if fibers are suspended waiting for
* elements to be added to the queue.
*/
size(): Effect.Effect
/**
* Retrieves the size of the queue, which is equal to the number of elements
* in the queue. This may be negative if fibers are suspended waiting for
* elements to be added to the queue. Returns None if shutdown has been called
*/
unsafeSize(): Option.Option
/**
* Returns `true` if the `Queue` contains at least one element, `false`
* otherwise.
*/
isFull(): Effect.Effect
/**
* Returns `true` if the `Queue` contains zero elements, `false` otherwise.
*/
isEmpty(): Effect.Effect
/**
* Interrupts any fibers that are suspended on `offer` or `take`. Future calls
* to `offer*` and `take*` will be interrupted immediately.
*/
shutdown(): Effect.Effect
/**
* Returns `true` if `shutdown` has been called, otherwise returns `false`.
*/
isShutdown(): Effect.Effect
/**
* Waits until the queue is shutdown. The `Effect` returned by this method will
* not resume until the queue has been shutdown. If the queue is already
* shutdown, the `Effect` will resume right away.
*/
awaitShutdown(): Effect.Effect
}
/**
* @since 1.0.0
* @category models
*/
export interface Strategy extends Queue.StrategyVariance {
/**
* Returns the number of surplus values that were unable to be added to the
* `Queue`
*/
surplusSize(): number
/**
* Determines how the `Queue.Strategy` should shut down when the `Queue` is
* shut down.
*/
shutdown(): Effect.Effect
/**
* Determines the behavior of the `Queue.Strategy` when there are surplus
* values that could not be added to the `Queue` following an `offer`
* operation.
*/
handleSurplus(
iterable: Iterable,
queue: BackingQueue,
takers: MutableQueue.MutableQueue>,
isShutdown: MutableRef.MutableRef
): Effect.Effect
/**
* Determines the behavior of the `Queue.Strategy` when the `Queue` has empty
* slots following a `take` operation.
*/
unsafeOnQueueEmptySpace(
queue: BackingQueue,
takers: MutableQueue.MutableQueue>
): void
}
/**
* @since 1.0.0
* @category models
*/
export interface BackingQueue {
/**
* Dequeues an element from the queue.
* Returns either an element from the queue, or the `def` param.
*/
poll(def: Def): A | Def
/**
* Dequeues up to `limit` elements from the queue.
*/
pollUpTo(limit: number): Chunk.Chunk
/**
* Enqueues a collection of values into the queue.
*
* Returns a `Chunk` of the values that were **not** able to be enqueued.
*/
offerAll(elements: Iterable): Chunk.Chunk
/**
* Offers an element to the queue.
*
* Returns whether the enqueue was successful or not.
*/
offer(element: A): boolean
/**
* The **maximum** number of elements that a queue can hold.
*
* **Note**: unbounded queues can still implement this interface with
* `capacity = Infinity`.
*/
capacity(): number
/**
* Returns the number of elements currently in the queue
*/
length(): number
}
/**
* @since 1.0.0
*/
export declare namespace Queue {
/**
* @since 1.0.0
* @category models
*/
export interface EnqueueVariance {
readonly [EnqueueTypeId]: {
readonly _In: (_: A) => void
}
}
/**
* @since 1.0.0
* @category models
*/
export interface DequeueVariance {
readonly [DequeueTypeId]: {
readonly _Out: (_: never) => A
}
}
/**
* @since 1.0.0
* @category models
*/
export interface StrategyVariance {
readonly [QueueStrategyTypeId]: {
readonly _A: (_: never) => A
}
}
}
/**
* Returns `true` if the specified value is a `Queue`, `false` otherwise.
*
* @since 1.0.0
* @category refinements
*/
export const isQueue: (u: unknown) => u is Queue = internal.isQueue
/**
* Returns `true` if the specified value is a `Dequeue`, `false` otherwise.
*
* @since 1.0.0
* @category refinements
*/
export const isDequeue: (u: unknown) => u is Dequeue = internal.isDequeue
/**
* Returns `true` if the specified value is a `Enqueue`, `false` otherwise.
*
* @since 1.0.0
* @category refinements
*/
export const isEnqueue: (u: unknown) => u is Enqueue = internal.isEnqueue
/**
* @since 1.0.0
* @category strategies
*/
export const backPressureStrategy: () => Strategy = internal.backPressureStrategy
/**
* @since 1.0.0
* @category strategies
*/
export const droppingStrategy: () => Strategy = internal.droppingStrategy
/**
* @since 1.0.0
* @category strategies
*/
export const slidingStrategy: () => Strategy = internal.slidingStrategy
/**
* @since 1.0.0
* @category constructors
*/
export const make: (queue: BackingQueue, strategy: Strategy) => Effect.Effect> =
internal.make
/**
* Makes a new bounded `Queue`. When the capacity of the queue is reached, any
* additional calls to `offer` will be suspended until there is more room in
* the queue.
*
* **Note**: When possible use only power of 2 capacities; this will provide
* better performance by utilising an optimised version of the underlying
* `RingBuffer`.
*
* @since 1.0.0
* @category constructors
*/
export const bounded: (requestedCapacity: number) => Effect.Effect> = internal.bounded
/**
* Makes a new bounded `Queue` with the dropping strategy.
*
* When the capacity of the queue is reached, new elements will be dropped and the
* old elements will remain.
*
* **Note**: When possible use only power of 2 capacities; this will provide
* better performance by utilising an optimised version of the underlying
* `RingBuffer`.
*
* @since 1.0.0
* @category constructors
*/
export const dropping: (requestedCapacity: number) => Effect.Effect> = internal.dropping
/**
* Makes a new bounded `Queue` with the sliding strategy.
*
* When the capacity of the queue is reached, new elements will be added and the
* old elements will be dropped.
*
* **Note**: When possible use only power of 2 capacities; this will provide
* better performance by utilising an optimised version of the underlying
* `RingBuffer`.
*
* @since 1.0.0
* @category constructors
*/
export const sliding: (requestedCapacity: number) => Effect.Effect> = internal.sliding
/**
* Creates a new unbounded `Queue`.
*
* @since 1.0.0
* @category constructors
*/
export const unbounded: () => Effect.Effect> = internal.unbounded
/**
* Returns the number of elements the queue can hold.
*
* @since 1.0.0
* @category getters
*/
export const capacity: (self: Dequeue | Enqueue) => number = internal.capacity
/**
* Retrieves the size of the queue, which is equal to the number of elements
* in the queue. This may be negative if fibers are suspended waiting for
* elements to be added to the queue.
*
* @since 1.0.0
* @category getters
*/
export const size: (self: Dequeue | Enqueue) => Effect.Effect = internal.size
/**
* Returns `true` if the `Queue` contains zero elements, `false` otherwise.
*
* @since 1.0.0
* @category getters
*/
export const isEmpty: (self: Dequeue | Enqueue) => Effect.Effect = internal.isEmpty
/**
* Returns `true` if the `Queue` contains at least one element, `false`
* otherwise.
*
* @since 1.0.0
* @category getters
*/
export const isFull: (self: Dequeue | Enqueue) => Effect.Effect = internal.isFull
/**
* Returns `true` if `shutdown` has been called, otherwise returns `false`.
*
* @since 1.0.0
* @category getters
*/
export const isShutdown: (self: Dequeue | Enqueue) => Effect.Effect =
internal.isShutdown
/**
* Waits until the queue is shutdown. The `Effect` returned by this method will
* not resume until the queue has been shutdown. If the queue is already
* shutdown, the `Effect` will resume right away.
*
* @since 1.0.0
* @category utils
*/
export const awaitShutdown: (self: Dequeue | Enqueue) => Effect.Effect =
internal.awaitShutdown
/**
* Interrupts any fibers that are suspended on `offer` or `take`. Future calls
* to `offer*` and `take*` will be interrupted immediately.
*
* @since 1.0.0
* @category utils
*/
export const shutdown: (self: Dequeue | Enqueue) => Effect.Effect = internal.shutdown
/**
* Places one value in the queue.
*
* @since 1.0.0
* @category utils
*/
export const offer: {
(value: A): (self: Enqueue) => Effect.Effect
(self: Enqueue, value: A): Effect.Effect
} = internal.offer
/**
* Places one value in the queue.
*
* @since 1.0.0
* @category utils
*/
export const unsafeOffer: {
(value: A): (self: Enqueue) => boolean
(self: Enqueue, value: A): boolean
} = internal.unsafeOffer
/**
* For Bounded Queue: uses the `BackPressure` Strategy, places the values in
* the queue and always returns true. If the queue has reached capacity, then
* the fiber performing the `offerAll` will be suspended until there is room
* in the queue.
*
* For Unbounded Queue: Places all values in the queue and returns true.
*
* For Sliding Queue: uses `Sliding` Strategy If there is room in the queue,
* it places the values otherwise it removes the old elements and enqueues the
* new ones. Always returns true.
*
* For Dropping Queue: uses `Dropping` Strategy, It places the values in the
* queue but if there is no room it will not enqueue them and return false.
*
* @since 1.0.0
* @category utils
*/
export const offerAll: {
(iterable: Iterable): (self: Enqueue) => Effect.Effect
(self: Enqueue, iterable: Iterable): Effect.Effect
} = internal.offerAll
/**
* Returns the first value in the `Queue` as a `Some`, or `None` if the queue
* is empty.
*
* @since 1.0.0
* @category utils
*/
export const poll: (self: Dequeue) => Effect.Effect> = internal.poll
/**
* Takes the oldest value in the queue. If the queue is empty, this will return
* a computation that resumes when an item has been added to the queue.
*
* @since 1.0.0
* @category utils
*/
export const take: (self: Dequeue) => Effect.Effect = internal.take
/**
* Takes all the values in the queue and returns the values. If the queue is
* empty returns an empty collection.
*
* @since 1.0.0
* @category utils
*/
export const takeAll: (self: Dequeue) => Effect.Effect> = internal.takeAll
/**
* Takes up to max number of values from the queue.
*
* @since 1.0.0
* @category utils
*/
export const takeUpTo: {
(max: number): (self: Dequeue) => Effect.Effect>
(self: Dequeue, max: number): Effect.Effect>
} = internal.takeUpTo
/**
* Takes a number of elements from the queue between the specified minimum and
* maximum. If there are fewer than the minimum number of elements available,
* suspends until at least the minimum number of elements have been collected.
*
* @since 1.0.0
* @category utils
*/
export const takeBetween: {
(min: number, max: number): (self: Dequeue) => Effect.Effect>
(self: Dequeue, min: number, max: number): Effect.Effect>
} = internal.takeBetween
/**
* Takes the specified number of elements from the queue. If there are fewer
* than the specified number of elements available, it suspends until they
* become available.
*
* @since 1.0.0
* @category utils
*/
export const takeN: {
(n: number): (self: Dequeue) => Effect.Effect>
(self: Dequeue, n: number): Effect.Effect>
} = internal.takeN