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

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheraff committed Oct 28, 2023
1 parent b1733d9 commit 9d506b4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/async/deserializeAsync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
TsonParseAsyncOptions,
TsonType,
createTsonParseAsync,
tsonAsyncGeneratorFunction,
tsonAsyncIterable,
tsonBigint,
tsonPromise,
tsonAsyncGeneratorFunction,
} from "../index.js";
import { assert } from "../internals/assert.js";
import {
Expand Down Expand Up @@ -93,7 +93,7 @@ test("deserialize async iterable", async () => {
}
});

test.only("stringify async iterable + promise + async generator function", async () => {
test("stringify async iterable + promise + async generator function", async () => {
const tson = createTsonAsync({
nonce: () => "__tson",
types: [tsonAsyncIterable, tsonPromise, tsonBigint, tsonAsyncGeneratorFunction],
Expand All @@ -117,9 +117,9 @@ test.only("stringify async iterable + promise + async generator function", async

const input = {
foo: "bar",
generator,
iterable: generator(),
promise: Promise.resolve(42),
generator,
};

const strIterable = tson.stringifyJsonStream(input);
Expand All @@ -134,13 +134,15 @@ test.only("stringify async iterable + promise + async generator function", async
for await (const value of output.iterable) {
iteratorResult.push(value);
}

expect(iteratorResult).toEqual([1n, 2n, 3n, 4n, 5n]);

const generatorResult1 = [];
const iterator1 = output.generator();
for await (const value of iterator1) {
generatorResult1.push(value);
}

expect(generatorResult1).toEqual([1n, 2n, 3n, 4n, 5n]);

// generator should be able to be iterated again
Expand All @@ -149,6 +151,7 @@ test.only("stringify async iterable + promise + async generator function", async
for await (const value of iterator2) {
generatorResult2.push(value);
}

expect(generatorResult2).toEqual([1n, 2n, 3n, 4n, 5n]);
});

Expand Down
17 changes: 15 additions & 2 deletions src/async/handlers/tsonAsyncGeneratorFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type SerializedIterableResult =
| [typeof ITERATOR_ERROR, unknown]
| [typeof ITERATOR_VALUE, unknown];

function isAsyncGeneratorFunction(value: unknown): value is () => AsyncGenerator<unknown, void, unknown> {
function isAsyncGeneratorFunction(value: unknown): value is () => AsyncGenerator<unknown, void> {
return (
!!value &&
typeof value === "function" &&
Expand All @@ -22,7 +22,7 @@ function isAsyncGeneratorFunction(value: unknown): value is () => AsyncGenerator
}

export const tsonAsyncGeneratorFunction: TsonAsyncType<
() => AsyncGenerator<unknown, void, unknown>,
() => AsyncGenerator<unknown, void>,
SerializedIterableResult
> = {
async: true,
Expand Down Expand Up @@ -50,40 +50,51 @@ export const tsonAsyncGeneratorFunction: TsonAsyncType<
opts.close()
return
}

throw value // <-- is this `throw` necessary for "stream management" / "error reporting"? Or should we only throw in the generator?
}

switch (value[0]) {
case ITERATOR_DONE: {
opts.close();
break loop;
}

case ITERATOR_ERROR: {
opts.close();
break;
}
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- synchronously set when creating `promiseNext`
resolveNext!()
promiseNext = new Promise<void>(resolve => resolveNext = resolve)
}

collectionDone = true
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- synchronously set when creating `promiseNext`
resolveNext!()
}()

/**
* Generator that yields values from the stream
* - handles waiting for chunks if stream is still active
* - handles throwing errors from values
* @yields {unknown}
*/
return async function* generator() {
await promiseNext
for (let i = 0; i < chunks.length; i++) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- `i` is always in range
const value = chunks[i]!
if (value instanceof TsonStreamInterruptedError) {
if (value.cause instanceof TsonAbortError) {
return;
}

throw value;
}

switch (value[0]) {
case ITERATOR_DONE: {
return;
Expand All @@ -98,6 +109,7 @@ export const tsonAsyncGeneratorFunction: TsonAsyncType<
break; // <-- breaks the switch, not the loop
}
}

if (i === chunks.length - 1) {
if (collectionDone) break
await promiseNext
Expand All @@ -113,6 +125,7 @@ export const tsonAsyncGeneratorFunction: TsonAsyncType<
`AsyncGeneratorFunction must have 0 arguments to be serializable, got ${opts.value.length}`
);
}

try {
const iterator = opts.value()
for await (const value of iterator) {
Expand Down

0 comments on commit 9d506b4

Please sign in to comment.