/** * @since 2.0.0 */ import * as internal from "./internal/stm/tQueue.js" import type * as Option from "./Option.js" import type { Predicate } from "./Predicate.js" import type * as STM from "./STM.js" import type * as Types from "./Types.js" /** * @since 2.0.0 * @category symbols */ export const TDequeueTypeId: unique symbol = internal.TDequeueTypeId /** * @since 2.0.0 * @category symbols */ export type TDequeueTypeId = typeof TDequeueTypeId /** * @since 2.0.0 * @category symbols */ export const TEnqueueTypeId: unique symbol = internal.TEnqueueTypeId /** * @since 2.0.0 * @category symbols */ export type TEnqueueTypeId = typeof TEnqueueTypeId /** * @since 2.0.0 * @category models */ export interface TQueue extends TEnqueue, TDequeue {} /** * @since 2.0.0 * @category models */ export interface TEnqueue extends TQueue.TEnqueueVariance, BaseTQueue { /** * Places one value in the queue. */ offer(value: A): STM.STM /** * For Bounded TQueue: 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 TQueue: Places all values in the queue and returns true. * * For Sliding TQueue: 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 TQueue: 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): STM.STM } /** * @since 2.0.0 * @category models */ export interface TDequeue extends TQueue.TDequeueVariance, BaseTQueue { /** * Views the next element in the queue without removing it, retrying if the * queue is empty. */ readonly peek: STM.STM /** * Views the next element in the queue without removing it, returning `None` * if the queue is empty. */ readonly peekOption: STM.STM> /** * 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. */ readonly take: STM.STM /** * Takes all the values in the queue and returns the values. If the queue is * empty returns an empty collection. */ readonly takeAll: STM.STM> /** * Takes up to max number of values from the queue. */ takeUpTo(max: number): STM.STM> } /** * The base interface that all `TQueue`s must implement. * * @since 2.0.0 * @category models */ export interface BaseTQueue { /** * Returns the number of elements the queue can hold. */ capacity(): number /** * 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. */ readonly size: STM.STM /** * Returns `true` if the `TQueue` contains at least one element, `false` * otherwise. */ readonly isFull: STM.STM /** * Returns `true` if the `TQueue` contains zero elements, `false` otherwise. */ readonly isEmpty: STM.STM /** * Interrupts any fibers that are suspended on `offer` or `take`. Future calls * to `offer*` and `take*` will be interrupted immediately. */ readonly shutdown: STM.STM /** * Returns `true` if `shutdown` has been called, otherwise returns `false`. */ readonly isShutdown: STM.STM /** * Waits until the queue is shutdown. The `STM` returned by this method will * not resume until the queue has been shutdown. If the queue is already * shutdown, the `STM` will resume right away. */ readonly awaitShutdown: STM.STM } /** * @since 2.0.0 */ export declare namespace TQueue { /** * @since 2.0.0 * @category models */ export interface TEnqueueVariance { readonly [TEnqueueTypeId]: { readonly _In: Types.Contravariant } } /** * @since 2.0.0 * @category models */ export interface TDequeueVariance { readonly [TDequeueTypeId]: { readonly _Out: Types.Covariant } } } /** * Returns `true` if the specified value is a `TQueue`, `false` otherwise. * * @since 2.0.0 * @category refinements */ export const isTQueue: (u: unknown) => u is TQueue = internal.isTQueue /** * Returns `true` if the specified value is a `TDequeue`, `false` otherwise. * * @since 2.0.0 * @category refinements */ export const isTDequeue: (u: unknown) => u is TDequeue = internal.isTDequeue /** * Returns `true` if the specified value is a `TEnqueue`, `false` otherwise. * * @since 2.0.0 * @category refinements */ export const isTEnqueue: (u: unknown) => u is TEnqueue = internal.isTEnqueue /** * Waits until the queue is shutdown. The `STM` returned by this method will * not resume until the queue has been shutdown. If the queue is already * shutdown, the `STM` will resume right away. * * @since 2.0.0 * @category mutations */ export const awaitShutdown: (self: TDequeue | TEnqueue) => STM.STM = internal.awaitShutdown /** * Creates a bounded queue with the back pressure strategy. The queue will * retain values until they have been taken, applying back pressure to * offerors if the queue is at capacity. * * For best performance use capacities that are powers of two. * * @since 2.0.0 * @category constructors */ export const bounded: (requestedCapacity: number) => STM.STM> = internal.bounded /** * Returns the number of elements the queue can hold. * * @since 2.0.0 * @category getters */ export const capacity: (self: TDequeue | TEnqueue) => number = internal.capacity /** * Creates a bounded queue with the dropping strategy. The queue will drop new * values if the queue is at capacity. * * For best performance use capacities that are powers of two. * * @since 2.0.0 * @category constructors */ export const dropping: (requestedCapacity: number) => STM.STM> = internal.dropping /** * Returns `true` if the `TQueue` contains zero elements, `false` otherwise. * * @since 2.0.0 * @category getters */ export const isEmpty: (self: TDequeue | TEnqueue) => STM.STM = internal.isEmpty /** * Returns `true` if the `TQueue` contains at least one element, `false` * otherwise. * * @since 2.0.0 * @category getters */ export const isFull: (self: TDequeue | TEnqueue) => STM.STM = internal.isFull /** * Returns `true` if `shutdown` has been called, otherwise returns `false`. * * @since 2.0.0 * @category getters */ export const isShutdown: (self: TDequeue | TEnqueue) => STM.STM = internal.isShutdown /** * Places one value in the queue. * * @since 2.0.0 * @category mutations */ export const offer: { /** * Places one value in the queue. * * @since 2.0.0 * @category mutations */ (value: A): (self: TEnqueue) => STM.STM /** * Places one value in the queue. * * @since 2.0.0 * @category mutations */ (self: TEnqueue, value: A): STM.STM } = internal.offer /** * For Bounded TQueue: 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 TQueue: Places all values in the queue and returns true. * * For Sliding TQueue: 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 TQueue: 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 2.0.0 * @category mutations */ export const offerAll: { /** * For Bounded TQueue: 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 TQueue: Places all values in the queue and returns true. * * For Sliding TQueue: 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 TQueue: 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 2.0.0 * @category mutations */ (iterable: Iterable): (self: TEnqueue) => STM.STM /** * For Bounded TQueue: 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 TQueue: Places all values in the queue and returns true. * * For Sliding TQueue: 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 TQueue: 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 2.0.0 * @category mutations */ (self: TEnqueue, iterable: Iterable): STM.STM } = internal.offerAll /** * Views the next element in the queue without removing it, retrying if the * queue is empty. * * @since 2.0.0 * @category getters */ export const peek: (self: TDequeue) => STM.STM = internal.peek /** * Views the next element in the queue without removing it, returning `None` * if the queue is empty. * * @since 2.0.0 * @category getters */ export const peekOption: (self: TDequeue) => STM.STM> = internal.peekOption /** * Takes a single element from the queue, returning `None` if the queue is * empty. * * @since 2.0.0 * @category getters */ export const poll: (self: TDequeue) => STM.STM> = internal.poll /** * Drops elements from the queue while they do not satisfy the predicate, * taking and returning the first element that does satisfy the predicate. * Retries if no elements satisfy the predicate. * * @since 2.0.0 * @category mutations */ export const seek: { /** * Drops elements from the queue while they do not satisfy the predicate, * taking and returning the first element that does satisfy the predicate. * Retries if no elements satisfy the predicate. * * @since 2.0.0 * @category mutations */ (predicate: Predicate): (self: TDequeue) => STM.STM /** * Drops elements from the queue while they do not satisfy the predicate, * taking and returning the first element that does satisfy the predicate. * Retries if no elements satisfy the predicate. * * @since 2.0.0 * @category mutations */ (self: TDequeue, predicate: Predicate): STM.STM } = internal.seek /** * Interrupts any fibers that are suspended on `offer` or `take`. Future calls * to `offer*` and `take*` will be interrupted immediately. * * @since 2.0.0 * @category mutations */ export const shutdown: (self: TDequeue | TEnqueue) => STM.STM = internal.shutdown /** * 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 2.0.0 * @category getters */ export const size: (self: TDequeue | TEnqueue) => STM.STM = internal.size /** * Creates a bounded queue with the sliding strategy. The queue will add new * values and drop old values if the queue is at capacity. * * For best performance use capacities that are powers of two. * * @since 2.0.0 * @category constructors */ export const sliding: (requestedCapacity: number) => STM.STM> = internal.sliding /** * 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 2.0.0 * @category mutations */ export const take: (self: TDequeue) => STM.STM = internal.take /** * Takes all the values in the queue and returns the values. If the queue is * empty returns an empty collection. * * @since 2.0.0 * @category mutations */ export const takeAll: (self: TDequeue) => STM.STM> = internal.takeAll /** * 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, * retries until at least the minimum number of elements have been collected. * * @since 2.0.0 * @category mutations */ export const takeBetween: { /** * 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, * retries until at least the minimum number of elements have been collected. * * @since 2.0.0 * @category mutations */ (min: number, max: number): (self: TDequeue) => STM.STM> /** * 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, * retries until at least the minimum number of elements have been collected. * * @since 2.0.0 * @category mutations */ (self: TDequeue, min: number, max: number): STM.STM> } = internal.takeBetween /** * Takes the specified number of elements from the queue. If there are fewer * than the specified number of elements available, it retries until they * become available. * * @since 2.0.0 * @category mutations */ export const takeN: { /** * Takes the specified number of elements from the queue. If there are fewer * than the specified number of elements available, it retries until they * become available. * * @since 2.0.0 * @category mutations */ (n: number): (self: TDequeue) => STM.STM> /** * Takes the specified number of elements from the queue. If there are fewer * than the specified number of elements available, it retries until they * become available. * * @since 2.0.0 * @category mutations */ (self: TDequeue, n: number): STM.STM> } = internal.takeN /** * Takes up to max number of values from the queue. * * @since 2.0.0 * @category mutations */ export const takeUpTo: { /** * Takes up to max number of values from the queue. * * @since 2.0.0 * @category mutations */ (max: number): (self: TDequeue) => STM.STM> /** * Takes up to max number of values from the queue. * * @since 2.0.0 * @category mutations */ (self: TDequeue, max: number): STM.STM> } = internal.takeUpTo /** * Creates an unbounded queue. * * @since 2.0.0 * @category constructors */ export const unbounded: () => STM.STM> = internal.unbounded