From 1901e77ff37c650db4469236cabdbbd2f0f00895 Mon Sep 17 00:00:00 2001 From: KATT Date: Mon, 2 Oct 2023 20:05:14 +0200 Subject: [PATCH] ideas --- .rfcs/001-serialize-async.md | 57 ++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/.rfcs/001-serialize-async.md b/.rfcs/001-serialize-async.md index a57aa45f..52042c62 100644 --- a/.rfcs/001-serialize-async.md +++ b/.rfcs/001-serialize-async.md @@ -4,7 +4,8 @@ ```js const out = [ - // + // is just a `[` that initializes the array pf the response + // { json: { foo: "bar", @@ -13,7 +14,7 @@ const out = [ }, nonce: "__tson", }, - // + // // [ // ------ this could be streamed ------ @@ -30,6 +31,9 @@ const out = [ #### Emitting ```js +async function* stringifyAsync() {} + +const response = null; const data = { promise: Promise.resolve(42), }; @@ -44,31 +48,46 @@ for await (const chunk of stringifyAsync(data)) { **First chunk**: ```js -async function* stringifyEmitter() { - // +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 } ```