import { CancelledAct } from './cancelled'; /** * @deprecated use ListenerFunction */ export type listenerFunction = (arg: any) => any; export type ListenerFunction = (arg: any) => any; export type ValidateInputFn = (...args: any[]) => true | CancelledAct | any | Promise; export type autoUnsubscribeArgs = { eventName: string; listener: ListenerFunction; context?: any; }; export interface DIMap { [key: string]: any; } export interface IActMaster { exec: ActExec; } export interface ActMasterOptions { actions?: ActMasterAction[]; di?: DIMap; autoUnsubscribeCallback?: (args: autoUnsubscribeArgs) => void; errorHandlerEventName?: ActEventName; } export type devActMasterConfig = { autoUnsubscribeCallback: ActMasterOptions['autoUnsubscribeCallback']; errorHandlerEventName?: ActEventName; }; export interface ActMasterAction { /** * Name of the action */ readonly name: string; /** * Function executor */ exec: (...args: any[]) => Promise | any; /** * List of emitNames to be called after * TODO: change to ActEventName */ $watch?: string[]; /** * @deprecated * Use $watch */ watch?: string[]; /** * An action can have only one result if several calls are made */ $isSingleton?: boolean; /** * @deprecated * Use $isSingleton */ isSingleExec?: boolean; /** * Validating arguments before passing them to exec */ $validate?: ValidateInputFn; /** * @deprecated * Use $validate */ validateInput?: ValidateInputFn; /** * Pass Dependency to action */ $di?: (key: string) => T; /** * @deprecated * Use $di to inherit from the BaseActMasterAction class */ useDI?: (contexts: { [key: string]: any; }) => void; /** * Pass Emitter to action */ $emit?: EmitAction; /** * @deprecated * Use $emit inherited from the BaseActMasterAction class */ useEmit?: (emit: EmitAction) => void; /** * Name of the action that catches the error * Use ActName type if you want to check the available name through types * @example * $onError: ActName<'OnError'> = 'OnError'; */ $onError?: string; /** * @deprecated * Use $onError */ errorHandlerEventName?: string; [key: string]: any; } export interface ActMaster { exec: ActExec; } /** * Type to check the name of Act */ export type ActName = N; export interface ActMasterActionDevDI extends ActMasterAction { _DI_CONTAINER_?: { readonly [DIName: string]: any; }; _DI_MAP_?: { [key: string]: string; }; } export interface ActGenerated { default: (name: string, ...args: any) => Promise; defaultSubs: (name: string, cb: (data: any) => any, ctx?: any) => () => boolean; defaultActionList: ActMasterAction[]; } type _ExecType = K extends keyof ActGenerated ? ActGenerated[K] : ActGenerated['default']; type _MapType = K extends keyof ActGenerated ? ActGenerated[K] : Record; type _SubsType = K extends keyof ActGenerated ? ActGenerated[K] : ActGenerated['defaultSubs']; type _NameType = K extends keyof ActGenerated ? ActGenerated[K] : string; type Fn = (...args: any) => any; type IsFn = T extends (...args: any) => any ? T : never; type UnionToIntersection = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never; type UnPromise = T extends Promise ? U : T; type UnsubscribeFnType = () => boolean; type PromiseResult = Promise>; type FuncFromAct = T extends { readonly name: infer N; readonly exec: infer F; } ? (name: N, ...args: Parameters>) => PromiseResult> | null> : never; type MapFromAct = T extends { readonly name: infer N; readonly exec: infer F; } ? Record>) => PromiseResult> | null>> : never; type SubsFromAct = T extends { readonly name: infer N; readonly exec: infer F; } ? (name: N, cb: (data: UnPromise>>) => any, ctx?: any) => UnsubscribeFnType : never; type NamesFromAct = T extends { readonly name: infer N; readonly exec: Fn; } ? N : never; /** * @example * declare module 'act-master' { * export interface ActGenerated { * acts: Acts; * subs: Subs; * map: MapAct; * names: Names; * subFnFromAct: SubFnFromAct; * } * } */ export type Acts = LS extends (infer A)[] ? UnionToIntersection> : never; export type MapAct = LS extends (infer A)[] ? UnionToIntersection> : never; export type Subs = LS extends (infer A)[] ? UnionToIntersection> : never; export type Names = LS extends (infer A)[] ? NamesFromAct : never; export type ActExec = _ExecType; export type ActSubscribeType = _SubsType; export type ActEventName = _NameType; export type EmitAction = ActExec; export type ActProxy = _MapType; export {};