safe-flow
Version:
A safer way to cancel or interrupt the await-async flow.
191 lines (190 loc) • 7.89 kB
TypeScript
export type Plugin = {
onState: StateHandler;
};
declare class Plugins {
plugins: (Plugin | undefined)[];
onState(state: TraceState, thread: Thread): void;
add(plugin: Plugin): void;
remove(plugin: Plugin): void;
}
export declare const plugins: Plugins;
export type FlowOptions = {
name?: string;
} & Omit<FlowupOptions, 'filter' | 'names'>;
export type FlowupOptions = {
token?: any;
names?: {
[key: string]: true | FlowOptions;
};
plugins?: Plugin[];
} & Config;
export type FlowResult<TReturn = any, TThis = any, TParam extends any[] = any[], TErrf extends boolean = false> = TErrf extends false ? [Canceled<TReturn, TThis, TParam>] | [null, TReturn] : [unknown] | [null, Canceled<TReturn, TThis, TParam>] | [null, null, TReturn];
export type SafeFlowCreator<TReturn = any, TThis = any, TParam extends any[] = any, TErrf extends boolean = false> = (...args: TParam) => Promise<FlowResult<TReturn, TThis, TParam, TErrf>> & (TErrf extends false ? {
safe_flow_promise: true;
canceled: () => boolean;
cancel: (reason?: any) => void;
state: () => PromiseState;
errf: () => ReturnType<SafeFlowCreator<TReturn, TThis, TParam, true>>;
onCancel: (handler: CancelHandler) => ReturnType<SafeFlowCreator<TReturn, TThis, TParam>>;
} : {
safe_flow_promise: true;
canceled: () => boolean;
cancel: (reason?: any) => void;
state: () => PromiseState;
onCancel: (handler: CancelHandler) => ReturnType<SafeFlowCreator<TReturn, TThis, TParam, true>>;
});
type Canceler = (reason: any, isDropout?: boolean) => any;
export declare const FlowState: {
readonly canceled: 1;
readonly done: 2;
};
export declare const ErrfState: {
readonly error: 1;
readonly canceled: 2;
readonly done: 3;
};
export declare enum PromiseState {
pending = 0,
fulfilled = 1,
rejected = 2,
canceled = 3
}
type CancelHandler = (reason: any) => any;
export declare class Thread {
func: Function;
creator: SafeFlowCreator;
parent: Thread | undefined;
token: any;
trace: boolean | Trace;
autoBreak: boolean;
name?: string | undefined;
id?: number | undefined;
onState?: StateHandler | undefined;
plugins?: Plugin[] | undefined;
promiseState: PromiseState;
canceled: boolean;
state?: TraceState;
canceler?: Canceler;
cancellation?: Canceled;
children: Thread[];
willCancel: boolean;
willBreak: boolean;
get hasChildren(): boolean;
_completed: boolean;
get completed(): boolean;
_disposed: boolean;
get disposed(): boolean;
_level: number;
levelup(): void;
constructor(func: Function, creator: SafeFlowCreator, parent: Thread | undefined, token: any, trace: boolean | Trace, autoBreak?: boolean, name?: string | undefined, id?: number | undefined, onState?: StateHandler | undefined, plugins?: Plugin[] | undefined);
changeState(state: TraceState.thread_starting): void;
changeState(state: TraceState.thread_idle): void;
changeState(state: TraceState.thread_canceled, reason: unknown): void;
changeState(state: TraceState.thread_error, error: unknown): void;
changeState(state: TraceState.thread_done, value: unknown): void;
changeState(state: TraceState.thread_completed): void;
changeState(state: TraceState.thread_done_canceled, value: unknown): void;
changeState(state: TraceState.thread_disposed): void;
addChild(thread: Thread): void;
removeChild(thread: Thread): void;
complete(dispose?: boolean): void;
dispose(): void;
}
export declare class Canceled<TReturn = any, TThis = any, TParam extends any[] = any> {
creator: SafeFlowCreator<TReturn, TThis, TParam>;
thisArg: TThis;
args: TParam;
reason: any;
constructor(creator: SafeFlowCreator<TReturn, TThis, TParam>, thisArg: TThis, args: TParam, reason: any);
retry(): Promise<[Canceled<TReturn, TThis, TParam>] | [null, TReturn]> & {
safe_flow_promise: true;
canceled: () => boolean;
cancel: (reason?: any) => void;
state: () => PromiseState;
errf: () => Promise<[unknown] | [null, null, TReturn] | [null, Canceled<TReturn, TThis, TParam>]> & {
safe_flow_promise: true;
canceled: () => boolean;
cancel: (reason?: any) => void;
state: () => PromiseState;
onCancel: (handler: CancelHandler) => Promise<[unknown] | [null, null, TReturn] | [null, Canceled<TReturn, TThis, TParam>]> & any;
};
onCancel: (handler: CancelHandler) => Promise<[Canceled<TReturn, TThis, TParam>] | [null, TReturn]> & any;
};
}
export type Filter = (name: string) => boolean;
export type StateHandler = (state: TraceState, thread: Thread) => any;
type Config = {
trace?: boolean | Trace;
standalone?: boolean;
autoBreak?: boolean;
filter?: Filter;
onState?: StateHandler;
development?: 'auto' | 'development' | 'production';
};
export declare function configure(options?: Config): void;
export declare const __debug_logger: {
log: (...args: any[]) => void;
};
export declare const __debug_token_creators: Map<any, SafeFlowCreator<any, any, any, false>[]>;
export declare const __debug_creator_processes: Map<SafeFlowCreator<any, any, any, false>, Thread[]>;
export declare const __debug_get_curr_thread: () => Thread | undefined;
export declare function __debug_clear_names(): void;
export declare function __debug_clear_threads(): void;
export declare function __debug_enable(value: boolean): void;
export declare const __debug_live_threads: Thread[];
export declare function __debug_in_thread(): boolean;
export declare enum TraceState {
creator_created = 0,
thread_starting = 1,
thread_idle = 2,
thread_canceled = 3,
thread_error = 4,
thread_done = 5,
thread_done_canceled = 6,
thread_completed = 7,
thread_disposed = 8
}
export type TraceEvent = {
name: string;
state: TraceState.creator_created;
} | {
name: string;
id?: number;
state: TraceState.thread_canceled;
reason?: any;
} | {
name: string;
id?: number;
state: TraceState.thread_error;
error: unknown;
} | {
name: string;
id?: number;
state: TraceState.thread_done;
value: unknown;
} | {
name: string;
id?: number;
state: TraceState.thread_done_canceled;
value?: unknown;
} | {
name: string;
id?: number;
state: TraceState.thread_starting | TraceState.thread_idle | TraceState.thread_completed | TraceState.thread_disposed;
};
export type Trace = (event: TraceEvent) => void;
export declare function flow<TReturn = any, TParam extends any[] = any>(func: (...args: TParam) => Promise<TReturn>, options?: FlowOptions): SafeFlowCreator<TReturn, ThisParameterType<typeof func>, TParam>;
export declare function isInvalid(): boolean;
export declare function isSafeFlowPromise(promise: any): boolean;
export declare function isCreator(func: Function): boolean;
export declare function cancel(creator: SafeFlowCreator, reason?: any): void;
export declare function cancel(token?: any, reason?: any): void;
export declare function cancelAll(reason?: any): void;
export declare function cancelSelf(reason?: any): void;
export declare function flowed<TReturn = any, TThis = any, TParam extends any[] = any>(func: (this: TThis, ...args: TParam) => Promise<TReturn>): SafeFlowCreator<TReturn, TThis, TParam>;
export declare function isFlowupObject(target: any): boolean;
export declare function flowup<T = any>(target: T, options?: FlowupOptions): T;
export declare function internalFlowup(target: any, options?: FlowupOptions): void;
export declare function flowable(options: FlowOptions): (target: any, propertyKey: string, descriptor?: PropertyDescriptor) => void;
export declare function flowable(target: any, propertyKey: string, descriptor?: PropertyDescriptor): void;
export {};