This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
generated from JoshuaKGoldberg/create-typescript-app
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: serialization of promises (#20)
- Loading branch information
Showing
31 changed files
with
1,095 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
## Serializing promises and other async generators | ||
|
||
### Finished JSON output | ||
|
||
```js | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars | ||
const out = [ | ||
// <first line> is just a `[` that initializes the array pf the response | ||
// <second line> | ||
{ | ||
json: { | ||
foo: "bar", | ||
iterator: ["AsyncIterator", 1, "__tson"], | ||
promise: ["Promise", 0, "__tson"], | ||
}, | ||
nonce: "__tson", | ||
}, | ||
// <second line> | ||
// <second line of json> | ||
[ | ||
// ------ this could be streamed ------ | ||
[1, ["chunk", "chunk from iterator"]], | ||
[0, ["resolve", "result of promise"]], | ||
[1, ["chunk", "another chunk from iterator"]], | ||
[1, ["end"]], | ||
], | ||
]; | ||
``` | ||
|
||
### Serializing | ||
|
||
> This is now implemented |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { test } from "vitest"; | ||
|
||
import "./asyncTypes.js"; | ||
|
||
test.todo("check that it retains type"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// eslint-disable-next-line eslint-comments/disable-enable-pair | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { TsonType } from "../types.js"; | ||
import { TsonBranded, TsonTypeTesterCustom } from "../types.js"; | ||
import { serialized } from "../types.js"; | ||
|
||
export type TsonAsyncStringifierIterator<TValue> = AsyncIterable<string> & { | ||
[serialized]: TValue; | ||
}; | ||
|
||
export type TsonAsyncStringifier = <TValue>( | ||
value: TValue, | ||
space?: number, | ||
) => TsonAsyncStringifierIterator<TValue>; | ||
export type TsonAsyncIndex = TsonBranded<number, "AsyncRegistered">; | ||
export interface TsonTransformerSerializeDeserializeAsync<TValue> { | ||
async: true; | ||
/** | ||
* From JSON-serializable value | ||
*/ | ||
deserialize: ( | ||
v: TsonAsyncIndex, | ||
register: (index: TsonAsyncIndex) => Promise<TValue>, | ||
) => TValue; | ||
|
||
/** | ||
* The key to use when serialized | ||
*/ | ||
key: string; | ||
/** | ||
* JSON-serializable value | ||
*/ | ||
serialize: ( | ||
v: TValue, | ||
register: (thing: TValue) => TsonAsyncIndex, | ||
) => TsonAsyncIndex; | ||
} | ||
|
||
export interface TsonAsyncType<TValue> | ||
extends TsonTransformerSerializeDeserializeAsync<TValue>, | ||
TsonTypeTesterCustom {} | ||
export interface TsonAsyncOptions { | ||
/** | ||
* The nonce function every time we start serializing a new object | ||
* Should return a unique value every time it's called | ||
* @default `${crypto.randomUUID} if available, otherwise a random string generated by Math.random` | ||
*/ | ||
nonce?: () => number | string; | ||
/** | ||
* The list of types to use | ||
*/ | ||
types: (TsonAsyncType<any> | TsonType<any, any> | TsonType<any, never>)[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { TsonAsyncOptions } from "./asyncTypes.js"; | ||
import { createTsonParseAsync } from "./deserializeAsync.js"; | ||
import { createAsyncTsonStringify } from "./serializeAsync.js"; | ||
|
||
export const createTsonAsync = (opts: TsonAsyncOptions) => ({ | ||
parse: createTsonParseAsync(opts), | ||
stringify: createAsyncTsonStringify(opts), | ||
}); |
Oops, something went wrong.