ciorent
Version:
A lightweight, low-overhead concurrency library
71 lines (70 loc) • 1.86 kB
TypeScript
/**
* Describe a fiber process
*/
export type Process<TReturn = unknown> = [proc: Promise<TReturn | undefined>, status: 1 | 2 | 3, children: Process[]];
/**
* Describe a fiber runtime
*/
export type Runtime = <
const TReturn,
const Args extends any[]
>(gen: (proc: Process<TReturn>, ...args: Args) => Generator<any, TReturn>, ...args: Args) => Process<TReturn>;
/**
* Check whether the fiber is running
*/
export declare const running: (t: Process) => boolean;
/**
* Check whether the fiber has completed
*/
export declare const completed: (t: Process) => boolean;
/**
* Check whether the fiber has been interrupted
*/
export declare const interrupted: (t: Process) => boolean;
/**
* Create a fiber function
* @param f
*/
export declare const fn: <const Fn extends (thread: Process, ...args: any[]) => Generator>(f: Fn) => Fn;
/**
* A basic fiber runtime
* @param g
*/
export declare const spawn: Runtime;
/**
* Interrupt the execution of a fiber
* @param t
*/
export declare const interrupt: (t: Process) => void;
/**
* Timeout a fiber
* @param t
* @param ms
*/
export declare const timeout: (t: Process, ms: number) => Promise<void>;
/**
* Wait for a fiber and retrieve its result
* @param t
*/
export declare function join<T extends Process>(t: T): Generator<Awaited<T[0]>, Awaited<T[0]>>;
/**
* Wait for a fiber to finish and retrieve its result
* @param t
*/
export declare const done: <T extends Process>(t: T) => T[0];
/**
* Mount child fiber lifetime to parent lifetime
* @param child
* @param parent
*/
export declare const mount: (child: Process, parent: Process) => void;
/**
* Control the fiber with an abort signal
* @param t
* @param signal
*/
export declare const control: (t: Process, signal: AbortSignal) => void;
/**
* Unwrap a promise result
*/
export declare function unwrap<T extends Promise<any>>(t: T): Generator<Awaited<T>, Awaited<T>>;