All files index.ts

97.95% Statements 96/98
91.3% Branches 21/23
100% Functions 23/23
100% Lines 93/93

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400                                              5x     63x 63x 63x     63x 63x 63x 63x 63x   63x             63x 2x       61x 61x 61x     61x 61x             148495x 148495x 148495x             148484x   148484x 148484x 148484x 148484x 148484x               24482x 22x   24482x 24482x             24456x 24456x 24456x 24456x                     148497x 1x         148496x     12902x 12902x       12902x 1x         148495x       148495x 11555x 11555x 11555x                     148535x     11580x 11580x           148535x 51x       148484x       148484x 12901x 12901x 12901x     148484x                   38x       38x 25x 25x   38x       38x 1x 1x   38x                   101028x               7x               3x               4x               3x               2x               1x                                                     15x 10063x 10063x   15x   10048x                                       2x 2x                                                     2x                                                 3x 3x 12x   3x                                   6x 6x 16x 16x 15x   1x     6x         5x
/**
 * AsyncQueue - A TypeScript implementation of an async producer-consumer queue with backpressure control
 * Uses circular buffer for O(1) enqueue/dequeue operations
 *
 * Developed by AI HiveĀ® at O2.services
 * https://o2.services
 *
 * Copyright (c) 2024 AI HiveĀ® at O2.services
 * Licensed under the MIT License
 */
 
/**
 * Promise resolver function type
 */
type PromiseResolver = () => void;
 
/**
 * AsyncQueue provides a thread-safe producer-consumer queue with backpressure control,
 * similar to .NET's Channel<T> or Go channels.
 * Uses a circular buffer for optimal performance.
 *
 * @template T The type of items in the queue
 */
export class AsyncQueue<T = any> {
  private readonly maxSize: number;
  private readonly buffer: (T | undefined)[];
  private head = 0;  // Index where we dequeue from
  private tail = 0;  // Index where we enqueue to
  private count = 0; // Number of items in queue
 
  // Waiting queues with reserved capacity - never shrink, only grow
  private waitingConsumers: (PromiseResolver | undefined)[] = [];
  private waitingConsumersCount = 0;
  private waitingProducers: (PromiseResolver | undefined)[] = [];
  private waitingProducersCount = 0;
  private readonly INITIAL_WAITING_CAPACITY = 16;
 
  private closed = false;
 
  /**
   * Creates a new AsyncQueue instance
   * @param maxSize Maximum number of items the queue can hold before producers block (default: 1)
   */
  constructor(maxSize = 1) {
    if (maxSize < 1) {
      throw new Error('maxSize must be at least 1');
    }
    // Use power of 2 for faster modulo operation with bitwise AND
    // Round up to nearest power of 2
    this.maxSize = maxSize;
    const bufferSize = 1 << (32 - Math.clz32(maxSize - 1));
    this.buffer = new Array(bufferSize);
 
    // Pre-allocate initial capacity for waiting queues
    this.waitingConsumers.length = this.INITIAL_WAITING_CAPACITY;
    this.waitingProducers.length = this.INITIAL_WAITING_CAPACITY;
  }
 
  /**
   * Adds an item to the circular buffer
   */
  private addToBuffer(item: T): void {
    this.buffer[this.tail] = item;
    this.tail = (this.tail + 1) & (this.buffer.length - 1);
    this.count++;
  }
 
  /**
   * Removes an item from the circular buffer
   */
  private removeFromBuffer(): T | undefined {
    Iif (this.count === 0) return undefined;
 
    const item = this.buffer[this.head];
    this.buffer[this.head] = undefined; // Help GC
    this.head = (this.head + 1) & (this.buffer.length - 1);
    this.count--;
    return item;
  }
 
  /**
   * Pushes a resolver onto a waiting queue with capacity management
   */
  private pushWaiter(queue: (PromiseResolver | undefined)[], count: number, resolver: PromiseResolver): number {
    // Grow capacity if needed (double the size)
    if (count >= queue.length) {
      queue.length = queue.length * 2;
    }
    queue[count] = resolver;
    return count + 1;
  }
 
  /**
   * Pops a resolver from a waiting queue (LIFO)
   */
  private popWaiter(queue: (PromiseResolver | undefined)[], count: number): PromiseResolver | undefined {
    Iif (count === 0) return undefined;
    const resolver = queue[count - 1];
    queue[count - 1] = undefined; // Help GC
    return resolver;
  }
 
