* Add support for chunked streaming in `index.ts` by setting up a next chunk promise and handling "next" and "abort" messages in the receive update callback.

This commit is contained in:
Derrick Hammer 2023-04-09 17:16:12 -04:00
parent f69ff102cc
commit 824881ed88
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 26 additions and 5 deletions

View File

@ -265,11 +265,19 @@ async function handleLs(aq: ActiveQuery) {
}
let aborted = false;
let nextChunk = defer();
aq.setReceiveUpdate?.(() => {
aborted = true;
aq.setReceiveUpdate?.((data: any) => {
switch (data) {
case "abort":
aborted = true;
break;
case "next":
nextChunk.resolve();
nextChunk = defer();
break;
}
});
const iterable = fs.ls(
getCID(aq.callerInput.cid),
aq.callerInput.options ?? {}
@ -280,6 +288,8 @@ async function handleLs(aq: ActiveQuery) {
break;
}
aq.sendUpdate(JSON.parse(JSON.stringify(item)));
await nextChunk.promise;
}
aq.respond();
@ -294,9 +304,18 @@ async function handleCat(aq: ActiveQuery) {
}
let aborted = false;
let nextChunk = defer();
aq.setReceiveUpdate?.(() => {
aborted = true;
aq.setReceiveUpdate?.((data: any) => {
switch (data) {
case "abort":
aborted = true;
break;
case "next":
nextChunk.resolve();
nextChunk = defer();
break;
}
});
const iterable = fs.cat(
@ -310,6 +329,8 @@ async function handleCat(aq: ActiveQuery) {
}
aq.sendUpdate(chunk);
await nextChunk.promise;
}
aq.respond();