*refactor async iteration
This commit is contained in:
parent
d3e4577943
commit
73c94f03b4
32
src/index.ts
32
src/index.ts
|
@ -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,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
};
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue