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