UNPKG

websocket-ts

Version:

<div> <div align="center"> <img src="https://raw.githubusercontent.com/jjxxs/websocket-ts/gh-pages/websocket-ts-logo.svg" alt="websocket-ts" width="300" height="65" /> </div> <p align="center"> <img src="https://github.com/jjxxs/websocket-ts

42 lines (32 loc) 778 B
import { Queue } from "./queue"; /** * An array queue is a queue that has an unbounded capacity. Reading from an array queue * will return the oldest element and effectively remove it from the queue. */ export class ArrayQueue<E> implements Queue<E> { private readonly elements: E[]; constructor() { this.elements = []; } add(element: E): void { this.elements.push(element); } clear() { this.elements.length = 0; } forEach(fn: (element: E) => unknown) { this.elements.forEach(fn); } length(): number { return this.elements.length; } isEmpty(): boolean { return this.elements.length === 0; } peek(): E | undefined { return this.elements[0]; } read(): E | undefined { return this.elements.shift(); } }