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

Commit

Permalink
ideas
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT committed Oct 2, 2023
1 parent 86bc8ca commit 1901e77
Showing 1 changed file with 38 additions and 19 deletions.
57 changes: 38 additions & 19 deletions .rfcs/001-serialize-async.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

```js
const out = [

Check failure on line 6 in .rfcs/001-serialize-async.md

View workflow job for this annotation

GitHub Actions / lint

'out' is assigned a value but never used

Check failure on line 6 in .rfcs/001-serialize-async.md

View workflow job for this annotation

GitHub Actions / lint

'out' is assigned a value but never used
// <first line of JSON>
// <first line> is just a `[` that initializes the array pf the response
// <second line>
{
json: {
foo: "bar",
Expand All @@ -13,7 +14,7 @@ const out = [
},
nonce: "__tson",
},
// <first line of JSON>
// <second line>
// <second line of json>
[
// ------ this could be streamed ------
Expand All @@ -30,6 +31,9 @@ const out = [
#### Emitting

```js
async function* stringifyAsync() {}

const response = null;
const data = {
promise: Promise.resolve(42),
};
Expand All @@ -44,31 +48,46 @@ for await (const chunk of stringifyAsync(data)) {
**First chunk**:

```js
async function* stringifyEmitter() {
// <first line: init the array, ignored when parsing>
async function* deserializeAsync() {}

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

// second line: the shape of the json - used when parsing>
yield JSON.stringify(jsonAndNonce) + "\n";
const valuesIterator = deserializeAsync();

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

// each value
let isFirstValue = false;
for await (const [refIndex, serializedValue] of valuesIterator) {
if (!isFirstValue) {
yield ",";
yield "\n";
}
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;
}

yield JSON.stringify([refIndex, serializedValue]);
case "VALUE": {
if (!isFirstStreamedValue) {
yield ",";
}

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

yield "\n";
continue;
}
}
}

yield "]"; // end value array

yield "]"; // end response
}
```

0 comments on commit 1901e77

Please sign in to comment.