*refactor async iteration

This commit is contained in:
Derrick Hammer 2023-04-09 16:19:41 -04:00
parent d3e4577943
commit 73c94f03b4
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 22 additions and 10 deletions

View File

@ -4,7 +4,7 @@ import { CatOptions, LsOptions, StatOptions } from "@helia/unixfs";
interface AbortableGenerator {
abort: () => void;
iterable: AsyncGenerator<object>;
iterable: () => AsyncIterable<Uint8Array>;
}
export class IPFSClient extends Client {
@ -36,7 +36,7 @@ export class IPFSClient extends Client {
method: string,
data: any
): AbortableGenerator {
const pipe = defer();
let pipe = defer<Uint8Array>();
let done = false;
@ -52,15 +52,27 @@ export class IPFSClient extends Client {
return {
abort() {
update();
update("abort");
},
// @ts-ignore
iterable: async function* (): AsyncGenerator<object> {
// @ts-ignore
const iterator = (await pipe.promise)[Symbol.asyncIterator]();
for await (const value of iterator) {
yield value as object;
}
iterable(): AsyncIterable<Uint8Array> {
return {
[Symbol.asyncIterator]() {
return {
async next(): Promise<IteratorResult<Uint8Array>> {
const chunk = await pipe.promise;
update("next");
pipe = defer();
return {
value: chunk,
done: true as const,
};
},
};
},
};
},
};
}