/** * 检查类型是否为多级通配符(包含 **) * @description 判断字符串类型是否包含多级通配符 "**" * @example * type Result1 = IsMultiWildcard<"user/**">; * // Result1 = true * type Result2 = IsMultiWildcard<"user/*">; * // Result2 = false * type Result3 = IsMultiWildcard<**">; * // Result3 = true */ type IsMultiWildcard = T extends `${string}/**` | "**" ? true : false; type Join = T extends [ infer First extends string, ...infer Rest extends string[] ] ? Rest["length"] extends 0 ? First : `${First}${Delimiter}${Join}` : ""; type Split = S extends `${infer Head}${Delimiter}${infer Tail}` ? [Head, ...Split] : [S]; type DeepPartial = { [P in keyof T]?: T[P] extends object ? DeepPartial : T[P]; }; type ProcessSegment = S extends "*" ? `${string}` : S; type ProcessSegments = Arr extends [] ? [] : Arr extends [infer First extends string, ...infer Rest extends string[]] ? [ProcessSegment, ...ProcessSegments] : []; type ReplaceWildcard = T extends string ? IsMultiWildcard extends true ? T extends "**" ? `${string}/${string}` : T extends "*" ? string : T extends `${infer Head}/**` ? `${ReplaceWildcard}/${string}` : T : Join>> : T; /** * 事件消息相关 */ interface FastEventMeta { } interface FastEventMessageExtends { } type FastEventMessage = Record> = { type: T; payload: P; meta?: FastEventMeta & M & Record; } & FastEventMessageExtends; type WildcardStyle = `*` | `**` | `${string}/*` | `*/${string}` | `${string}/*/${string}` | `${string}/**`; type TypedFastEventMessage = Record, M = any> = ({ [K in keyof Events as K extends WildcardStyle ? ReplaceWildcard : never]: { type: ReplaceWildcard; payload: Events[K]; meta?: Partial & Record; }; } & { [K in keyof Events as K extends WildcardStyle ? never : K]: { type: Exclude; payload: Events[K]; meta?: Partial & M & Record; }; })[Exclude] & FastEventMessageExtends; type FastListenerExecutor = (listeners: FastEventListenerMeta[], message: TypedFastEventMessage, args: FastEventListenerArgs, execute: (this: FastEventListenerMeta, listener: FastEventListenerMeta, message: TypedFastEventMessage, args: FastEventListenerArgs, catchErrors?: boolean) => Promise | any) => Promise | any[]; /** * 事件相关 */ declare enum FastEventListenerFlags { Transformed = 1 } type FastEventListenerArgs> = { retain?: boolean; meta?: DeepPartial & Record; abortSignal?: AbortSignal; /** * * allSettled: 使用Promise.allSettled()执行所有监听器 * race: 使用Promise.race()执行所有监听器,只有第一个执行完成就返回,其他监听器执行结果会被忽略 * balance: 尽可能平均执行各个监听器 * sequence: 按照监听器添加顺序依次执行 */ executor?: FastListenerExecutor; /** * 当emit参数解析完成后的回调,用于修改emit参数 */ parseArgs?: (message: TypedFastEventMessage, args: FastEventListenerArgs) => void; /** * 额外的标识 * * - 1: transformed 当消息是经过transform转换后的消息时的标识 * */ flags?: FastEventListenerFlags; /** * 如果消息经过转换前的原主题 */ rawEventType?: string; /** * 发布端前缀广播(当前仅 FastLiteEvent 实现运行时支持) * * 开启后,本次 emit 除命中自身路径外,同时唤醒 emit 终点节点 * 子树(不含终点自身)内所有已订阅监听器——含通配符订阅 * (`*` / `**`)与具体后代路径,每个后代收到改写为自身订阅 * 路径的事件消息。方向仅向下。 * * - `true`:默认改写,自动把后代监听器的 message.type 替换为 * 该后代完整路径,其余字段原样透传。 * - 函数:逐个后代调用(后代按 DFS 先序),返回 * `[message, args]` 覆盖原消息与参数;返回 `null`/falsy 跳过该后代。 * * 注:正常匹配(emit 路径直接命中)不经 broadcast 改写。 */ broadcast?: true | ((type: string, message: FastEventMessage, args: FastEventListenerArgs) => [FastEventMessage, FastEventListenerArgs] | FastEventMessage | null); }; type TypedFastEventListener = (this: C, message: TypedFastEventMessage, M>, args: FastEventListenerArgs) => any | Promise; /** * [ * 0: 监听器函数引用, * 1: 需要执行多少次, =0代表不限 * 2: 实际执行的次数(用于负载均衡时记录) * 3: 标签 用于调试一般可以标识监听器类型或任意信息 * 4: 标识, * 5: 监听器最后一次执行结果,仅仅在debug时启用,如果结果是对象则是一个WeakRef * ] */ type FastEventListenerMeta = [ TypedFastEventListener, number, number, string, number, any? ]; type FastListenerPipe = (listener: TypedFastEventListener) => TypedFastEventListener; interface FastEventListenerDecorators { queue: string; } type FastQueuePriority = "none"; type FastQueueOverflows = "drop" | "expand" | "slide" | "throw"; type QueueListenerPipeOptions = { size?: number; maxExpandSize?: number; expandOverflow?: Omit; overflow?: FastQueueOverflows; lifetime?: number; onPush?: (newMessage: TypedFastEventMessage, messages: [TypedFastEventMessage, number][]) => void; onPop?: (messages: [TypedFastEventMessage, number][], hasNew: boolean) => [TypedFastEventMessage, number] | undefined; onDrop?: (message: TypedFastEventMessage) => void; }; declare const queue: (options?: QueueListenerPipeOptions) => FastListenerPipe; declare const dropping: (size?: number) => FastListenerPipe; declare const sliding: (size?: number) => FastListenerPipe; declare const expanding: (options?: Omit) => FastListenerPipe; interface RetryListenerPipeOptions { retries?: number; interval?: number | ((retryCount: number) => number); drop?: (message: TypedFastEventMessage, error: Error) => void; } /** * 创建一个重试管道,在监听器执行失败时自动重试 * @param retries 最大重试次数 * @param options 配置选项 * @returns FastListenerPipe */ declare function retry(options?: RetryListenerPipeOptions): FastListenerPipe; /** * 创建一个超时装饰器,限制监听器函数的执行时间 * @param ms 超时时间(毫秒) * @param defaultValue 可选的默认返回值,如果提供则超时时返回此值,否则抛出TimeoutError * @returns 装饰器函数 */ declare const timeout: (ms: number, defaultValue?: T) => FastListenerPipe; interface DebounceOptions { /** * 当消息被丢弃时的回调函数 */ drop?: (message: TypedFastEventMessage) => void; } /** * 创建一个防抖动装饰器,限制监听器函数的执行频率 * @param ms 防抖动时间(毫秒) * @param options 可选的配置项 * @returns 装饰器函数 */ declare const debounce: (ms: number, options?: DebounceOptions) => FastListenerPipe; interface ThrottleOptions { /** * 当消息被丢弃时的回调函数 */ drop?: (message: TypedFastEventMessage) => void; } /** * 创建一个节流装饰器,限制监听器函数的执行频率 * @param interval 节流时间间隔(毫秒) * @param options 可选的配置项 * @returns 装饰器函数 */ declare const throttle: (interval: number, options?: ThrottleOptions) => FastListenerPipe; /** * 创建一个memorize pipe,用于缓存上一次的返回值 * @param predicate 可选的判断函数,用于决定是否使用缓存 * @returns FastListenerPipe */ declare function memorize(predicate?: (message: TypedFastEventMessage, args: FastEventListenerArgs) => boolean | Promise): FastListenerPipe; export { type DebounceOptions, type FastEventListenerDecorators, type FastListenerPipe, type FastQueueOverflows, type FastQueuePriority, type QueueListenerPipeOptions, type RetryListenerPipeOptions, type ThrottleOptions, debounce, dropping, expanding, memorize, queue, retry, sliding, throttle, timeout };