  /**
   * Adds an item to the queue. Blocks if the queue is full.
   * @param item The item to add to the queue
   * @returns A promise that resolves when the item has been added
   * @throws Error if the queue has been closed
   */
  async enqueue(item: T): Promise<void> {
    // Prevent new items after close() to ensure clean shutdown
    if (this.closed) {
      throw new Error('Queue is closed');
    }
 
    // BLOCKING MECHANISM: Wait if queue is at capacity
    // This implements backpressure - fast producers slow down to match consumers
    while (this.count >= this.maxSize && !this.closed) {
      // Create unresolved Promise, store only the resolve function
      // This suspends the producer until a consumer makes space
      await new Promise<void>(resolve => {
        this.waitingProducersCount = this.pushWaiter(this.waitingProducers, this.waitingProducersCount, resolve);
      });
 
      // Check again after waking - queue might have been closed while waiting
      if (this.closed) {
        throw new Error('Queue is closed');
      }
    }
 
    // Add item to circular buffer (we now have space)
    this.addToBuffer(item);
 
    // WAKE MECHANISM: If any consumer is waiting for an item, wake ONE
    // Uses LIFO (stack) for O(1) performance - order doesn't affect correctness
    if (this.waitingConsumersCount > 0) {
      const consumer = this.popWaiter(this.waitingConsumers, this.waitingConsumersCount);
      this.waitingConsumersCount--;
      consumer?.(); // Calling resolve() wakes the awaiting consumer
    }
  }
 
  /**
   * Removes and returns the oldest item from the queue. Blocks if the queue is empty.
   * @returns A promise that resolves to the item, or undefined if the queue is closed and empty
   */
  async dequeue(): Promise<T | undefined> {
    // BLOCKING MECHANISM: Wait if queue is empty
    // Consumers block here until producers provide items or queue closes
    while (this.count === 0 && !this.closed) {
      // Create unresolved Promise, store only the resolve function
      // This suspends the consumer until a producer adds an item
      await new Promise<void>(resolve => {
        this.waitingConsumersCount = this.pushWaiter(this.waitingConsumers, this.waitingConsumersCount, resolve);
      });
    }
 
    // After waking/looping, check if we exited due to close (not an item)
    // Return undefined to signal "end of stream" to consumers
    if (this.count === 0 && this.closed) {
      return undefined;
    }
 
    // Remove and get the oldest item from circular buffer (FIFO order)
    const item = this.removeFromBuffer();
 
    // WAKE MECHANISM: If any producer is waiting for space, wake ONE
    // Uses LIFO (stack) for O(1) performance - order doesn't affect correctness
    if (this.waitingProducersCount > 0) {
      const producer = this.popWaiter(this.waitingProducers, this.waitingProducersCount);
      this.waitingProducersCount--;
      producer?.(); // Calling resolve() wakes the awaiting producer
    }
 
    return item;
  }
 
  /**
   * Signals that no more items will be added to the queue.
   * Existing items can still be consumed.
   */
  close(): void {
    // Signal that no more items will be added
    // Existing items can still be consumed
    this.closed = true;
 
    // Wake ALL waiting consumers - they'll return undefined
    // This allows graceful shutdown where all consumers exit cleanly
    for (let i = 0; i < this.waitingConsumersCount; i++) {
      this.waitingConsumers[i]?.();
      this.waitingConsumers[i] = undefined; // Help GC
    }
    this.waitingConsumersCount = 0;
 
    // Wake ALL waiting producers - they'll throw an error
    // This prevents deadlock where producers wait forever
    for (let i = 0; i < this.waitingProducersCount; i++) {
      this.waitingProducers[i]?.();
      this.waitingProducers[i] = undefined; // Help GC
    }
    this.waitingProducersCount = 0;
  }
 
  /**
   * Checks if the queue is closed AND empty
   * @returns true if the queue is closed and has no remaining items
   */
  get isClosed(): boolean {
    // Queue is "fully closed" only when closed AND empty
    // This allows consumers to drain remaining items after close()
    return this.closed && this.count === 0;
  }
 
  /**
   * Gets the current number of items in the queue
   * @returns The number of items currently in the queue
   */
  get size(): number {
    return this.count;
  }
 
