Adding onClose callback to AsyncIter
This commit is contained in:
parent
799651d37f
commit
0c50717a4c
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "utils",
|
"name": "utils",
|
||||||
"version": "2.2.12",
|
"version": "2.2.13",
|
||||||
"description": "Some helpful utility classes and functions",
|
"description": "Some helpful utility classes and functions",
|
||||||
"author": "Fabian Stamm <dev@fabianstamm.de>",
|
"author": "Fabian Stamm <dev@fabianstamm.de>",
|
||||||
"contributors": [],
|
"contributors": [],
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hibas123/utils",
|
"name": "@hibas123/utils",
|
||||||
"version": "2.2.12",
|
"version": "2.2.13",
|
||||||
"description": "Different Utilities, that are not worth own packages",
|
"description": "Different Utilities, that are not worth own packages",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"types": "lib/index.d.ts",
|
"types": "lib/index.d.ts",
|
||||||
|
@ -7,17 +7,22 @@ interface IAsyncIteratorMessage<T> {
|
|||||||
close: true | undefined;
|
close: true | undefined;
|
||||||
}
|
}
|
||||||
export default class AsyncIteratorFromCB<T> implements AsyncIterable<T> {
|
export default class AsyncIteratorFromCB<T> implements AsyncIterable<T> {
|
||||||
private onData = new Observable<IAsyncIteratorMessage<T>>();
|
#onClose: (() => void)[] = [];
|
||||||
|
#onData = new Observable<IAsyncIteratorMessage<T>>();
|
||||||
constructor() {}
|
constructor() {}
|
||||||
|
|
||||||
|
public onClose(callback: () => void) {
|
||||||
|
this.#onClose.push(callback);
|
||||||
|
}
|
||||||
|
|
||||||
public getCallback() {
|
public getCallback() {
|
||||||
return (error: Error | undefined, data: T | undefined) => {
|
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) {
|
public send(data: T) {
|
||||||
this.onData.send({
|
this.#onData.send({
|
||||||
error: undefined,
|
error: undefined,
|
||||||
data,
|
data,
|
||||||
close: undefined,
|
close: undefined,
|
||||||
@ -25,18 +30,19 @@ export default class AsyncIteratorFromCB<T> implements AsyncIterable<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public close() {
|
public close() {
|
||||||
this.onData.send({
|
this.#onData.send({
|
||||||
close: true,
|
close: true,
|
||||||
data: undefined,
|
data: undefined,
|
||||||
error: undefined,
|
error: undefined,
|
||||||
});
|
});
|
||||||
this.onData.close();
|
this.#onData.close();
|
||||||
|
this.#onClose.forEach((cb) => cb());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Symbol.asyncIterator]() {
|
[Symbol.asyncIterator]() {
|
||||||
const queue: IAsyncIteratorMessage<T>[] = [];
|
const queue: IAsyncIteratorMessage<T>[] = [];
|
||||||
const signal = new Signal();
|
const signal = new Signal();
|
||||||
this.onData.subscribe((data) => {
|
this.#onData.subscribe((data) => {
|
||||||
queue.push(data);
|
queue.push(data);
|
||||||
signal.sendSignal();
|
signal.sendSignal();
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user