Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
cool
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT committed Oct 2, 2023
1 parent 9d632f4 commit 04b3596
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions .rfcs/001-serialize-async.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,38 +66,43 @@ function asyncSerializer(value) {
return [head, iterator];
}

export async function* asyncStringify(value) {
// first line of the json: init the array, ignored when parsing>
yield "[\n";
export function asyncStringify(value) {
// head looks like

const [head, iterator] = asyncSerializer(value);
// [
// {}
// ,[

// (head is only called once)
const [head, iterator] = asyncSerializer(value);

// first line of the json: init the array, ignored when parsing>
let headAsString = "[" + "\n";
// second line: the shape of the json - used when parsing>
yield JSON.stringify(head) + "\n";
headAsString += JSON.stringify(head) + "\n";

// third line: comma before values, ignored when parsing
yield ",";
yield "["; // values start
yield "\n";
let isFirstStreamedValue = true;

for await (const chunk of iterator) {
if (!isFirstStreamedValue) {
// add a comma between each value to ensure it's valid JSON
// needs to be ignored
yield ",";
headAsString += ",[" + "\n";

async function* serializedAsString() {
let isFirstStreamedValue = true;
for await (const chunk of iterator) {
if (!isFirstStreamedValue) {
// add a comma between each value to ensure it's valid JSON
// will be ignored when parsing ignored
yield ",";
}

isFirstStreamedValue = false;
yield JSON.stringify(chunk.value) + "\n";

yield "\n";
continue;
}

isFirstStreamedValue = false;
yield JSON.stringify(chunk.value) + "\n";

yield "\n";
continue;
yield "]"; // end value array
yield "]"; // end response
}

yield "]"; // end value array
yield "]"; // end response
return [headAsString, serializedAsString()];
}
```

0 comments on commit 04b3596

Please sign in to comment.