/** * MessageCenter - 主题订阅与消息路由系统 * * 提供发布/订阅模式的消息总线,支持主题通配符、 * 消息过滤、订阅者隔离等功能 */ export type Topic = string; export type TopicPattern = string; export type MessageHandler = (message: T, topic: string) => void | Promise; export interface Subscription { id: string; pattern: TopicPattern; handler: MessageHandler; once?: boolean; } export interface PublishOptions { /** * 是否异步执行订阅者回调 */ async?: boolean; /** * 订阅者执行超时时间(毫秒) */ timeout?: number; } export interface MessageCenterOptions { /** * 是否启用调试日志 */ debug?: boolean; /** * 订阅者默认超时时间(毫秒) */ defaultHandlerTimeout?: number; } export declare class MessageCenter { private subscriptions; private subscriptionIdCounter; private readonly options; private readonly debug; constructor(options?: Partial); /** * 发布消息到指定主题 */ publish(topic: Topic, message: T, options?: PublishOptions): Promise; /** * 订阅主题(支持通配符) * * 通配符规则: * - `*` 匹配所有主题 * - `foo:*` 匹配 `foo:` 开头的所有主题 * - `foo.bar` 精确匹配 `foo.bar` */ subscribe(pattern: TopicPattern, handler: MessageHandler, options?: { once?: boolean; }): string; /** * 取消订阅 */ unsubscribe(subscriptionId: string): boolean; /** * 取消指定模式的所有订阅 */ unsubscribeByPattern(pattern: TopicPattern): number; /** * 获取所有订阅信息(用于调试) */ getSubscriptions(): Array<{ topic: Topic; count: number; patterns: TopicPattern[]; }>; /** * 清除所有订阅 */ clear(): void; /** * 查找匹配指定主题的订阅 */ private findSubscriptions; /** * 判断订阅模式是否匹配目标主题 */ private matchPattern; /** * 解析主题模式 */ private parsePattern; /** * 生成订阅 ID */ private generateSubscriptionId; }