From 19d8e783fb72e164b25d8952eaa24382915539c2 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 17 Apr 2023 02:04:10 -0400 Subject: [PATCH] *refactor connectModuleGenerator to use a undefined value to signal the end of the stream --- src/index.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index cd8df35..c6abd53 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,16 +38,14 @@ export class IPFSClient extends Client { ): AbortableGenerator { let pipe = defer(); - let done = false; - const [update, result] = this.connectModule(method, data, (item: any) => { pipe.resolve(item); }); (async () => { const ret = await result; - done = true; this.handleError(ret); + pipe.resolve(undefined); })(); return { @@ -60,21 +58,21 @@ export class IPFSClient extends Client { [Symbol.asyncIterator]() { return { async next(): Promise> { - if (done) { + const chunk = await pipe.promise; + if (chunk === undefined) { return { - value: undefined, - done, + value: new Uint8Array(), + done: true, }; } - const chunk = await pipe.promise; update("next"); pipe = defer(); return { value: chunk, - done, + done: false, }; }, };