UNPKG

suspenders-js

Version:

Asynchronous programming library utilizing coroutines, functional reactive programming and structured concurrency.

49 lines (48 loc) 1.37 kB
import { Scope } from "./Scope"; /** * Use yield on this in a coroutine to suspend the coroutine on an async task that resolves to T. * If in TypeScript use yield* suspend(Suspender<T>) to help the type checker get the resolved type. */ export declare type Suspender<T> = (s: ResultCallback<T>) => (CancelFunction | void); /** * Data object contains value or error of resolved async task. */ export declare type Result<T> = Readonly<{ tag: `error`; error: unknown; } | { tag?: `value`; value: T; }>; export declare type Resume<T> = Readonly<{ tag: `finish`; }> | Result<T>; /** * Callback when async task resolves. */ export declare type ResultCallback<T> = (result: Result<T>) => void; /** * Cancels an async task. */ export declare type CancelFunction = () => void; /** * Instantiated coroutine. */ export declare type Coroutine<T> = Generator<Suspender<unknown>, T, unknown>; /** * Call Scope.launch(CoroutineFactory<T>) to instantiate a Coroutine. */ export declare type CoroutineFactory<T> = (this: Scope) => Coroutine<T>; /** * Collector interface used to emit values. */ export interface Collector<T> { emit(value: T): void; } /** * Observer interface is implemented by objects that observe upstream flows. */ export interface Observer<T> extends Collector<T> { complete(): void; error(error: unknown): void; }