@flowlab/event
Version:
FlowLab event-driven system
154 lines (135 loc) • 3.91 kB
TypeScript
type EventPayload = any;
/**
* 事件处理器函数类型
*/
type EventHandler = (payload: EventPayload) => Promise<void>;
/**
* EventBus 接口定义,支持 emit/on/off/start/stop 等
*/
interface IEventBus {
emit(eventName: string, payload: EventPayload): Promise<void>;
on(eventName: string, handler: EventHandler): void;
off(eventName: string, handler: EventHandler): void;
start(): Promise<void>;
stop(): Promise<void>;
}
/**
* 事件总线选项
*/
interface EventBusOptions {
persistence?: boolean;
redisUrl?: string;
}
/**
* 事件注册信息
*/
interface EventRegistryEntry {
eventName: string;
handler: EventHandler;
}
/**
* 触发事件
* @param eventName - 事件名称
* @param payload - 传递的数据
*/
declare function emitEvent(eventName: string, payload: EventPayload): Promise<void>;
/**
* 绑定一个事件处理函数到指定事件
* @param eventName 事件名
* @param handler 处理函数
*/
declare function bindEvent(eventName: string, handler: EventHandler): void;
/**
* 从事件中解绑一个处理器(或所有)
* @param eventName 事件名
* @param handler 可选的具体处理器
*/
declare function unbindEvent(eventName: string, handler?: EventHandler): void;
/**
* 节点执行函数类型
* @template Input 节点输入类型
* @template Output 节点输出类型
* @template Context 上下文类型
*/
type NodeFunction<Input = any, Output = any, Context = any> = (input: Input, context: Context) => Promise<Output>;
/**
* 节点执行参数(可选)
*/
interface NodeOptions {
retries?: number;
timeout?: number;
condition?: (input: any, context?: any) => boolean;
}
/**
* Workflow 执行上下文
*/
interface WorkflowContext {
tenantId?: string;
userId?: string;
userRole?: string;
traceId?: string;
locale?: string;
[key: string]: any;
}
/**
* Workflow 是链式定义的工作流任务流实例
*/
declare class Workflow {
constructor(name: string);
addStep(
name: string,
fn: NodeFunction,
options?: NodeOptions
): this;
addParallelStep(
steps: {
name: string;
fn: NodeFunction;
options?: NodeOptions;
}[]
): this;
addCondition(
condition: (input: any, context: WorkflowContext) => string | boolean,
branches: Record<string, Workflow>
): this;
addSubWorkflow(name: string, workflow: Workflow): this;
run(input: any, context?: WorkflowContext): Promise<any>;
}
/**
* 将一个工作流绑定到事件上
* @param eventName 事件名称
* @param workflow 要运行的工作流实例
*/
declare function registerWorkflow(eventName: string, workflow: Workflow): void;
/**
* 启动事件监听器(如果是内存实现则为 noop)
*/
declare function startEventListener(): Promise<void>;
/**
* 停止事件监听器
*/
declare function stopEventListener(): Promise<void>;
/**
* 列出当前绑定的所有事件名(仅限内存实现)
*/
declare function listBoundEvents(): string[];
/**
* 判断某个事件是否已绑定处理器
* @param eventName 事件名
*/
declare function isEventBound(eventName: string): boolean;
/**
* 全局监听所有事件(仅支持 InMemory 实现)
* @param callback (eventName, payload) => void
*/
declare function onAnyEvent(callback: (eventName: string, payload: EventPayload) => void): void;
/**
* 获取当前事件总线实例
*/
declare function getCurrentEventBus(): IEventBus;
/**
* 替换当前使用的事件总线(可用于 Redis/Kafka)
* @param bus 自定义实现的事件总线
*/
declare function useEventBus(bus: IEventBus): void;
export { EventBusOptions, EventHandler, EventPayload, EventRegistryEntry, IEventBus, bindEvent, emitEvent, getCurrentEventBus, isEventBound, listBoundEvents, onAnyEvent, registerWorkflow, startEventListener, stopEventListener, unbindEvent, useEventBus };