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