diff --git a/meta.json b/meta.json index 619191e..714b698 100644 --- a/meta.json +++ b/meta.json @@ -1,6 +1,6 @@ { "name": "utils", - "version": "2.2.12", + "version": "2.2.13", "description": "Some helpful utility classes and functions", "author": "Fabian Stamm ", "contributors": [], diff --git a/package.json b/package.json index 170d08a..abfeacf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hibas123/utils", - "version": "2.2.12", + "version": "2.2.13", "description": "Different Utilities, that are not worth own packages", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src/asynciter.ts b/src/asynciter.ts index 931f67a..3f6f11b 100644 --- a/src/asynciter.ts +++ b/src/asynciter.ts @@ -7,17 +7,22 @@ interface IAsyncIteratorMessage { close: true | undefined; } export default class AsyncIteratorFromCB implements AsyncIterable { - private onData = new Observable>(); + #onClose: (() => void)[] = []; + #onData = new Observable>(); constructor() {} + public onClose(callback: () => void) { + this.#onClose.push(callback); + } + public getCallback() { return (error: Error | undefined, data: T | undefined) => { - this.onData.send({ error, data, close: undefined }); + this.#onData.send({ error, data, close: undefined }); }; } public send(data: T) { - this.onData.send({ + this.#onData.send({ error: undefined, data, close: undefined, @@ -25,18 +30,19 @@ export default class AsyncIteratorFromCB implements AsyncIterable { } public close() { - this.onData.send({ + this.#onData.send({ close: true, data: undefined, error: undefined, }); - this.onData.close(); + this.#onData.close(); + this.#onClose.forEach((cb) => cb()); } [Symbol.asyncIterator]() { const queue: IAsyncIteratorMessage[] = []; const signal = new Signal(); - this.onData.subscribe((data) => { + this.#onData.subscribe((data) => { queue.push(data); signal.sendSignal(); });