/** * Generic backed queue interface. (Base classes) */ import type { SimpleStoreCRUDSupportTransaction } from "../databases/SimpleStoreBase"; import { type QueueKeyOnProcess, type RequestPositions, type QueueKey, type QueryOptions, type DequeueOptions, type DequeuedItem, type DequeueCallback } from "./BackedQueueTypes"; import { QueueBackendTransaction, QueueBackendWithTransaction } from "./QueueBackendTypes"; /** * Abstract base class for a backed queue. * We employ simple in-memory tracking for primary implementation purposes. * Operations prior to commit are non-atomic, must be idempotent, and must avoid side effects until commit completes. * @template T Type of items in the queue. */ export declare abstract class BackedQueue { /** * Backend store for the queue. */ protected abstract backend: QueueBackendWithTransaction; /** * Promise that resolves when the queue is ready. */ protected readyPromise: Promise; get isReady(): Promise; /** * Add an item to the dead letter queue. * @param key Key of the item being added to the dead letter queue. * @param item Item being added to the dead letter queue. */ protected abstract addDeadLetter(key: QueueKeyOnProcess, item: T): Promise; /** * Commit a processed item. * @param key Key of the item to commit. */ commit(key: QueueKeyOnProcess): Promise; /** * Requeue an item. * @param key Key of the item to requeue. * @param position Target position for the requeued item. */ requeue(key: QueueKeyOnProcess, position?: RequestPositions): Promise; /** * Revoke an item from queue or processing. * If the item is in the queue, we remove it. * In-flight items are marked revoked and are not committed. Call this when processing determines removal. * @param key Key of the item to revoke. */ revoke(key: QueueKeyOnProcess | QueueKey): Promise; /** * Name of the queue. */ protected readonly _name: string; /** * Construct a new BackedQueue instance. * @param name Queue name. */ constructor(name: string); /** * Handle an item added to the dead letter queue. * @param key Key of the item added to the dead letter queue. * @param item Item added to the dead letter queue. * @param reason Reason for dead lettering. */ protected onDeadLetter(key: QueueKeyOnProcess, item: T, reason: Error): Promise; private _enqueueWaiters; private _dequeueWaiters; /** * Prepare a waiter with query options. * @param options Query options. * @returns Promise with resolvers and a release function. */ private handleQueryOptions; /** * Wait for the next enqueue. * @param options Query options. */ protected waitNextEnqueued(options?: QueryOptions): Promise; /** * Wait for the next dequeue. * @param options Query options. */ protected waitNextDequeue(options?: QueryOptions): Promise; /** * Notify that an item has been enqueued. * We allow spurious wake-ups for robustness. */ protected onEnqueued(): void; /** * Notify that an item has been dequeued. */ protected onDequeued(): void; /** * Move an item from queue to processing. * @param tx Backend store transaction. * @param key Key of the item to move. */ protected _queueToProcessing(tx: QueueBackendTransaction, key: QueueKey): Promise; /** * Restore an item from processing back to the queue. * @param key Key of the item to restore. * @returns Promise that resolves when complete. */ protected restoreToQueue(key: QueueKeyOnProcess): Promise; /** * Check if the queue is empty. * @returns True if empty, false otherwise. */ isQueueEmpty(): Promise; /** * Check if any items are in flight. * @returns True if at least one item is in flight, false otherwise. */ isAnyInFlight(): Promise; /** * Wait until at least one item is available. * @param options Query options (same shape as DequeueOptions, without dequeue). */ protected waitForNext(options?: DequeueOptions): Promise; /** * Dequeue one item. * While processing, items are in-flight and remain locked until committed, requeued, or revoked. * In-flight state is implementation dependent and volatile. * @param options Dequeue options. */ private __dequeue; /** * Dequeue an item. * @param options Dequeue options. * @returns Dequeued item. */ dequeue(options?: DequeueOptions): Promise>; /** * Enqueue an item. * @param item Item to enqueue. * @returns Key of the enqueued item. */ enqueue(item: T): Promise; /** * Dequeue and process an item via a callback. * @param options Dequeue options. * @param callback Processing callback. */ process(options: DequeueOptions, callback: DequeueCallback): Promise; /** * Wait until the queue becomes empty. * @param options Query options. */ waitForEmptyQueue(options?: QueryOptions): Promise; } /** * Persistent backed queue base that tracks in-flight processing keys. * Suitable for multi-worker scenarios. * @template T Type of items in the queue. */ export declare abstract class PersistentIDBBackedQueueBase extends BackedQueue { backend: QueueBackendWithTransaction; /** * Base prefix for the queue. */ abstract get basePrefix(): string; /** * Initialise the queue with storage. * @param storage Storage backend. */ init(storage: SimpleStoreCRUDSupportTransaction): Promise; /** * Construct a new PersistentIDBackedQueueBase instance. * @param name Queue name. * @param storage Storage backend. */ constructor(name: string, storage: SimpleStoreCRUDSupportTransaction); addDeadLetter(key: QueueKeyOnProcess, item: T): Promise; dequeue(options?: DequeueOptions): Promise>; enqueue(item: T): Promise; process(options: DequeueOptions, callback: DequeueCallback): Promise; waitForEmptyQueue(options?: QueryOptions): Promise; }