lik/ts/classes.backpressuredarray.ts

63 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-02-25 12:01:06 +00:00
import * as plugins from './classes.plugins.js';
2023-11-13 13:40:01 +00:00
export class BackpressuredArray<T> {
2023-11-13 15:43:06 +00:00
public data: T[];
2023-11-13 13:40:01 +00:00
private highWaterMark: number;
2023-11-13 15:27:04 +00:00
public hasSpace = new plugins.smartrx.rxjs.Subject<'hasSpace'>();
2023-11-13 16:20:45 +00:00
private itemsAvailable = new plugins.smartrx.rxjs.Subject<'itemsAvailable'>();
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);
2023-11-13 16:20:45 +00:00
this.itemsAvailable.next('itemsAvailable');
2023-11-13 13:40:01 +00:00
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
}
2023-11-13 16:20:45 +00:00
return spaceAvailable;
2023-11-13 13:40:01 +00:00
}
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 16:20:45 +00:00
waitForItems(): Promise<void> {
return new Promise<void>((resolve) => {
if (this.data.length > 0) {
resolve();
} else {
const subscription = this.itemsAvailable.subscribe(() => {
subscription.unsubscribe();
resolve();
});
}
});
}
2023-11-13 13:40:01 +00:00
}