|
| 1 | +// eslint-disable-next-line eslint-comments/disable-enable-pair |
| 2 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 3 | +import { TsonType } from "../types.js"; |
| 4 | +import { TsonBranded, TsonTypeTesterCustom } from "../types.js"; |
| 5 | +import { serialized } from "../types.js"; |
| 6 | + |
| 7 | +export type TsonAsyncStringifierIterator<TValue> = AsyncIterable<string> & { |
| 8 | + [serialized]: TValue; |
| 9 | +}; |
| 10 | + |
| 11 | +export type TsonAsyncStringifier = <TValue>( |
| 12 | + value: TValue, |
| 13 | + space?: number, |
| 14 | +) => TsonAsyncStringifierIterator<TValue>; |
| 15 | +export type TsonAsyncIndex = TsonBranded<number, "AsyncRegistered">; |
| 16 | +export interface TsonTransformerSerializeDeserializeAsync<TValue> { |
| 17 | + async: true; |
| 18 | + /** |
| 19 | + * From JSON-serializable value |
| 20 | + */ |
| 21 | + deserialize: ( |
| 22 | + v: TsonAsyncIndex, |
| 23 | + register: (index: TsonAsyncIndex) => Promise<TValue>, |
| 24 | + ) => TValue; |
| 25 | + |
| 26 | + /** |
| 27 | + * The key to use when serialized |
| 28 | + */ |
| 29 | + key: string; |
| 30 | + /** |
| 31 | + * JSON-serializable value |
| 32 | + */ |
| 33 | + serialize: ( |
| 34 | + v: TValue, |
| 35 | + register: (thing: TValue) => TsonAsyncIndex, |
| 36 | + ) => TsonAsyncIndex; |
| 37 | +} |
| 38 | + |
| 39 | +export interface TsonAsyncType<TValue> |
| 40 | + extends TsonTransformerSerializeDeserializeAsync<TValue>, |
| 41 | + TsonTypeTesterCustom {} |
| 42 | +export interface TsonAsyncOptions { |
| 43 | + /** |
| 44 | + * The nonce function every time we start serializing a new object |
| 45 | + * Should return a unique value every time it's called |
| 46 | + * @default `${crypto.randomUUID} if available, otherwise a random string generated by Math.random` |
| 47 | + */ |
| 48 | + nonce?: () => number | string; |
| 49 | + /** |
| 50 | + * The list of types to use |
| 51 | + */ |
| 52 | + types: (TsonAsyncType<any> | TsonType<any, any> | TsonType<any, never>)[]; |
| 53 | +} |
0 commit comments