simple-in-memory-queue
Version:
A simple in-memory queue, for nodejs and the browser, with consumers for common usecases.
48 lines (47 loc) • 1.34 kB
TypeScript
import { EventStream } from 'event-stream-pubsub';
import { QueueOrder } from '../../domain/constants';
export interface Queue<T> {
/**
* add one or more items into the queue
*/
push: (item: T | T[]) => void;
/**
* view the top items from the queue, without removing them
*
* note
* - `.peek() === .peek(1) === .peek(0, 1)` -> view the top item
* - `.peek(2) === .peek(0, 2)` -> view the top 2 items
* - `.peek(2, 3)` -> view the top 2nd and 3rd items
*/
peek: (start?: number, end?: number) => T[];
/**
* remove the top items from the queue
*
* note
* - `.pop() === .pop(1) === .pop(0, 1)` -> remove the top item
* - `.pop(2) === .pop(0, 2)` -> remove the top 2 items
* - `.pop(2, 3)` -> remove the top 2nd and 3rd items
*/
pop: (start?: number, end?: number) => T[];
/**
* the current number of items in the queue
*/
length: number;
/**
* event streams that can be subscribed to
*/
on: {
push: Omit<EventStream<{
items: T[];
}>, 'publish'>;
peek: EventStream<{
items: T[];
}>;
pop: EventStream<{
items: T[];
}>;
};
}
export declare const createQueue: <T>({ order }: {
order: QueueOrder;
}) => Queue<T>;