UNPKG

buflux

Version:

A high-performance, event-driven buffer library for Node.js and browsers, with configurable overflow strategies

78 lines (77 loc) 2.49 kB
import { EventEmitter } from './event-emitter'; import { OverflowMode } from '../types/overflow-mode.enum'; /** Configuration options for creating a Buflux instance */ interface BufluxOptions { /** Maximum number of items the buffer can hold */ capacity: number; /** Strategy to handle overflow when buffer is full */ overflow?: OverflowMode; } /** * A fixed-capacity buffer with configurable overflow strategies * @template T The type of items stored in the buffer * @extends EventEmitter<T> */ export declare class Buflux<T> extends EventEmitter<T> { private buffer; private readonly capacity; private readonly overflowMode; private overflowHandlers; private overflowHandler; /** * Creates a new Buflux instance * @param {BufluxOptions} options - Configuration options * @throws {Error} If capacity is not a positive integer */ constructor(options: BufluxOptions); /** * Initializes handlers for different overflow modes * @private */ private initializeOverflowHandlers; /** * Adds an item to the buffer * @param {T} item - Item to add * @returns {boolean} True if item was added successfully, false otherwise * @emits enqueue When item is added successfully * @emits overflow When an item is evicted (in EVICT mode) * @emits reject When item is rejected (in REJECT mode) */ enqueue(item: T): boolean; /** * Removes and returns the oldest item from the buffer * @returns {T | undefined} The removed item, or undefined if buffer is empty * @emits dequeue When an item is removed */ dequeue(): T | undefined; /** * Checks if the buffer is at capacity * @returns {boolean} True if buffer is full */ isFull(): boolean; /** * Checks if the buffer has no items * @returns {boolean} True if buffer is empty */ isEmpty(): boolean; /** * Gets the current number of items in the buffer * @returns {number} Current buffer size */ size(): number; /** * Returns the oldest item without removing it * @returns {T | undefined} The oldest item, or undefined if buffer is empty */ peek(): T | undefined; /** * Removes all items from the buffer */ clear(): void; /** * Returns a copy of the buffer contents as an array * @returns {T[]} Array containing buffer items in order */ toArray(): T[]; } export {};