async-streamify
Version:
Stream and serialize nested promises and async iterables over HTTP, workers, etc
63 lines • 2.03 kB
TypeScript
import BufferedAsyncIterable from "../util/bufferedAsyncIterable.js";
/**
* Serializes objects containing promises and async iterables into a stream of updates.
* Handles nested promises, async iterables, and regular object properties.
*
* @template TSource - The type of the source object being serialized
*
* @example
* ```typescript
* const obj = {
* name: "test",
* promise: Promise.resolve(42),
* async *numbers() {
* yield 1;
* yield 2;
* }
* };
*
* const serializer = new AsyncObjectSerializer(obj);
*
* for await (const update of serializer) {
* console.log(update);
* // Initial: { name: "test", promise: { $promise: 1 }, numbers: { $asyncIterator: 2 } }
* // Promise resolved: [1, 42]
* // Iterator values: [2, { done: false, value: 1 }], [2, { done: false, value: 2 }]
* // Iterator done: [2, { done: true }]
* }
* ```
*/
export declare class AsyncObjectSerializer<TSource = object> extends BufferedAsyncIterable {
private sourceObject;
private serializationIdCounter;
private activeAsyncOperations;
private activeIterators;
/**
* Schedules updates for all active async iterators
* @returns void
*/
protected scheduleIteratorUpdates(): void;
/**
* Creates a new AsyncObjectSerializer instance
* @param object - The source object to serialize
*/
constructor(object: TSource);
/**
* Gets the next unique serialization ID and increments the active operation count
* @returns number
*/
private getNextSerializationId;
/**
* Decrements the active operation count and marks as done if no operations remain
* @returns void
*/
private decrementActiveCount;
/**
* Recursively serializes a value, handling promises, async iterables, and nested objects
* @param value - The value to serialize
* @returns The serialized value
*/
private serializeValue;
}
export default AsyncObjectSerializer;
//# sourceMappingURL=asyncObjectSerializer.d.ts.map