  /**
   * Gets the maximum size of the queue
   * @returns The maximum number of items the queue can hold
   */
  get capacity(): number {
    return this.maxSize;
  }
 
  /**
   * Checks if the queue is at full capacity
   * @returns true if the queue is full
   */
  get isFull(): boolean {
    return this.count >= this.maxSize;
  }
 
  /**
   * Checks if the queue is empty
   * @returns true if the queue has no items
   */
  get isEmpty(): boolean {
    return this.count === 0;
  }
 
  /**
   * Gets the number of waiting consumers
   * @returns The number of consumers waiting for items
   */
  get waitingConsumerCount(): number {
    return this.waitingConsumersCount;
  }
 
  /**
   * Gets the number of waiting producers
   * @returns The number of producers waiting for space
   */
  get waitingProducerCount(): number {
    return this.waitingProducersCount;
  }
 
  /**
   * Returns an async iterator for consuming items from the queue.
   * Allows the queue to be used with for-await-of loops.
   * The iterator will complete when the queue is closed and empty.
   *
   * @example
   * ```typescript
   * const queue = new AsyncQueue<number>();
   *
   * // Producer
   * setTimeout(async () => {
   *   for (let i = 0; i < 5; i++) {
   *     await queue.enqueue(i);
   *   }
   *   queue.close();
   * }, 0);
   *
   * // Consumer using async iterator
   * for await (const item of queue) {
   *   console.log(item); // 0, 1, 2, 3, 4
   * }
   * ```
   */
  async *[Symbol.asyncIterator](): AsyncIterator<T> {
    while (true) {
      const item = await this.dequeue();
      if (item === undefined) {
        // dequeue returns undefined only when queue is closed and empty
        break;
      }
      yield item;
    }
  }
 
  /**
   * Creates an async iterable that consumes items from the queue.
   * This is an alternative way to get an async iterator.
   *
   * @returns An async iterable for consuming queue items
   * @example
   * ```typescript
   * const queue = new AsyncQueue<string>();
   * const iterator = queue.iterate();
   *
   * for await (const item of iterator) {
   *   console.log(item);
   * }
   * ```
   */
  iterate(): AsyncIterable<T> {
    return {
      [Symbol.asyncIterator]: () => this[Symbol.asyncIterator]()
    };
  }
 
  /**
   * Converts the queue to an async generator.
   * Useful for transformation pipelines.
   *
   * @returns An async generator that yields items from the queue
   * @example
   * ```typescript
   * const queue = new AsyncQueue<number>();
   * const generator = queue.toAsyncGenerator();
   *
   * // Transform items
   * async function* double(source: AsyncGenerator<number>) {
   *   for await (const item of source) {
   *     yield item * 2;
   *   }
   * }
   *
   * for await (const item of double(generator)) {
   *   console.log(item);
   * }
   * ```
   */
  async *toAsyncGenerator(): AsyncGenerator<T> {
    yield* this;
  }
 
  /**
   * Drains all items from the queue into an array.
   * Waits until the queue is closed and returns all items.
   *
   * @returns A promise that resolves to an array of all items
   * @example
   * ```typescript
   * const queue = new AsyncQueue<number>();
   *
   * // Producer
   * (async () => {
   *   for (let i = 0; i < 5; i++) {
   *     await queue.enqueue(i);
   *   }
   *   queue.close();
   * })();
   *
   * const items = await queue.drain();
   * console.log(items); // [0, 1, 2, 3, 4]
   * ```
   */
  async drain(): Promise<T[]> {
    const items: T[] = [];
    for await (const item of this) {
      items.push(item);
    }
    return items;
  }
 
  /**
   * Takes up to n items from the queue.
   * Returns early if the queue is closed before n items are received.
   *
   * @param n The maximum number of items to take
   * @returns A promise that resolves to an array of items
   * @example
   * ```typescript
   * const queue = new AsyncQueue<number>();
   *
   * // Take first 3 items
   * const items = await queue.take(3);
   * ```
   */
  async take(n: number): Promise<T[]> {
    const items: T[] = [];
    for (let i = 0; i < n && !this.isClosed; i++) {
      const item = await this.dequeue();
      if (item !== undefined) {
        items.push(item);
      } else {
        break;
      }
    }
    return items;
  }
}
 
// Default export for CommonJS compatibility
export default AsyncQueue;