fastevent 2.0.2 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1005 @@
1
+ type FastListenerExecutor = (listeners: FastListenerMeta[], message: FastEventMessage, args: FastEventListenerArgs, execute: (listener: FastEventListener, message: FastEventMessage, args: FastEventListenerArgs, catchErrors?: boolean) => Promise<any> | any) => Promise<any[]> | any[];
2
+
3
+ type FastListenerPipe = (listener: FastEventListener) => FastEventListener;
4
+
5
+ interface FaseEventMessageExtends {
6
+ }
7
+ interface FastEventMeta {
8
+ }
9
+ type FastEventMessage<Events extends Record<string, any> = Record<string, any>, M = any> = ({
10
+ [K in keyof Events]: {
11
+ type: Exclude<K, number | symbol>;
12
+ payload: Events[K];
13
+ meta: FastEventMeta & M & Record<string, any>;
14
+ };
15
+ }[Exclude<keyof Events, number | symbol>]) & FaseEventMessageExtends;
16
+ type FastEventEmitMessage<Events extends Record<string, any> = Record<string, any>, M = any> = ({
17
+ [K in keyof Events]: {
18
+ type: Exclude<K, number | symbol>;
19
+ payload?: Events[K];
20
+ meta?: Partial<FastEventMeta> & M & Record<string, any>;
21
+ };
22
+ }[Exclude<keyof Events, number | symbol>]) & FaseEventMessageExtends;
23
+ type FastEventListener<T extends string = string, P = any, M = any, C = any> = (this: C, message: FastEventMessage<{
24
+ [K in T]: P;
25
+ }, M>, args: FastEventListenerArgs<M>) => any | Promise<any>;
26
+ type FastEventAnyListener<Events extends Record<string, any> = Record<string, any>, Meta = never, Context = any> = (this: Context, message: FastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => any | Promise<any>;
27
+ /**
28
+ * [监听器函数引用,需要执行多少次,实际执行的次数(用于负载均衡时记录)]
29
+ */
30
+ type FastListenerMeta = [FastEventListener<any, any>, number, number];
31
+ type FastListenerNode = {
32
+ __listeners: FastListenerMeta[];
33
+ } & {
34
+ [key: string]: FastListenerNode;
35
+ };
36
+ type FastEventSubscriber = {
37
+ off: () => void;
38
+ /**
39
+ * 为什么要有一个listener引用? 主要用于移除监听器时使用
40
+ *
41
+ * - 正常情况下
42
+ * const subscriber = emitter.on('event', listener)
43
+ *
44
+ * subscriber.off()
45
+ * emitter.off('event', listener)
46
+ * emitter.off(listener)
47
+ *
48
+ * - 在使用scope时
49
+ * const scope = emitter.scope("xxx")
50
+ * const subscriber = scope.on('event', listener)
51
+ *
52
+ * subscriber.off() 可以正常生效
53
+ * scope.off('event', listener) // 无法生效
54
+ * scope.off(listener) // 无法生效
55
+ * 因为在scope中,为了让监听器可以处理scope的逻辑,对listener进行了包装,
56
+ * 因此在事件注册表中登记的不是listener,而是经过包装的监听器
57
+ * subscriber.off() 可以正常生效
58
+ * 如果要使用scope.off或emitter.off
59
+ * 需要使用subscriber.listener, subscriber.listener记录了原始的监听器引用
60
+ * subscriber.listener===listener
61
+ *
62
+ * scope.off('event', subscriber.listener) // 生效
63
+ * scope.off(subscriber.listener) // 生效
64
+ *
65
+ */
66
+ listener: FastEventListener<any, any, any>;
67
+ };
68
+ type FastListeners = FastListenerNode;
69
+ type FastEventOptions<Meta = Record<string, any>, Context = any> = {
70
+ id: string;
71
+ debug: boolean;
72
+ delimiter: string;
73
+ context: Context;
74
+ ignoreErrors: boolean;
75
+ meta: Meta;
76
+ onAddListener?: (type: string, listener: FastEventListener, options: FastEventListenOptions<Record<string, any>, Meta>) => boolean | FastEventSubscriber | void;
77
+ onRemoveListener?: (type: string, listener: FastEventListener) => void;
78
+ onClearListeners?: () => void;
79
+ onListenerError?: ((error: Error, listener: FastEventListener, message: FastEventMessage<any, Meta>, args: FastEventListenerArgs<Meta> | undefined) => void);
80
+ onBeforeExecuteListener?: (message: FastEventMessage<any, Meta>, args: FastEventListenerArgs<Meta>) => boolean | void | any[];
81
+ onAfterExecuteListener?: (message: FastEventMessage<any, Meta>, returns: any[], listeners: FastListenerNode[]) => void;
82
+ /**
83
+ * 全局执行器
84
+ */
85
+ executor?: FastListenerExecutor;
86
+ onMessage?: FastEventListener;
87
+ expandEmitResults?: boolean;
88
+ };
89
+ interface FastEvents {
90
+ }
91
+ type PickScopeEvents<T extends Record<string, any>, Prefix extends string> = {
92
+ [K in keyof T as K extends `${Prefix}/${infer Rest}` ? Rest : never]: T[K];
93
+ };
94
+ type ScopeEvents<T extends Record<string, any>, Prefix extends string> = PickScopeEvents<T, Prefix>;
95
+ type FastEventListenOptions<Events extends Record<string, any> = Record<string, any>, Meta = any> = {
96
+ count?: number;
97
+ prepend?: boolean;
98
+ filter?: (message: FastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => boolean;
99
+ off?: (message: FastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => boolean;
100
+ pipes?: FastListenerPipe[];
101
+ };
102
+ type FastEventListenerArgs<M = Record<string, any>> = {
103
+ retain?: boolean;
104
+ meta?: Partial<M> & Record<string, any>;
105
+ abortSignal?: AbortSignal;
106
+ /**
107
+ *
108
+ * allSettled: 使用Promise.allSettled()执行所有监听器
109
+ * race: 使用Promise.race()执行所有监听器,只有第一个执行完成就返回,其他监听器执行结果会被忽略
110
+ * balance: 尽可能平均执行各个监听器
111
+ * sequence: 按照监听器添加顺序依次执行
112
+ */
113
+ executor?: FastListenerExecutor;
114
+ /**
115
+ * 当emit参数解析完成后的回调,用于修改emit参数
116
+ */
117
+ parseArgs?: (message: FastEventMessage, args: FastEventListenerArgs) => void;
118
+ };
119
+ type Merge<T extends object, U extends object> = {
120
+ [K in keyof T | keyof U]: K extends keyof U ? U[K] : K extends keyof T ? T[K] : never;
121
+ };
122
+ type RequiredItems<T extends object, Items extends string[]> = Omit<T, Items[number]> & {
123
+ [K in Items[number] & keyof T]-?: Exclude<T[K], undefined>;
124
+ };
125
+ type Expand<T> = T extends infer O ? {
126
+ [K in keyof O]: O[K];
127
+ } : never;
128
+ type OptionalItems<T, K extends keyof T> = Expand<Omit<T, K> & {
129
+ [P in K]?: T[P];
130
+ }>;
131
+ type DeepPartial<T> = {
132
+ [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
133
+ };
134
+ type Fallback<T, F> = [
135
+ T
136
+ ] extends [never] ? F : T extends undefined ? F : T;
137
+ type ChangeFieldType<Record, Name extends string, Type = any> = Expand<Omit<Record, Name> & {
138
+ [K in Name]: Type;
139
+ }>;
140
+ type OverrideOptions<T> = ChangeFieldType<Required<T>, 'context', never>;
141
+ type ObjectKeys<T, I = string> = {
142
+ [P in keyof T]: P extends I ? P : never;
143
+ }[keyof T];
144
+ /**
145
+ * 从类型数组中移除重复项,返回保留唯一类型的元组
146
+ * @template T - 输入的任意类型数组(元组)
147
+ * @template Result - 内部使用的累积结果数组(默认空数组)
148
+ * @returns {any[]} 去重后的类型元组,保留首次出现的顺序
149
+ *
150
+ * @example
151
+ * type T1 = Unique<[number, string, number]>; // [number, string]
152
+ * type T2 = Unique<[1, 2, 2, 3]>; // [1, 2, 3]
153
+ * type T3 = Unique<['a', 'b', 'a']>; // ['a', 'b']
154
+ */
155
+ type Unique<T extends any[], Result extends any[] = []> = T extends [infer First, ...infer Rest] ? First extends Result[number] ? Unique<Rest, Result> : Unique<Rest, [...Result, First]> : Result;
156
+ type Overloads<T> = Unique<T extends {
157
+ (...args: infer A1): infer R1;
158
+ (...args: infer A2): infer R2;
159
+ (...args: infer A3): infer R3;
160
+ (...args: infer A4): infer R4;
161
+ (...args: infer A5): infer R5;
162
+ (...args: infer A6): infer R6;
163
+ (...args: infer A7): infer R7;
164
+ (...args: infer A8): infer R8;
165
+ } ? [
166
+ (...args: A1) => R1,
167
+ ((...args: A2) => R2),
168
+ ((...args: A3) => R3),
169
+ ((...args: A4) => R4),
170
+ ((...args: A5) => R5),
171
+ ((...args: A6) => R6),
172
+ ((...args: A7) => R7),
173
+ ((...args: A8) => R8)
174
+ ] : T extends {
175
+ (...args: infer A1): infer R1;
176
+ (...args: infer A2): infer R2;
177
+ (...args: infer A3): infer R3;
178
+ (...args: infer A4): infer R4;
179
+ (...args: infer A5): infer R5;
180
+ (...args: infer A6): infer R6;
181
+ (...args: infer A7): infer R7;
182
+ } ? [
183
+ (...args: A1) => R1,
184
+ ((...args: A2) => R2),
185
+ ((...args: A3) => R3),
186
+ ((...args: A4) => R4),
187
+ ((...args: A5) => R5),
188
+ ((...args: A6) => R6),
189
+ ((...args: A7) => R7)
190
+ ] : T extends {
191
+ (...args: infer A1): infer R1;
192
+ (...args: infer A2): infer R2;
193
+ (...args: infer A3): infer R3;
194
+ (...args: infer A4): infer R4;
195
+ (...args: infer A5): infer R5;
196
+ (...args: infer A6): infer R6;
197
+ } ? [
198
+ (...args: A1) => R1,
199
+ ((...args: A2) => R2),
200
+ ((...args: A3) => R3),
201
+ ((...args: A4) => R4),
202
+ ((...args: A5) => R5),
203
+ ((...args: A6) => R6)
204
+ ] : T extends {
205
+ (...args: infer A1): infer R1;
206
+ (...args: infer A2): infer R2;
207
+ (...args: infer A3): infer R3;
208
+ (...args: infer A4): infer R4;
209
+ (...args: infer A5): infer R5;
210
+ } ? [
211
+ (...args: A1) => R1,
212
+ ((...args: A2) => R2),
213
+ ((...args: A3) => R3),
214
+ ((...args: A4) => R4),
215
+ ((...args: A5) => R5)
216
+ ] : T extends {
217
+ (...args: infer A1): infer R1;
218
+ (...args: infer A2): infer R2;
219
+ (...args: infer A3): infer R3;
220
+ (...args: infer A4): infer R4;
221
+ } ? [
222
+ (...args: A1) => R1,
223
+ ((...args: A2) => R2),
224
+ ((...args: A3) => R3),
225
+ ((...args: A4) => R4)
226
+ ] : T extends {
227
+ (...args: infer A1): infer R1;
228
+ (...args: infer A2): infer R2;
229
+ (...args: infer A3): infer R3;
230
+ } ? [
231
+ ((...args: A1) => R1),
232
+ ((...args: A2) => R2),
233
+ ((...args: A3) => R3)
234
+ ] : T extends {
235
+ (...args: infer A1): infer R1;
236
+ (...args: infer A2): infer R2;
237
+ } ? [
238
+ ((...args: A1) => R1),
239
+ ((...args: A2) => R2)
240
+ ] : T extends {
241
+ (...args: infer A1): infer R1;
242
+ } ? [
243
+ (...args: A1) => R1
244
+ ] : [
245
+ T
246
+ ]>;
247
+ type Dict<V = any> = Record<Exclude<string, number | symbol>, V>;
248
+
249
+ type FastEventBusMessage<Events extends Record<string, any> = Record<string, any>, M = any> = FastEventEmitMessage<Events, M> & {
250
+ from?: string;
251
+ to?: string;
252
+ };
253
+ type FastEventBusOptions<Meta = Record<string, any>, Context = any> = FastEventOptions<Meta, Context>;
254
+ interface FastEventBusEvents {
255
+ '$connect': string;
256
+ '$disconnect': string;
257
+ 'data': any;
258
+ }
259
+ type FastEventBusEventTypes = keyof FastEventBusEvents;
260
+ interface FastEventBusNodes {
261
+ }
262
+ type FastEventBusNodeIds = keyof FastEventBusNodes;
263
+
264
+ type FastEventScopeOptions<Meta = Record<string, any>, Context = any> = {
265
+ meta: FastEventScopeMeta & FastEventMeta & Meta;
266
+ context: Context;
267
+ executor?: FastListenerExecutor;
268
+ onMessage?: FastEventListener;
269
+ };
270
+ type FastEventScopeMeta = {
271
+ scope: string;
272
+ };
273
+ declare class FastEventScope<Events extends Record<string, any> = Record<string, any>, Meta extends Record<string, any> = Record<string, any>, Context = never, Types extends keyof Events = keyof Events, FinalMeta extends Record<string, any> = FastEventMeta & FastEventScopeMeta & Meta> {
274
+ __FastEventScope__: boolean;
275
+ private _options;
276
+ types: {
277
+ events: Events;
278
+ meta: FinalMeta;
279
+ context: Fallback<Context, typeof this>;
280
+ };
281
+ prefix: string;
282
+ emitter: FastEvent<Events>;
283
+ constructor(options?: DeepPartial<FastEventScopeOptions<FinalMeta, Context>>);
284
+ get context(): Fallback<Context, typeof this>;
285
+ get options(): FastEventScopeOptions<FinalMeta, Context>;
286
+ bind(emitter: FastEvent<any>, prefix: string, options?: DeepPartial<FastEventScopeOptions<FinalMeta, Context>>): void;
287
+ /**
288
+ * 获取作用域监听器
289
+ * 当启用作用域时,对原始监听器进行包装,添加作用域前缀处理逻辑
290
+ * @param listener 原始事件监听器
291
+ * @returns 包装后的作用域监听器
292
+ * @private
293
+ */
294
+ private _getScopeListener;
295
+ private _getScopeType;
296
+ private _fixScopeType;
297
+ on<T extends Types = Types>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
298
+ on<T extends string>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
299
+ on(type: '**', options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
300
+ on<T extends Types = Types>(type: T, listener: FastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
301
+ on<T extends string>(type: T, listener: FastEventListener<string, any, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
302
+ on(type: '**', listener: FastEventAnyListener<Events, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
303
+ once<T extends Types = Types>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
304
+ once<T extends string>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
305
+ once<T extends Types = Types>(type: T, listener: FastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
306
+ once<T extends string>(type: T, listener: FastEventListener<string, any, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
307
+ onAny(options?: Pick<FastEventListenOptions, 'prepend'>): FastEventSubscriber;
308
+ onAny<P = any>(listener: FastEventAnyListener<{
309
+ [K: string]: P;
310
+ }, FinalMeta, Fallback<Context, typeof this>>, options?: Pick<FastEventListenOptions, 'prepend'>): FastEventSubscriber;
311
+ off(listener: FastEventListener<any, any, any>): void;
312
+ off(type: string, listener: FastEventListener<any, any, any>): void;
313
+ off(type: Types, listener: FastEventListener<any, any, any>): void;
314
+ off(type: string): void;
315
+ off(type: Types): void;
316
+ offAll(): void;
317
+ clear(): void;
318
+ emit(type: Types, directive: symbol): void;
319
+ emit(type: string, directive: symbol): void;
320
+ emit<R = any>(type: Types, payload?: Events[Types], options?: FastEventListenerArgs<FinalMeta>): R[];
321
+ emit<R = any, T extends string = string>(type: T, payload?: T extends Types ? Events[Types] : any, options?: FastEventListenerArgs<FinalMeta>): R[];
322
+ emit<R = any>(message: FastEventEmitMessage<Events, FinalMeta>, options?: FastEventListenerArgs<FinalMeta>): R[];
323
+ emit<R = any, T extends string = string>(message: FastEventEmitMessage<{
324
+ [K in T]: K extends Types ? Events[K] : any;
325
+ }, FinalMeta>, options?: FastEventListenerArgs<FinalMeta>): R[];
326
+ emitAsync<R = any>(type: string, payload?: any, options?: FastEventListenerArgs<FinalMeta>): Promise<[R | Error][]>;
327
+ emitAsync<R = any>(type: Types, payload?: Events[Types], options?: FastEventListenerArgs<FinalMeta>): Promise<[R | Error][]>;
328
+ emitAsync<R = any>(message: FastEventMessage<Events, FinalMeta>, options?: FastEventListenerArgs<FinalMeta>): Promise<[R | Error][]>;
329
+ waitFor<T extends Types>(type: T, timeout?: number): Promise<FastEventMessage<{
330
+ [key in T]: Events[T];
331
+ }, FinalMeta>>;
332
+ waitFor(type: string, timeout?: number): Promise<FastEventMessage<{
333
+ [key: string]: any;
334
+ }, FinalMeta>>;
335
+ waitFor<P = any>(type: string, timeout?: number): Promise<FastEventMessage<{
336
+ [key: string]: P;
337
+ }, FinalMeta>>;
338
+ /**
339
+ * 创建一个新的作用域实例
340
+ * @param prefix - 作用域前缀
341
+ * @returns 新的FastEventScope实例
342
+ *
343
+ * @description
344
+ * 基于当前作用域创建一个新的子作用域。新作用域会继承当前作用域的所有特性,
345
+ * 并在事件类型前添加额外的前缀。这允许创建层级化的事件命名空间。
346
+ *
347
+ * 作用域的特性:
348
+ * - 自动为所有事件类型添加前缀
349
+ * - 在触发事件时自动添加前缀
350
+ * - 在接收事件时自动移除前缀
351
+ * - 支持多层级的作用域嵌套
352
+ *
353
+ * @example
354
+ * ```ts
355
+ * const emitter = new FastEvent();
356
+ * const userScope = emitter.scope('user');
357
+ * const profileScope = userScope.scope('profile');
358
+ *
359
+ * // 在profileScope中监听'update'事件
360
+ * // 实际监听的是'user/profile/update'
361
+ * profileScope.on('update', (data) => {
362
+ * console.log('Profile updated:', data);
363
+ * });
364
+ *
365
+ * // 在profileScope中触发'update'事件
366
+ * // 实际触发的是'user/profile/update'
367
+ * profileScope.emit('update', { name: 'John' });
368
+ * ```
369
+ */
370
+ scope<E extends Record<string, any> = Record<string, any>, P extends string = string, M extends Record<string, any> = Record<string, any>, C = Context>(prefix: P, options?: DeepPartial<FastEventScopeOptions<Partial<FinalMeta> & M, C>>): FastEventScope<ScopeEvents<Events, P> & E, FinalMeta & M, C>;
371
+ scope<E extends Record<string, any> = Record<string, any>, P extends string = string, M extends Record<string, any> = Record<string, any>, C = Context, ScopeInstance extends FastEventScope<any, any, any> = FastEventScope<ScopeEvents<ScopeEvents<Events, P> & E, P> & E, FinalMeta & M, C>>(prefix: P, scopeObj: ScopeInstance, options?: DeepPartial<FastEventScopeOptions<Partial<FinalMeta> & M, C>>): ScopeInstance;
372
+ /**
373
+ * 当on/once/onAny未指定监听器时,此为默认监听器
374
+ * @param message
375
+ */
376
+ onMessage(message: FastEventMessage<Events, FinalMeta>, args: FastEventListenerArgs<FinalMeta>): void;
377
+ }
378
+
379
+ /**
380
+ * FastEvent 事件发射器类
381
+ *
382
+ * @template Events - 事件类型定义,继承自FastEvents接口
383
+ * @template Meta - 事件元数据类型,默认为任意键值对对象
384
+ * @template Types - 事件类型的键名类型,默认为Events的键名类型
385
+ */
386
+ declare class FastEvent<Events extends Record<string, any> = Record<string, any>, Meta extends Record<string, any> = Record<string, any>, Context = never, AllEvents extends Record<string, any> = Events & FastEvents, Types extends keyof AllEvents = Expand<Exclude<keyof (AllEvents), number | symbol>>> {
387
+ __FastEvent__: boolean;
388
+ /** 事件监听器树结构,存储所有注册的事件监听器 */
389
+ listeners: FastListeners;
390
+ /** 事件发射器的配置选项 */
391
+ private _options;
392
+ /** 事件名称的分隔符,默认为'/' */
393
+ private _delimiter;
394
+ /** 事件监听器执行时的上下文对象 */
395
+ private _context;
396
+ /** 保留的事件消息映射,Key是事件名称,Value是保留的事件消息 */
397
+ retainedMessages: Map<string, any>;
398
+ /** 当前注册的监听器总数 */
399
+ listenerCount: number;
400
+ types: {
401
+ events: AllEvents;
402
+ meta: Expand<FastEventMeta & Meta & Record<string, any>>;
403
+ context: Expand<Fallback<Context, typeof this>>;
404
+ };
405
+ /**
406
+ * 创建FastEvent实例
407
+ * @param options - 事件发射器的配置选项
408
+ *
409
+ * 默认配置:
410
+ * - debug: false - 是否启用调试模式
411
+ * - id: 随机字符串 - 实例唯一标识符
412
+ * - delimiter: '/' - 事件名称分隔符
413
+ * - context: null - 监听器执行上下文
414
+ * - ignoreErrors: true - 是否忽略监听器执行错误
415
+ */
416
+ constructor(options?: Partial<FastEventOptions<Meta, Context>>);
417
+ /** 获取事件发射器的配置选项 */
418
+ get options(): FastEventOptions<Meta, Context>;
419
+ get context(): Fallback<Context, typeof this>;
420
+ get meta(): Meta;
421
+ /** 获取事件发射器的唯一标识符 */
422
+ get id(): string;
423
+ /**
424
+ * 添加事件监听器到事件树中
425
+ * @param parts - 事件路径数组
426
+ * @param listener - 事件监听器函数
427
+ * @param options - 监听器配置选项
428
+ * @param options.count - 监听器触发次数限制
429
+ * @param options.prepend - 是否将监听器添加到监听器列表开头
430
+ * @returns [节点, 监听器索引] - 返回监听器所在节点和在节点监听器列表中的索引
431
+ * @private
432
+ */
433
+ private _addListener;
434
+ private _enableDevTools;
435
+ /**
436
+ *
437
+ * 根据parts路径遍历监听器树,并在最后的节点上执行回调函数
438
+ *
439
+ * @param parts
440
+ * @param callback
441
+ * @returns
442
+ */
443
+ private _forEachNodes;
444
+ /**
445
+ * 从监听器节点中移除指定的事件监听器
446
+ * @private
447
+ * @param node - 监听器节点
448
+ * @param listener - 需要移除的事件监听器
449
+ * @description 遍历节点的监听器列表,移除所有匹配的监听器。支持移除普通函数和数组形式的监听器
450
+ */
451
+ private _removeListener;
452
+ private _pipeListener;
453
+ /**
454
+ * 注册事件监听器
455
+ * @param type - 事件类型,支持以下格式:
456
+ * - 普通字符串:'user/login'
457
+ * - 通配符:'user/*'(匹配单层)或'user/**'(匹配多层)
458
+ * - 全局监听:'**'(监听所有事件)
459
+ * @param listener - 事件监听器函数
460
+ * @param options - 监听器配置选项:
461
+ * - count: 触发次数限制,0表示无限制
462
+ * - prepend: 是否将监听器添加到监听器队列开头
463
+ * @returns 返回订阅者对象,包含off方法用于取消监听
464
+ *
465
+ * @example
466
+ * ```ts
467
+ * // 监听特定事件
468
+ * emitter.on('user/login', (data) => console.log(data));
469
+ *
470
+ * // 使用通配符
471
+ * emitter.on('user/*', (data) => console.log(data));
472
+ *
473
+ * // 限制触发次数
474
+ * emitter.on('event', handler, { count: 3 });
475
+ * ```
476
+ */
477
+ on<T extends Types = Types>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
478
+ on<T extends string>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
479
+ on(type: '**', options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
480
+ on<T extends Types = Types>(type: T, listener: FastEventListener<Exclude<T, number | symbol>, AllEvents[T], Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
481
+ on<T extends string>(type: T, listener: FastEventAnyListener<AllEvents, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
482
+ on(type: '**', listener: FastEventAnyListener<Record<string, any>, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
483
+ /**
484
+ * 注册一次性事件监听器
485
+ * @param type - 事件类型,支持与on方法相同的格式:
486
+ * - 普通字符串:'user/login'
487
+ * - 通配符:'user/*'(匹配单层)或'user/**'(匹配多层)
488
+ * @param listener - 事件监听器函数
489
+ * @returns 返回订阅者对象,包含off方法用于取消监听
490
+ *
491
+ * @description
492
+ * 监听器只会在事件首次触发时被调用一次,之后会自动解除注册。
493
+ * 这是on方法的特例,相当于设置options.count = 1。
494
+ * 如果事件有保留消息,新注册的监听器会立即收到最近一次的保留消息并解除注册。
495
+ *
496
+ * @example
497
+ * ```ts
498
+ * // 只监听一次登录事件
499
+ * emitter.once('user/login', (data) => {
500
+ * console.log('用户登录:', data);
501
+ * });
502
+ * ```
503
+ */
504
+ once<T extends Types = Types>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
505
+ once<T extends string>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
506
+ once<T extends Types = Types>(type: T, listener: FastEventListener<Exclude<T, number | symbol>, AllEvents[T], Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
507
+ once<T extends string>(type: T, listener: FastEventAnyListener<AllEvents, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
508
+ /**
509
+ * 注册一个监听器,用于监听所有事件
510
+ * @param listener 事件监听器函数,可以接收任意类型的事件数据
511
+ * @returns {FastEventSubscriber} 返回一个订阅者对象,包含 off 方法用于取消监听
512
+ * @example
513
+ * ```ts
514
+ * const subscriber = emitter.onAny((eventName, data) => {
515
+ * console.log(eventName, data);
516
+ * });
517
+ *
518
+ * // 取消监听
519
+ * subscriber.off();
520
+ * ```listener: FastEventAnyListener<AllEvents, Meta, Fallback<Context, typeof this>>): FastEventSubscriber
521
+ */
522
+ onAny(options?: Omit<FastEventListenOptions<AllEvents, Meta>, 'count'>): FastEventSubscriber;
523
+ onAny<P = any>(listener: FastEventAnyListener<Record<string, P>, Meta, Fallback<Context, typeof this>>, options?: Omit<FastEventListenOptions<AllEvents, Meta>, 'count'>): FastEventSubscriber;
524
+ /**
525
+ *
526
+ * 当调用on/once/onAny时如果没有指定监听器,则调用此方法
527
+ *
528
+ * 此方法供子类继承
529
+ *
530
+ */
531
+ onMessage(message: FastEventMessage<AllEvents, Meta>, args: FastEventListenerArgs<Meta>): void;
532
+ off(listener: FastEventListener<any, any, any>): void;
533
+ off(type: string, listener: FastEventListener<any, any, any>): void;
534
+ off(type: Types, listener: FastEventListener<any, any, any>): void;
535
+ off(type: string): void;
536
+ off(type: Types): void;
537
+ /**
538
+ * 移除所有事件监听器
539
+ * @param entry - 可选的事件前缀,如果提供则只移除指定前缀下的的监听器
540
+ * @description
541
+ * - 如果提供了prefix参数,则只清除该前缀下的所有监听器
542
+ * - 如果没有提供prefix,则清除所有监听器
543
+ * - 同时会清空保留的事件(_retainedEvents)
544
+ * - 重置监听器对象为空
545
+
546
+ * @example
547
+ *
548
+ * ```ts
549
+ * emitter.offAll(); // 清除所有监听器
550
+ * emitter.offAll('a/b'); // 清除a/b下的所有监听器
551
+ *
552
+ */
553
+ offAll(entry?: string): void;
554
+ /**
555
+ * 移除保留的事件
556
+ * @param prefix - 事件前缀。如果不提供,将清除所有保留的事件。
557
+ * 如果提供前缀,将删除所有以该前缀开头的事件。
558
+ * 如果前缀不以分隔符结尾,会自动添加分隔符。
559
+ * @private
560
+ */
561
+ private _removeRetainedEvents;
562
+ clear(prefix?: string): void;
563
+ /**
564
+ * 发送最后一次事件的消息给对应的监听器
565
+ *
566
+ * @param type - 事件类型,支持通配符(*)匹配
567
+ * @private
568
+ *
569
+ * 处理流程:
570
+ * 1. 如果事件类型包含通配符,则遍历所有保留的消息,匹配符合模式的事件
571
+ * 2. 如果是普通事件类型,则直接获取对应的保留消息
572
+ * 3. 遍历匹配到的消息,查找对应路径的监听器节点
573
+ * 4. 执行所有匹配到的监听器
574
+ */
575
+ private _emitRetainMessage;
576
+ /**
577
+ * 遍历监听器节点树
578
+ * @param node 当前遍历的监听器节点
579
+ * @param parts 事件名称按'.'分割的部分数组
580
+ * @param callback 遍历到目标节点时的回调函数
581
+ * @param index 当前遍历的parts数组索引,默认为0
582
+ * @param lastFollowing 当命中**时该值为true, 注意**只能作在路径的最后面,如a.**有效,而a.**.b无效
583
+ * @private
584
+ *
585
+ * 支持三种匹配模式:
586
+ * - 精确匹配: 'a.b.c'
587
+ * - 单层通配: 'a.*.c'
588
+ * - 多层通配: 'a.**'
589
+ */
590
+ private _traverseToPath;
591
+ private _traverseListeners;
592
+ private _onListenerError;
593
+ /**
594
+ * 执行单个监听器函数
595
+ * @param listener - 要执行的监听器函数或包装过的监听器对象
596
+ * @param message - 事件消息对象,包含type、payload和meta
597
+ * @param args - 监听器参数
598
+ * @param catchErrors - 是否捕获并处理执行过程中的错误
599
+ * @returns 监听器的执行结果或错误对象(如果配置了ignoreErrors)
600
+ * @private
601
+ *
602
+ * @description
603
+ * 执行单个监听器函数,处理以下情况:
604
+ * - 如果监听器是包装过的(有__wrappedListener属性),调用包装的函数
605
+ * - 否则直接调用监听器函数
606
+ * - 使用配置的上下文(_context)作为this
607
+ * - 捕获并处理执行过程中的错误:
608
+ * - 如果有onListenerError回调,调用它
609
+ * - 如果配置了ignoreErrors,返回错误对象
610
+ * - 否则抛出错误
611
+ */
612
+ private _executeListener;
613
+ private _getListenerExecutor;
614
+ /**
615
+ * 执行监听器节点列表中的所有监听器函数
616
+ * @param nodes 监听器节点列表
617
+ * @param message 事件消息对象
618
+ * @param args 监听器参数
619
+ * @param args 监听器参数
620
+ * @returns 所有监听器函数的执行结果数组
621
+ * @private
622
+ *
623
+ * 执行流程:
624
+ * 1. 将所有节点中的监听器函数展平为一维数组
625
+ * 2. 通过执行器执行所有监听器函数
626
+ * 3. 更新监听器的执行次数,并移除达到执行次数限制的监听器
627
+ */
628
+ private _executeListeners;
629
+ /**
630
+ * 触发事件并执行对应的监听器
631
+ *
632
+ * @param type - 事件类型字符串或包含事件信息的对象
633
+ * @param payload - 事件携带的数据负载
634
+ * @param retain - 是否保留该事件(用于新订阅者)
635
+ * @param meta - 事件元数据
636
+ * @returns 所有监听器的执行结果数组
637
+ *
638
+ * @example
639
+ * // 方式1: 参数形式
640
+ * emit('user.login', { id: 1 }, true)
641
+ *
642
+ * // 方式2: 对象形式
643
+ * emit({ type: 'user.login', payload: { id: 1 } ,meta:{...}}}, true)
644
+ */
645
+ /**
646
+ * 同步触发事件
647
+ * @param type - 事件类型,可以是字符串或预定义的事件类型
648
+ * @param payload - 事件数据负载
649
+ * @param retain - 是否保留该事件,用于后续新的订阅者
650
+ * @param meta - 事件元数据
651
+ * @returns 所有监听器的执行结果数组
652
+ *
653
+ * @description
654
+ * 同步触发指定类型的事件,支持两种调用方式:
655
+ * 1. 参数形式:emit(type, payload, retain, meta)
656
+ * 2. 对象形式:emit({ type, payload, meta }, retain)
657
+ *
658
+ * 特性:
659
+ * - 支持通配符匹配,一个事件可能触发多个监听器
660
+ * - 如果设置了retain为true,会保存最后一次的事件数据
661
+ * - 按照注册顺序同步调用所有匹配的监听器
662
+ * - 如果配置了ignoreErrors,监听器抛出的错误会被捕获并返回
663
+ *
664
+ * @example
665
+ * ```ts
666
+ * // 简单事件触发
667
+ * emitter.emit('user/login', { userId: 123 });
668
+ *
669
+ * // 带保留的事件触发
670
+ * emitter.emit('status/change', { online: true }, true);
671
+ *
672
+ * // 带元数据的事件触发
673
+ * emitter.emit('data/update', newData, false, { timestamp: Date.now() });
674
+ *
675
+ * // 使用对象形式触发
676
+ * emitter.emit({
677
+ * type: 'user/login',
678
+ * payload: { userId: 123 },
679
+ * meta: { time: Date.now() }
680
+ * }, true);
681
+ *
682
+ * // 清除保留事件
683
+ * emitter.emit("user/login")
684
+ *
685
+ * ```
686
+ */
687
+ emit<T extends Types = Types>(type: T, directive: symbol): any[];
688
+ emit(type: string, directive: symbol): any[];
689
+ emit<R = any, T extends Types = Types>(type: T, payload?: AllEvents[T], retain?: boolean): R[];
690
+ emit<R = any, T extends string = string>(type: T, payload?: T extends Types ? AllEvents[Types] : any, retain?: boolean): R[];
691
+ emit<R = any, T extends string = string>(message: FastEventEmitMessage<{
692
+ [K in T]: K extends Types ? AllEvents[K] : any;
693
+ }, Meta>, retain?: boolean): R[];
694
+ emit<R = any>(message: FastEventEmitMessage<AllEvents, Meta>, retain?: boolean): R[];
695
+ emit<R = any, T extends Types = Types>(type: T, payload?: AllEvents[T], options?: FastEventListenerArgs<Meta>): R[];
696
+ emit<R = any, T extends string = string>(type: T, payload?: T extends Types ? AllEvents[Types] : any, options?: FastEventListenerArgs<Meta>): R[];
697
+ emit<R = any, T extends string = string>(message: FastEventEmitMessage<{
698
+ [K in T]: K extends Types ? AllEvents[K] : any;
699
+ }, Meta>, options?: FastEventListenerArgs<Meta>): R[];
700
+ emit<R = any>(message: FastEventEmitMessage<AllEvents, Meta>, options?: FastEventListenerArgs<Meta>): R[];
701
+ /**
702
+ * 异步触发事件
703
+ * @param type - 事件类型,可以是字符串或预定义的事件类型
704
+ * @param payload - 事件数据负载
705
+ * @param retain - 是否保留该事件,用于后续新的订阅者
706
+ * @param meta - 事件元数据
707
+ * @returns Promise,解析为所有监听器的执行结果数组
708
+ *
709
+ * @description
710
+ * 异步触发指定类型的事件,与emit方法类似,但有以下区别:
711
+ * - 返回Promise,等待所有异步监听器执行完成
712
+ * - 使用Promise.allSettled处理监听器的执行结果
713
+ * - 即使某些监听器失败,也会等待所有监听器执行完成
714
+ * - 返回结果包含成功值或错误信息
715
+ *
716
+ * @example
717
+ * ```ts
718
+ * // 异步事件处理
719
+ * const results = await emitter.emitAsync('data/process', rawData);
720
+ *
721
+ * // 处理结果包含成功和失败的情况
722
+ * results.forEach(result => {
723
+ * if (result instanceof Error) {
724
+ * console.error('处理失败:', result);
725
+ * } else {
726
+ * console.log('处理成功:', result);
727
+ * }
728
+ * });
729
+ *
730
+ * // 带元数据的异步事件
731
+ * await emitter.emitAsync('batch/process', items, false, {
732
+ * batchId: 'batch-001',
733
+ * timestamp: Date.now()
734
+ * });
735
+ * ```
736
+ */
737
+ emitAsync<R = any>(type: string, payload?: any, retain?: boolean): Promise<[R | Error][]>;
738
+ emitAsync<R = any>(type: Types, payload?: AllEvents[Types], retain?: boolean): Promise<[R | Error][]>;
739
+ emitAsync<R = any, T extends string = string>(message: FastEventEmitMessage<{
740
+ [K in T]: K extends Types ? AllEvents[K] : any;
741
+ }, Meta>, retain?: boolean): Promise<[R | Error][]>;
742
+ emitAsync<R = any>(message: FastEventEmitMessage<AllEvents, Meta>, retain?: boolean): Promise<[R | Error][]>;
743
+ emitAsync<R = any>(type: string, payload?: any, options?: FastEventListenerArgs<Meta>): Promise<[R | Error][]>;
744
+ emitAsync<R = any>(type: Types, payload?: AllEvents[Types], options?: FastEventListenerArgs<Meta>): Promise<[R | Error][]>;
745
+ emitAsync<R = any, T extends string = string>(message: FastEventEmitMessage<{
746
+ [K in T]: K extends Types ? AllEvents[K] : any;
747
+ }, Meta>, options?: FastEventListenerArgs<Meta>): Promise<[R | Error][]>;
748
+ emitAsync<R = any>(message: FastEventEmitMessage<AllEvents, Meta>, options?: FastEventListenerArgs<Meta>): Promise<[R | Error][]>;
749
+ /**
750
+ * 等待指定事件发生,返回一个Promise
751
+ * @param type - 要等待的事件类型
752
+ * @param timeout - 超时时间(毫秒),默认为0表示永不超时
753
+ * @returns Promise,解析为事件消息对象,包含type、payload和meta
754
+ *
755
+ * @description
756
+ * 创建一个Promise,在指定事件发生时解析。
757
+ * - 当事件触发时,Promise会解析为事件消息对象
758
+ * - 如果设置了timeout且超时,Promise会被拒绝
759
+ * - 一旦事件发生或超时,会自动取消事件监听
760
+ *
761
+ * @example
762
+ * ```ts
763
+ * try {
764
+ * // 等待登录事件,最多等待5秒
765
+ * const event = await emitter.waitFor('user/login', 5000);
766
+ * console.log('用户登录成功:', event.payload);
767
+ * } catch (error) {
768
+ * console.error('等待登录超时');
769
+ * }
770
+ *
771
+ * // 无限等待事件
772
+ * const event = await emitter.waitFor('server/ready');
773
+ * console.log('服务器就绪');
774
+ * ```
775
+ */
776
+ waitFor<T extends Types>(type: T, timeout?: number): Promise<FastEventMessage<{
777
+ [key in T]: AllEvents[T];
778
+ }, Meta>>;
779
+ waitFor(type: string, timeout?: number): Promise<FastEventMessage<AllEvents, Meta>>;
780
+ waitFor<P = any>(type: string, timeout?: number): Promise<FastEventMessage<{
781
+ [key: string]: P;
782
+ }, Meta>>;
783
+ /**
784
+ * 创建一个新的事件作用域
785
+ * @param prefix - 作用域前缀,将自动添加到该作用域下所有事件名称前
786
+ * @returns 新的FastEventScope实例
787
+ *
788
+ * @description
789
+ * 创建一个新的事件作用域,用于在特定命名空间下管理事件。
790
+ *
791
+ * 重要特性:
792
+ * - 作用域与父事件发射器共享同一个监听器表
793
+ * - 作用域中的事件会自动添加前缀
794
+ * - 作用域的所有操作都会映射到父事件发射器上
795
+ * - 作用域不是完全隔离的,只是提供了事件名称的命名空间
796
+ *
797
+ * @example
798
+ * ```ts
799
+ * const emitter = new FastEvent();
800
+ *
801
+ * // 创建用户相关事件的作用域
802
+ * const userEvents = emitter.scope('user');
803
+ *
804
+ * // 在作用域中监听事件
805
+ * userEvents.on('login', (data) => {
806
+ * // 实际监听的是 'user/login'
807
+ * console.log('用户登录:', data);
808
+ * });
809
+ *
810
+ * // 在作用域中触发事件
811
+ * userEvents.emit('login', { userId: 123 });
812
+ * // 等同于 emitter.emit('user/login', { userId: 123 })
813
+ *
814
+ * // 创建嵌套作用域
815
+ * const profileEvents = userEvents.scope('profile');
816
+ * profileEvents.emit('update', { name: 'John' });
817
+ * // 等同于 emitter.emit('user/profile/update', { name: 'John' })
818
+ *
819
+ * // 清理作用域
820
+ * userEvents.offAll(); // 清理 'user' 前缀下的所有事件
821
+ * ```
822
+ */
823
+ scope<E extends Record<string, any> = Record<string, any>, P extends string = string, M extends Record<string, any> = Record<string, any>, C = Context>(prefix: P, options?: DeepPartial<FastEventScopeOptions<Meta & M, C>>): FastEventScope<ScopeEvents<AllEvents, P> & E, Meta & M, C>;
824
+ scope<E extends Record<string, any> = Record<string, any>, P extends string = string, M extends Record<string, any> = Record<string, any>, C = Context, ScopeObject extends FastEventScope<any, any, any> = FastEventScope<ScopeEvents<AllEvents, P> & E, Meta & M, C>>(prefix: P, scopeObj: ScopeObject, options?: DeepPartial<FastEventScopeOptions<Meta & M, C>>): ScopeObject;
825
+ }
826
+
827
+ type FastEventBusNodeOptions<Meta = Record<string, any>, Context = any> = FastEventOptions<Meta, Context> & {};
828
+ declare class FastEventBusNode<Events extends Record<string, any> = Record<string, any>, Meta extends Record<string, any> = Record<string, any>, Context = never> extends FastEvent<Events, Meta, Context> {
829
+ eventbus?: FastEventBus<any, any, any>;
830
+ private _subscribers;
831
+ constructor(options?: DeepPartial<FastEventBusNodeOptions<Meta, Context>>);
832
+ /**
833
+ *
834
+ * 如果事件名称带有名称空间分隔符::,则说明该触发的消息是要转发到其他节点,
835
+ * 由eventbus进行转发
836
+ *
837
+ * emit("otherNode@<事件名>") // 在otherNode节点上触发事件
838
+ * emit("@<事件名>") // 在当前总线队象上触发事件
839
+ *
840
+ * 事件执行前的监听器处理函数
841
+ * 处理带有命名空间前缀的事件,格式为 "<节点id>::<事件名>"
842
+ * 如果事件包含命名空间,则将事件转发到对应节点或全局事件总线
843
+ *
844
+ * @param message - 事件消息对象
845
+ * @param args - 事件监听器参数
846
+ * @returns {boolean} false 表示取消在内部触发事件,undefined 表示继续执行
847
+ * @private
848
+ */
849
+ private _onBeforeExecuteListener;
850
+ private _onAddListener;
851
+ /**
852
+ * 加入事件总线
853
+ * @param eventBus 要加入的事件总线
854
+ */
855
+ connect(eventbus: FastEventBus<any>): void;
856
+ disconnect(): void;
857
+ /**
858
+ *
859
+ * 订阅来自其他节点的消息
860
+ *
861
+ * 在Eventbus中的所有以`node::<节点id>/<事件>`的事件均会被转发到当前节点
862
+ *
863
+ * @private
864
+ */
865
+ private _onSubscribeForNode;
866
+ /**
867
+ * 发送点对点消息到指定节点
868
+ * @param toNodeId 目标节点ID
869
+ * @param message 要发送的消息
870
+ */
871
+ send<R = any, T extends FastEventBusNodeIds = FastEventBusNodeIds>(toNodeId: T, payload: any, args?: FastEventListenerArgs): R[];
872
+ send<R = any, T extends string = string>(toNodeId: T, payload: any, args?: FastEventListenerArgs): R[];
873
+ private _assertConnected;
874
+ /**
875
+ * 广播消息到所有其他节点
876
+ *
877
+ *
878
+ * @param message 要广播的消息
879
+ */
880
+ broadcast<R = any, T extends FastEventBusEventTypes = FastEventBusEventTypes>(message: FastEventBusMessage<{
881
+ [K in T]: K extends FastEventBusEventTypes ? FastEventBusEvents[K] : any;
882
+ }>, args?: FastEventListenerArgs): R[];
883
+ broadcast<R = any, T extends string = string>(message: FastEventBusMessage<{
884
+ [K in T]: K extends FastEventBusEventTypes ? FastEventBusEvents[K] : any;
885
+ }, Meta>, args?: FastEventListenerArgs): R[];
886
+ broadcast<R = any, T extends FastEventBusEventTypes = FastEventBusEventTypes>(type: T, payload: FastEventBusEvents[T], options?: FastEventListenerArgs<Meta>): R[];
887
+ broadcast<R = any, T extends string = string>(type: T, payload: T extends FastEventBusEventTypes ? FastEventBusEvents[T] : any, options?: FastEventListenerArgs<Meta>): R[];
888
+ /**
889
+ * 处理接收到的消息
890
+ * 供子类继承重载
891
+ */
892
+ onMessage(message: FastEventBusMessage, args: FastEventListenerArgs): void;
893
+ }
894
+
895
+ /**
896
+ *
897
+ * const eventbus = new FastEventBus()
898
+ *
899
+ * const node1 = new FastEventBusNode({id:"node1",...})
900
+ * const node2 = new FastEventBusNode({id:"node2",...})
901
+ * const node3 = new FastEventBusNode({id:"node3",...})
902
+ *
903
+ * - 将节点添加到FastEventBus中
904
+ * eventbus.add(node1)
905
+ * eventbus.add(node2)
906
+ * eventbus.add(node3)
907
+ * 或者
908
+ * node1.join(eventbus)
909
+ * node2.join(eventbus)
910
+ * node3.join(eventbus)
911
+ *
912
+ * - 每个节点均有唯一的节点名称,用于区分不同的节点
913
+ * - 每个节点是一个独立的FastEvent实例
914
+ * - 每个节点的配置选项与FastEvent相同
915
+ * - 节点之间相互独立,互不影响
916
+ * - FastEventBus负载节点之间的通讯
917
+ * - 所有节点均可以在onMessage中接收到其订阅或点对点或广播的消息
918
+ *
919
+ * - 可以广播消息给所有节点
920
+ *
921
+ * eventbus.broadcast(message)
922
+ * node1.broadcast(message)
923
+ *
924
+ * - 可以在节点之间进行点对点发送消息
925
+ *
926
+ * eventbus.send(node1.id, message)
927
+ * node1.send(node2.id, message)
928
+ *
929
+ *
930
+ * - 节点消息接收到的消息是FastEventBusNodeMessage,增加了from代表是消息来自哪里
931
+ * - 节点如果要订阅其他节点的消息,需要在事件类型名称加::
932
+ * node1.on("node2::test",listener,{...})代表订阅node2的test事件
933
+ * - 节点可以在全局触发事件
934
+ * node1.emit("::test") 在全局(即Eventbus)触发事件
935
+ * node2.on("::test",listener,{...})
936
+ *
937
+ */
938
+
939
+ declare class FastEventBus<Events extends Record<string, any> = FastEventBusEvents, Meta extends Record<string, any> = Record<string, any>, Context = never, AllEvents extends Record<string, any> = Exclude<Expand<Events & FastEventBusEvents>, number | symbol>, Types extends keyof AllEvents = Expand<Exclude<keyof (AllEvents), number | symbol>>> extends FastEvent<Events & FastEventBusEvents, Meta, Context> {
940
+ nodes: Map<string, FastEventBusNode<any, any, any>>;
941
+ constructor(options?: FastEventBusOptions<Meta, Context>);
942
+ /**
943
+ * 添加节点到事件总线
944
+ * @param node 要添加的节点
945
+ */
946
+ add(...nodes: FastEventBusNode<any, any, any>[]): void;
947
+ /**
948
+ * 从事件总线移除节点
949
+ * @param nodeId 要移除的节点ID
950
+ */
951
+ remove(nodeId: string): void;
952
+ /**
953
+ * 广播消息到所有节点
954
+ *
955
+ * broadcast(1) // 默认广播主题data
956
+ * broadcast({
957
+ * type: 'connect',
958
+ * payload: 100
959
+ * }) *
960
+ *
961
+ * @param message 要广播的消息
962
+ */
963
+ broadcast<R = any, T extends Types = Types>(message: FastEventBusMessage<{
964
+ [K in T]: AllEvents[T];
965
+ }, Meta>, args?: FastEventListenerArgs): R[];
966
+ broadcast<R = any, T extends string = string>(message: FastEventBusMessage<{
967
+ [K in T]: K extends Types ? AllEvents[K] : any;
968
+ }, Meta>, args?: FastEventListenerArgs): R[];
969
+ broadcast<R = any, T extends string = string>(type: T, payload?: T extends Types ? AllEvents[Types] : any, options?: FastEventListenerArgs<Meta>): R[];
970
+ broadcast<R = any, T extends Types = Types>(type: T, payload: AllEvents[T], args?: FastEventListenerArgs): R[];
971
+ /**
972
+ * 发送消息到指定节点
973
+ *
974
+ * 点对点消息的事件名称为data
975
+ *
976
+ * @param toNodeId 目标节点ID
977
+ * @param message 要发送的消息
978
+ */
979
+ send<R = any>(message: FastEventBusMessage, args?: FastEventListenerArgs): R[];
980
+ }
981
+
982
+ declare const NamespaceDelimiter = "::";
983
+ declare const BroadcastEvent = "@";
984
+ declare const NodeDataEvent = "data";
985
+
986
+ declare const __FastEvent__: unique symbol;
987
+ declare const __FastEventScope__: unique symbol;
988
+ declare class FastEventError extends Error {
989
+ constructor(message?: string);
990
+ }
991
+ declare class TimeoutError extends FastEventError {
992
+ }
993
+ declare class UnboundError extends FastEventError {
994
+ }
995
+ declare class AbortError extends FastEventError {
996
+ }
997
+ declare class CancelError extends FastEventError {
998
+ }
999
+ declare class QueueOverflowError extends FastEventError {
1000
+ }
1001
+ declare const FastEventDirectives: {
1002
+ clearRetain: symbol;
1003
+ };
1004
+
1005
+ export { AbortError, BroadcastEvent, CancelError, type ChangeFieldType, type DeepPartial, type Dict, type Expand, type Fallback, type FaseEventMessageExtends, FastEvent, type FastEventAnyListener, FastEventBus, type FastEventBusEventTypes, type FastEventBusEvents, type FastEventBusMessage, FastEventBusNode, type FastEventBusNodeIds, type FastEventBusNodeOptions, type FastEventBusNodes, type FastEventBusOptions, FastEventDirectives, type FastEventEmitMessage, FastEventError, type FastEventListenOptions, type FastEventListener, type FastEventListenerArgs, type FastEventMessage, type FastEventMeta, type FastEventOptions, FastEventScope, type FastEventScopeMeta, type FastEventScopeOptions, type FastEventSubscriber, type FastEvents, type FastListenerMeta, type FastListenerNode, type FastListeners, type Merge, NamespaceDelimiter, NodeDataEvent, type ObjectKeys, type OptionalItems, type Overloads, type OverrideOptions, type PickScopeEvents, QueueOverflowError, type RequiredItems, type ScopeEvents, TimeoutError, UnboundError, type Unique, __FastEventScope__, __FastEvent__ };