Adding onClose callback to AsyncIter
This commit is contained in:
parent
799651d37f
commit
0c50717a4c
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "utils",
|
||||
"version": "2.2.12",
|
||||
"version": "2.2.13",
|
||||
"description": "Some helpful utility classes and functions",
|
||||
"author": "Fabian Stamm <dev@fabianstamm.de>",
|
||||
"contributors": [],
|
||||
|
@ -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",
|
||||
|
@ -7,17 +7,22 @@ interface IAsyncIteratorMessage<T> {
|
||||
close: true | undefined;
|
||||
}
|
||||
export default class AsyncIteratorFromCB<T> implements AsyncIterable<T> {
|
||||
private onData = new Observable<IAsyncIteratorMessage<T>>();
|
||||
#onClose: (() => void)[] = [];
|
||||
#onData = new Observable<IAsyncIteratorMessage<T>>();
|
||||
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<T> implements AsyncIterable<T> {
|
||||
}
|
||||
|
||||
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<T>[] = [];
|
||||
const signal = new Signal();
|
||||
this.onData.subscribe((data) => {
|
||||
this.#onData.subscribe((data) => {
|
||||
queue.push(data);
|
||||
signal.sendSignal();
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user