diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 531ccad..05fa1dc 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/lik', - version: '6.0.11', + version: '6.0.12', description: 'light little helpers for node' } diff --git a/ts/backpressuredarray.ts b/ts/backpressuredarray.ts index b239444..3c845a5 100644 --- a/ts/backpressuredarray.ts +++ b/ts/backpressuredarray.ts @@ -4,6 +4,7 @@ export class BackpressuredArray { public data: T[]; private highWaterMark: number; public hasSpace = new plugins.smartrx.rxjs.Subject<'hasSpace'>(); + private itemsAvailable = new plugins.smartrx.rxjs.Subject<'itemsAvailable'>(); constructor(highWaterMark: number = 16) { this.data = []; @@ -12,11 +13,13 @@ export class BackpressuredArray { push(item: T): boolean { this.data.push(item); + this.itemsAvailable.next('itemsAvailable'); + const spaceAvailable = this.checkSpaceAvailable(); if (spaceAvailable) { this.hasSpace.next('hasSpace'); } - return spaceAvailable + return spaceAvailable; } shift(): T | undefined { @@ -43,4 +46,18 @@ export class BackpressuredArray { } }); } + + // New method to wait for items + waitForItems(): Promise { + return new Promise((resolve) => { + if (this.data.length > 0) { + resolve(); + } else { + const subscription = this.itemsAvailable.subscribe(() => { + subscription.unsubscribe(); + resolve(); + }); + } + }); + } }