*refactor connectModuleGenerator to use a undefined value to signal the end of the stream
This commit is contained in:
parent
27dce710a6
commit
19d8e783fb
14
src/index.ts
14
src/index.ts
|
@ -38,16 +38,14 @@ export class IPFSClient extends Client {
|
||||||
): AbortableGenerator {
|
): AbortableGenerator {
|
||||||
let pipe = defer<Uint8Array>();
|
let pipe = defer<Uint8Array>();
|
||||||
|
|
||||||
let done = false;
|
|
||||||
|
|
||||||
const [update, result] = this.connectModule(method, data, (item: any) => {
|
const [update, result] = this.connectModule(method, data, (item: any) => {
|
||||||
pipe.resolve(item);
|
pipe.resolve(item);
|
||||||
});
|
});
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
const ret = await result;
|
const ret = await result;
|
||||||
done = true;
|
|
||||||
this.handleError(ret);
|
this.handleError(ret);
|
||||||
|
pipe.resolve(undefined);
|
||||||
})();
|
})();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -60,21 +58,21 @@ export class IPFSClient extends Client {
|
||||||
[Symbol.asyncIterator]() {
|
[Symbol.asyncIterator]() {
|
||||||
return {
|
return {
|
||||||
async next(): Promise<IteratorResult<Uint8Array>> {
|
async next(): Promise<IteratorResult<Uint8Array>> {
|
||||||
if (done) {
|
const chunk = await pipe.promise;
|
||||||
|
if (chunk === undefined) {
|
||||||
return {
|
return {
|
||||||
value: undefined,
|
value: new Uint8Array(),
|
||||||
done,
|
done: true,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const chunk = await pipe.promise;
|
|
||||||
update("next");
|
update("next");
|
||||||
|
|
||||||
pipe = defer();
|
pipe = defer();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
value: chunk,
|
value: chunk,
|
||||||
done,
|
done: false,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue