{"version":3,"file":"index.cjs","names":[],"sources":["../../src/promise-queue/index.ts"],"sourcesContent":["/**\n * @deprecated Not used by the engine; CircularBuffer is the block queue. Kept for compatibility, removed in 3.0.\n */\nexport class PromiseQueue<T> {\n  /** Optional error handler for enqueue failures */\n  private errorHandler?: (error: unknown) => void;\n  /** Array of promises representing queued items */\n  private items: Array<Promise<T>>;\n\n  /** Function to resolve the next enqueued promise */\n  private enqueuer!: (val: T | PromiseLike<T>) => void;\n\n  /** Function to resolve the continue promise when space is available */\n  private batcher!: (val: boolean) => void;\n\n  /** Flag indicating if the queue is synced with the data source */\n  public synced = false;\n\n  /** Maximum number of items to keep in the queue */\n  private batchSize: number;\n\n  /** Promise that resolves when it's safe to enqueue more items */\n  private continuePromise: Promise<boolean>;\n\n  constructor(batchSize: number, errorHandler?: (error: unknown) => void) {\n    const nextVal = new Promise<T>((resolve, _reject) => {\n      this.enqueuer = resolve;\n    });\n    this.batchSize = batchSize;\n    this.errorHandler = errorHandler;\n    this.continuePromise = new Promise<boolean>((resolve, _reject) => {\n      this.batcher = resolve;\n    });\n    this.batcher(true);\n    this.items = [nextVal];\n  }\n\n  enqueue(item: T | PromiseLike<T>) {\n    try {\n      this.enqueuer(item);\n      const nextVal = new Promise<T>((resolve, _reject) => {\n        this.enqueuer = resolve;\n      });\n      this.items.push(nextVal);\n      if (this.size() > this.batchSize) {\n        this.continuePromise = new Promise<boolean>((resolve, _reject) => {\n          this.batcher = resolve;\n        });\n      }\n    }\n    catch (e) {\n      if (this.errorHandler) {\n        this.errorHandler(e);\n      }\n      // If no error handler provided, silently ignore (existing behavior for backward compatibility)\n    }\n  }\n\n  clear() {\n    const nextVal = new Promise<T>((resolve, _reject) => {\n      this.enqueuer = resolve;\n    });\n    this.continuePromise = new Promise<boolean>((resolve, _reject) => {\n      this.batcher = resolve;\n    });\n    this.batcher(true);\n    this.items = [nextVal];\n  }\n\n  dequeue() {\n    const item = this.items.shift();\n    if (this.size() <= this.batchSize) {\n      this.batcher(true);\n    }\n\n    return item as Promise<T>;\n  }\n\n  continue() {\n    return this.continuePromise;\n  }\n\n  setSynced() {\n    this.synced = true;\n  }\n\n  isEmpty() {\n    if (this.items.length == 0) {\n      return true;\n    }\n    else {\n      return false;\n    }\n  }\n\n  size() {\n    return this.items.length;\n  }\n}\n\n/**\n * Circular buffer implementation using promises for efficient memory usage\n * Reuses array slots in a circular fashion to maintain constant memory footprint\n * Used by the indexer for managing block processing queues\n */\nexport class CircularBuffer<T> {\n  /** Optional error handler for enqueue failures */\n  private errorHandler?: (error: unknown) => void;\n\n  /** Fixed-size array of promises representing buffered items */\n  private items: Array<Promise<T>>;\n\n  /** Function to resolve the next enqueued promise */\n  private enqueuer!: (val: T | PromiseLike<T>) => void;\n\n  /** Current number of items in the buffer */\n  private count: number = 0;\n\n  /** Index of the next item to dequeue */\n  private next: number = 0;\n\n  /** Function to resolve the continue promise when space is available */\n  private batcher!: (val: boolean) => void;\n\n  /** Flag indicating if the buffer is synced with the data source */\n  public synced = false;\n\n  /** Maximum number of items the buffer can hold */\n  private batchSize: number;\n\n  /** Promise that resolves when it's safe to enqueue more items */\n  private continuePromise: Promise<boolean>;\n\n  /** Index of the next available slot for enqueueing */\n  private nextAvail: number = 0;\n\n  constructor(batchSize: number, errorHandler?: (error: unknown) => void) {\n    if (batchSize <= 1) {\n      throw new Error(\"Batch size must be greater than 1\");\n    }\n    const nextVal = new Promise<T>((resolve, _reject) => {\n      this.enqueuer = resolve;\n    });\n    this.items = new Array<Promise<T>>(batchSize);\n    this.batchSize = batchSize;\n    this.errorHandler = errorHandler;\n    this.items[this.nextAvail] = nextVal;\n    this.count = 1;\n    this.nextAvail++;\n    if (this.nextAvail >= this.batchSize) {\n      this.nextAvail = 0;\n    }\n\n    this.continuePromise = new Promise<boolean>((resolve, _reject) => {\n      this.batcher = resolve;\n    });\n    this.batcher(true);\n  }\n\n  enqueue(item: T | PromiseLike<T>) {\n    if (this.count >= this.batchSize) {\n      // Overwriting would silently drop the oldest unprocessed block; callers must await continue()\n      const error = new Error(\"CircularBuffer is full (\" + this.batchSize + \" slots); await continue() before enqueueing\");\n      if (this.errorHandler) {\n        this.errorHandler(error);\n        return;\n      }\n      throw error;\n    }\n    try {\n      this.enqueuer(item);\n      const nextVal = new Promise<T>((resolve, _reject) => {\n        this.enqueuer = resolve;\n      });\n      this.items[this.nextAvail] = nextVal;\n      this.count++;\n      this.nextAvail++;\n      if (this.nextAvail >= this.batchSize) {\n        this.nextAvail = 0;\n      }\n\n      if (this.nextAvail == this.next) {\n        this.continuePromise = new Promise<boolean>((resolve, _reject) => {\n          this.batcher = resolve;\n        });\n      }\n    }\n    catch (e) {\n      if (this.errorHandler) {\n        this.errorHandler(e);\n      }\n      // If no error handler provided, silently ignore (existing behavior for backward compatibility)\n    }\n  }\n\n  dequeue() {\n    if (this.count <= 0) {\n      // The only pending slot (the sentinel) has already been handed out\n      throw new Error(\"CircularBuffer is empty; the previous dequeue() has not been fulfilled yet\");\n    }\n    const item = this.items[this.next];\n    this.next++;\n    this.count--;\n    if (this.next >= this.batchSize) {\n      this.next = 0;\n    }\n    if (this.count <= this.batchSize) {\n      this.batcher(true);\n    }\n    return item;\n  }\n\n  clear() {\n    this.next = 0;\n    this.nextAvail = 0;\n    const nextVal = new Promise<T>((resolve, _reject) => {\n      this.enqueuer = resolve;\n    });\n    this.items = new Array<Promise<T>>(this.batchSize);\n    this.items[this.nextAvail] = nextVal;\n    this.synced = false;\n    this.count = 1;\n    this.nextAvail++;\n    if (this.nextAvail >= this.batchSize) {\n      this.nextAvail = 0;\n    }\n\n    this.continuePromise = new Promise<boolean>((resolve, _reject) => {\n      this.batcher = resolve;\n    });\n    this.batcher(true);\n  }\n\n  continue() {\n    return this.continuePromise;\n  }\n\n  setSynced() {\n    this.synced = true;\n  }\n\n  isEmpty() {\n    if (this.count == 0) {\n      return true;\n    }\n    else {\n      return false;\n    }\n  }\n\n  size() {\n    return this.count;\n  }\n}\n"],"mappings":";;;;;AAGA,IAAa,eAAb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