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

Commit

Permalink
fix: rename AsyncIterator -> AsyncIterable (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT authored Oct 5, 2023
1 parent 331802d commit d8b1c01
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 67 deletions.
8 changes: 4 additions & 4 deletions src/async/serializeAsync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ test("serialize async iterable", async () => {
expect(head).toMatchInlineSnapshot(`
{
"json": [
"AsyncIterator",
"AsyncIterable",
0,
"__tson",
],
Expand Down Expand Up @@ -184,7 +184,7 @@ test("stringify async iterable + promise", async () => {
expect(buffer).toMatchInlineSnapshot(`
[
"[",
" {\\"json\\":{\\"foo\\":\\"bar\\",\\"iterable\\":[\\"AsyncIterator\\",0,\\"__tson\\"],\\"promise\\":[\\"Promise\\",1,\\"__tson\\"]},\\"nonce\\":\\"__tson\\"}",
" {\\"json\\":{\\"foo\\":\\"bar\\",\\"iterable\\":[\\"AsyncIterable\\",0,\\"__tson\\"],\\"promise\\":[\\"Promise\\",1,\\"__tson\\"]},\\"nonce\\":\\"__tson\\"}",
" ,",
" [",
" [1,[0,42]]",
Expand All @@ -201,7 +201,7 @@ test("stringify async iterable + promise", async () => {
"json": {
"foo": "bar",
"iterable": [
"AsyncIterator",
"AsyncIterable",
0,
"__tson",
],
Expand All @@ -215,6 +215,6 @@ test("stringify async iterable + promise", async () => {
}
`);

expect(head.json.iterable[0]).toBe("AsyncIterator");
expect(head.json.iterable[0]).toBe("AsyncIterable");
expect(head.json.promise[0]).toBe("Promise");
});
1 change: 1 addition & 0 deletions src/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from "./tsonSymbol.js";

// Async types
export * from "./tsonPromise.js";
export * from "./tsonAsyncIterable.js";
62 changes: 62 additions & 0 deletions src/handlers/tsonAsyncIterable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { TsonAsyncType } from "../async/asyncTypes.js";
import { TsonPromiseRejectionError } from "../errors.js";

const ITERATOR_VALUE = 0;
const ITERATOR_ERROR = 1;
const ITERATOR_DONE = 2;
type SerializedIteratorResult =
| [typeof ITERATOR_DONE]
| [typeof ITERATOR_ERROR, unknown]
| [typeof ITERATOR_VALUE, unknown];
function isAsyncIterator(value: unknown): value is AsyncIterable<unknown> {
return (
!!value &&
typeof value === "object" &&
typeof (value as any)[Symbol.asyncIterator] === "function"
);
}

export const tsonAsyncIterator: TsonAsyncType<
AsyncIterable<unknown>,
SerializedIteratorResult
> = {
async: true,
deserialize: (opts) => {
return (async function* generator() {
try {
for await (const value of opts.stream) {
switch (value[0]) {
case ITERATOR_DONE: {
return;
}

case ITERATOR_ERROR: {
throw TsonPromiseRejectionError.from(value[1]);
}

case ITERATOR_VALUE: {
yield value[1];
break;
}
}
}
} finally {
// `onDone` is a hack and shouldn't be needed
opts.onDone();
}
})();
},
key: "AsyncIterable",
serializeIterator: async function* serialize(opts) {
try {
for await (const value of opts.value) {
yield [ITERATOR_VALUE, value];
}

yield [ITERATOR_DONE];
} catch (err) {
yield [ITERATOR_ERROR, err];
}
},
test: isAsyncIterator,
};
64 changes: 1 addition & 63 deletions src/handlers/tsonPromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const tsonPromise: TsonAsyncType<MyPromise, SerializedPromiseValue> = {
const [status, result] = value.value;

status === PROMISE_RESOLVED
? resolve(result as any)
? resolve(result)
: reject(TsonPromiseRejectionError.from(result));
}

Expand All @@ -56,65 +56,3 @@ export const tsonPromise: TsonAsyncType<MyPromise, SerializedPromiseValue> = {
},
test: isPromise,
};

const ITERATOR_VALUE = 0;
const ITERATOR_ERROR = 1;
const ITERATOR_DONE = 2;

type SerializedIteratorResult =
| [typeof ITERATOR_DONE]
| [typeof ITERATOR_ERROR, unknown]
| [typeof ITERATOR_VALUE, unknown];

function isAsyncIterator(value: unknown): value is AsyncIterable<unknown> {
return (
!!value &&
typeof value === "object" &&
typeof (value as any)[Symbol.asyncIterator] === "function"
);
}

export const tsonAsyncIterator: TsonAsyncType<
AsyncIterable<unknown>,
SerializedIteratorResult
> = {
async: true,
deserialize: (opts) => {
return (async function* generator() {
try {
for await (const value of opts.stream) {
switch (value[0]) {
case ITERATOR_DONE: {
return;
}

case ITERATOR_ERROR: {
throw TsonPromiseRejectionError.from(value[1]);
}

case ITERATOR_VALUE: {
yield value[1];
break;
}
}
}
} finally {
// `onDone` is a hack and shouldn't be needed
opts.onDone();
}
})();
},
key: "AsyncIterator",
serializeIterator: async function* serialize(opts) {
try {
for await (const value of opts.value) {
yield [ITERATOR_VALUE, value];
}

yield [ITERATOR_DONE];
} catch (err) {
yield [ITERATOR_ERROR, err];
}
},
test: isAsyncIterator,
};

0 comments on commit d8b1c01

Please sign in to comment.