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

Commit

Permalink
woop
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT committed Oct 2, 2023
1 parent 3ee7594 commit 9d632f4
Showing 1 changed file with 38 additions and 31 deletions.
69 changes: 38 additions & 31 deletions .rfcs/001-serialize-async.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,45 +49,52 @@ for await (const chunk of stringifyAsync(data)) {
**First chunk**:

```js
async function* deserializeAsync() {}
function walk() {}

function asyncSerializer(value) {
const [
/**
* TsonSerialized
**/
head,
/**
* yields [index, data]
*/
iterator,
] = walk(value);

return [head, iterator];
}

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

const valuesIterator = deserializeAsync();
const [head, iterator] = asyncSerializer(value);

// (head is only called once)

// second line: the shape of the json - used when parsing>
yield 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 valuesIterator) {
switch (chunk.type) {
case "HEAD": {
// (head is only called once)

// second line: the shape of the json - used when parsing>
yield JSON.stringify(chunk.head) + "\n";

// third line: comma before values, ignored when parsing
yield ",";
yield "["; // values start
yield "\n";
continue;
}

case "VALUE": {
if (!isFirstStreamedValue) {
// add a comma between each value to ensure it's valid JSON
// needs to be ignored
yield ",";
}

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

yield "\n";
continue;
}
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 ",";
}

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

yield "\n";
continue;
}

yield "]"; // end value array
Expand Down

0 comments on commit 9d632f4

Please sign in to comment.