import * as plugins from './lik.plugins.js'; export class BackpressuredArray { private data: T[]; private highWaterMark: number; public hasSpace: plugins.smartrx.rxjs.Subject; constructor(highWaterMark: number = 16) { this.data = []; this.highWaterMark = highWaterMark; this.hasSpace = new plugins.smartrx.rxjs.Subject(); } push(item: T): boolean { this.data.push(item); const spaceAvailable = this.checkSpaceAvailable(); if (spaceAvailable) { this.hasSpace.next(); } return spaceAvailable } shift(): T | undefined { const item = this.data.shift(); if (this.checkSpaceAvailable()) { this.hasSpace.next(); } return item; } checkSpaceAvailable(): boolean { return this.data.length < this.highWaterMark; } waitForSpace(): Promise { return new Promise((resolve) => { if (this.checkSpaceAvailable()) { resolve(); } else { const subscription = this.hasSpace.subscribe(() => { subscription.unsubscribe(); resolve(); }); } }); } }