fastevent 2.1.4 → 2.2.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.
@@ -1,45 +1,54 @@
1
- type FastListenerExecutor = (listeners: FastListenerMeta[], message: TypedFastEventMessage, args: FastEventListenerArgs, execute: (listener: FastEventListener, message: TypedFastEventMessage, args: FastEventListenerArgs, catchErrors?: boolean) => Promise<any> | any) => Promise<any[]> | any[];
1
+ type FastListenerExecutor = (listeners: FastListenerMeta[], message: TypedFastEventMessage, args: FastEventListenerArgs, execute: (listener: TypedFastEventListener, message: TypedFastEventMessage, args: FastEventListenerArgs, catchErrors?: boolean) => Promise<any> | any) => Promise<any[]> | any[];
2
2
 
3
- type FastListenerPipe = (listener: FastEventListener) => FastEventListener;
3
+ type FastListenerPipe = (listener: TypedFastEventListener) => TypedFastEventListener;
4
4
 
5
5
  interface FastEventMeta {
6
6
  }
7
7
  interface FastEventMessageExtends {
8
8
  }
9
- type FastEventMessage = {
10
- type: string;
11
- payload: any;
12
- meta?: Record<string, any>;
13
- };
9
+ type FastEventMessage<P = any, M extends Record<string, any> = Record<string, any>, T extends string = string> = {
10
+ type: T;
11
+ payload: P;
12
+ meta?: M & Partial<FastEventMeta>;
13
+ } & FastEventMessageExtends;
14
14
  type TypedFastEventMessage<Events extends Record<string, any> = Record<string, any>, M = any> = ({
15
15
  [K in keyof Events]: {
16
16
  type: Exclude<K, number | symbol>;
17
17
  payload: Events[K];
18
18
  meta: FastEventMeta & M & Record<string, any>;
19
19
  };
20
- }[Exclude<keyof Events, number | symbol>]) & DeepPartial<FastEventMessageExtends>;
20
+ }[Exclude<keyof Events, number | symbol>]) & FastEventMessageExtends;
21
21
  type TypedFastEventMessageOptional<Events extends Record<string, any> = Record<string, any>, M = any> = ({
22
22
  [K in keyof Events]: {
23
23
  type: Exclude<K, number | symbol>;
24
24
  payload: Events[K];
25
25
  meta?: DeepPartial<FastEventMeta & M & Record<string, any>>;
26
26
  };
27
- }[Exclude<keyof Events, number | symbol>]) & DeepPartial<FastEventMessageExtends>;
27
+ }[Exclude<keyof Events, number | symbol>]) & FastEventMessageExtends;
28
28
  type FastEventEmitMessage<Events extends Record<string, any> = Record<string, any>, M = any> = ({
29
29
  [K in keyof Events]: {
30
30
  type: Exclude<K, number | symbol>;
31
31
  payload?: Events[K];
32
32
  meta?: DeepPartial<FastEventMeta & M & Record<string, any>>;
33
33
  };
34
- }[Exclude<keyof Events, number | symbol>]) & DeepPartial<FastEventMessageExtends>;
35
- type FastEventListener<T extends string = string, P = any, M = any, C = any> = (this: C, message: TypedFastEventMessage<{
34
+ }[Exclude<keyof Events, number | symbol>]) & FastEventMessageExtends;
35
+ type TypedFastEventListener<T extends string = string, P = any, M = any, C = any> = (this: C, message: TypedFastEventMessage<{
36
36
  [K in T]: P;
37
37
  }, M>, args: FastEventListenerArgs<M>) => any | Promise<any>;
38
- type FastEventAnyListener<Events extends Record<string, any> = Record<string, any>, Meta = never, Context = any> = (this: Context, message: TypedFastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => any | Promise<any>;
38
+ type TypedFastEventAnyListener<Events extends Record<string, any> = Record<string, any>, Meta = never, Context = any> = (this: Context, message: TypedFastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => any | Promise<any>;
39
+ type FastEventListeners<Events extends Record<string, any> = Record<string, any>, M = any, C = any> = ({
40
+ [K in keyof Events]: TypedFastEventListener<Exclude<K, number | symbol>, Events[K], M, C>;
41
+ });
42
+ type FastEventListener<P = any, M extends Record<string, any> = Record<string, any>, T extends string = string> = (message: FastEventMessage<P, M, T>, args: FastEventListenerArgs<M>) => any | Promise<any>;
39
43
  /**
40
- * [监听器函数引用,需要执行多少次,实际执行的次数(用于负载均衡时记录)]
44
+ * [
45
+ * 监听器函数引用,
46
+ * 需要执行多少次, =0代表不限
47
+ * 实际执行的次数(用于负载均衡时记录)
48
+ * 标签 用于调试一般可以标识监听器类型或任意信息
49
+ * ]
41
50
  */
42
- type FastListenerMeta = [FastEventListener<any, any>, number, number];
51
+ type FastListenerMeta = [TypedFastEventListener<any, any>, number, number, string?];
43
52
  type FastListenerNode = {
44
53
  __listeners: FastListenerMeta[];
45
54
  } & {
@@ -75,7 +84,7 @@ type FastEventSubscriber = {
75
84
  * scope.off(subscriber.listener) // 生效
76
85
  *
77
86
  */
78
- listener: FastEventListener<any, any, any>;
87
+ listener: TypedFastEventListener<any, any, any>;
79
88
  };
80
89
  type FastListeners = FastListenerNode;
81
90
  type FastEventOptions<Meta = Record<string, any>, Context = never> = {
@@ -85,17 +94,17 @@ type FastEventOptions<Meta = Record<string, any>, Context = never> = {
85
94
  context: Context;
86
95
  ignoreErrors: boolean;
87
96
  meta: Meta;
88
- onAddListener?: (type: string, listener: FastEventListener, options: FastEventListenOptions<Record<string, any>, Meta>) => boolean | FastEventSubscriber | void;
89
- onRemoveListener?: (type: string, listener: FastEventListener) => void;
97
+ onAddListener?: (type: string, listener: TypedFastEventListener, options: FastEventListenOptions<Record<string, any>, Meta>) => boolean | FastEventSubscriber | void;
98
+ onRemoveListener?: (type: string, listener: TypedFastEventListener) => void;
90
99
  onClearListeners?: () => void;
91
- onListenerError?: ((error: Error, listener: FastEventListener, message: TypedFastEventMessage<any, Meta>, args: FastEventListenerArgs<Meta> | undefined) => void);
100
+ onListenerError?: ((error: Error, listener: TypedFastEventListener, message: TypedFastEventMessage<any, Meta>, args: FastEventListenerArgs<Meta> | undefined) => void);
92
101
  onBeforeExecuteListener?: (message: TypedFastEventMessage<any, Meta>, args: FastEventListenerArgs<Meta>) => boolean | void | any[];
93
102
  onAfterExecuteListener?: (message: TypedFastEventMessage<any, Meta>, returns: any[], listeners: FastListenerNode[]) => void;
94
103
  /**
95
104
  * 全局执行器
96
105
  */
97
106
  executor?: FastListenerExecutor;
98
- onMessage?: FastEventListener;
107
+ onMessage?: TypedFastEventListener;
99
108
  expandEmitResults?: boolean;
100
109
  };
101
110
  interface FastEvents {
@@ -110,6 +119,15 @@ type FastEventListenOptions<Events extends Record<string, any> = Record<string,
110
119
  filter?: (message: TypedFastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => boolean;
111
120
  off?: (message: TypedFastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => boolean;
112
121
  pipes?: FastListenerPipe[];
122
+ /**
123
+ * 为监听器添加一个tag,在监听器注册表中记录,用于调试使用
124
+ *
125
+ * emitter.on(type,listener,{tag:"x"})
126
+ *
127
+ * emitter.getListeners(tag)
128
+ *
129
+ */
130
+ tag?: string;
113
131
  };
114
132
  type FastEventListenerArgs<M = Record<string, any>> = {
115
133
  retain?: boolean;
@@ -277,11 +295,11 @@ type FastEventScopeOptions<Meta = Record<string, any>, Context = never> = {
277
295
  meta: FastEventScopeMeta & FastEventMeta & Meta;
278
296
  context: Context;
279
297
  executor?: FastListenerExecutor;
280
- onMessage?: FastEventListener;
298
+ onMessage?: TypedFastEventListener;
281
299
  };
282
- type FastEventScopeMeta = {
300
+ interface FastEventScopeMeta {
283
301
  scope: string;
284
- };
302
+ }
285
303
  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> {
286
304
  __FastEventScope__: boolean;
287
305
  private _options;
@@ -290,6 +308,8 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
290
308
  meta: FinalMeta;
291
309
  context: Fallback<Context, typeof this>;
292
310
  message: TypedFastEventMessageOptional<Events, FinalMeta>;
311
+ listeners: FastEventListeners<Events, FinalMeta>;
312
+ anyListener: TypedFastEventAnyListener<Events, FinalMeta, Fallback<Context, typeof this>>;
293
313
  };
294
314
  prefix: string;
295
315
  emitter: FastEvent<Events>;
@@ -319,20 +339,20 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
319
339
  on<T extends Types = Types>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
320
340
  on<T extends string>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
321
341
  on(type: '**', options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
322
- on<T extends Types = Types>(type: T, listener: FastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
323
- on<T extends string>(type: T, listener: FastEventListener<string, any, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
324
- on(type: '**', listener: FastEventAnyListener<Events, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
342
+ on<T extends Types = Types>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
343
+ on<T extends string>(type: T, listener: TypedFastEventListener<string, any, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
344
+ on(type: '**', listener: TypedFastEventAnyListener<Events, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
325
345
  once<T extends Types = Types>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
326
346
  once<T extends string>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
327
- once<T extends Types = Types>(type: T, listener: FastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
328
- once<T extends string>(type: T, listener: FastEventListener<string, any, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
347
+ once<T extends Types = Types>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
348
+ once<T extends string>(type: T, listener: TypedFastEventListener<string, any, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
329
349
  onAny(options?: Pick<FastEventListenOptions, 'prepend'>): FastEventSubscriber;
330
- onAny<P = any>(listener: FastEventAnyListener<{
350
+ onAny<P = any>(listener: TypedFastEventAnyListener<{
331
351
  [K: string]: P;
332
352
  }, FinalMeta, Fallback<Context, typeof this>>, options?: Pick<FastEventListenOptions, 'prepend'>): FastEventSubscriber;
333
- off(listener: FastEventListener<any, any, any>): void;
334
- off(type: string, listener: FastEventListener<any, any, any>): void;
335
- off(type: Types, listener: FastEventListener<any, any, any>): void;
353
+ off(listener: TypedFastEventListener<any, any, any>): void;
354
+ off(type: string, listener: TypedFastEventListener<any, any, any>): void;
355
+ off(type: Types, listener: TypedFastEventListener<any, any, any>): void;
336
356
  off(type: string): void;
337
357
  off(type: Types): void;
338
358
  offAll(): void;
@@ -424,6 +444,8 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
424
444
  meta: Expand<FastEventMeta & Meta & Record<string, any>>;
425
445
  context: Expand<Fallback<Context, typeof this>>;
426
446
  message: TypedFastEventMessageOptional<AllEvents, Expand<FastEventMeta & Meta & Record<string, any>>>;
447
+ listeners: FastEventListeners<AllEvents, Expand<FastEventMeta & Meta & Record<string, any>>>;
448
+ anyListener: TypedFastEventAnyListener<AllEvents, Expand<FastEventMeta & Meta & Record<string, any>>, Expand<Fallback<Context, typeof this>>>;
427
449
  };
428
450
  /**
429
451
  * 创建FastEvent实例
@@ -509,9 +531,9 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
509
531
  on<T extends Types = Types>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
510
532
  on<T extends string>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
511
533
  on(type: '**', options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
512
- 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;
513
- on<T extends string>(type: T, listener: FastEventAnyListener<AllEvents, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
514
- on(type: '**', listener: FastEventAnyListener<Record<string, any>, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
534
+ on<T extends Types = Types>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, AllEvents[T], Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
535
+ on<T extends string>(type: T, listener: TypedFastEventAnyListener<AllEvents, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
536
+ on(type: '**', listener: TypedFastEventAnyListener<Record<string, any>, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
515
537
  /**
516
538
  * 注册一次性事件监听器
517
539
  * @param type - 事件类型,支持与on方法相同的格式:
@@ -535,8 +557,8 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
535
557
  */
536
558
  once<T extends Types = Types>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
537
559
  once<T extends string>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
538
- 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;
539
- once<T extends string>(type: T, listener: FastEventAnyListener<AllEvents, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
560
+ once<T extends Types = Types>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, AllEvents[T], Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
561
+ once<T extends string>(type: T, listener: TypedFastEventAnyListener<AllEvents, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
540
562
  /**
541
563
  * 注册一个监听器,用于监听所有事件
542
564
  * @param listener 事件监听器函数,可以接收任意类型的事件数据
@@ -552,7 +574,7 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
552
574
  * ```listener: FastEventAnyListener<AllEvents, Meta, Fallback<Context, typeof this>>): FastEventSubscriber
553
575
  */
554
576
  onAny(options?: Omit<FastEventListenOptions<AllEvents, Meta>, 'count'>): FastEventSubscriber;
555
- onAny<P = any>(listener: FastEventAnyListener<Record<string, P>, Meta, Fallback<Context, typeof this>>, options?: Omit<FastEventListenOptions<AllEvents, Meta>, 'count'>): FastEventSubscriber;
577
+ onAny<P = any>(listener: TypedFastEventAnyListener<Record<string, P>, Meta, Fallback<Context, typeof this>>, options?: Omit<FastEventListenOptions<AllEvents, Meta>, 'count'>): FastEventSubscriber;
556
578
  /**
557
579
  *
558
580
  * 当调用on/once/onAny时如果没有指定监听器,则调用此方法
@@ -561,9 +583,9 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
561
583
  *
562
584
  */
563
585
  onMessage(message: TypedFastEventMessage<AllEvents, Meta>, args: FastEventListenerArgs<Meta>): void;
564
- off(listener: FastEventListener<any, any, any>): void;
565
- off(type: string, listener: FastEventListener<any, any, any>): void;
566
- off(type: Types, listener: FastEventListener<any, any, any>): void;
586
+ off(listener: TypedFastEventListener<any, any, any>): void;
587
+ off(type: string, listener: TypedFastEventListener<any, any, any>): void;
588
+ off(type: Types, listener: TypedFastEventListener<any, any, any>): void;
567
589
  off(type: string): void;
568
590
  off(type: Types): void;
569
591
  /**
@@ -730,6 +752,7 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
730
752
  [K in T]: K extends Types ? AllEvents[K] : any;
731
753
  }, Meta>, options?: FastEventListenerArgs<Meta>): R[];
732
754
  emit<R = any>(message: FastEventEmitMessage<AllEvents, Meta>, options?: FastEventListenerArgs<Meta>): R[];
755
+ getListeners(type: keyof AllEvents): FastListenerMeta[];
733
756
  /**
734
757
  * 异步触发事件
735
758
  * @param type - 事件类型,可以是字符串或预定义的事件类型
@@ -1034,4 +1057,4 @@ declare const FastEventDirectives: {
1034
1057
  clearRetain: symbol;
1035
1058
  };
1036
1059
 
1037
- export { AbortError, BroadcastEvent, CancelError, type ChangeFieldType, type DeepPartial, type Dict, type Expand, type Fallback, 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 FastEventMessageExtends, 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, type TypedFastEventMessage, type TypedFastEventMessageOptional, UnboundError, type Unique, __FastEventScope__, __FastEvent__ };
1060
+ export { AbortError, BroadcastEvent, CancelError, type ChangeFieldType, type DeepPartial, type Dict, type Expand, type Fallback, FastEvent, 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 FastEventListeners, type FastEventMessage, type FastEventMessageExtends, 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, type TypedFastEventAnyListener, type TypedFastEventListener, type TypedFastEventMessage, type TypedFastEventMessageOptional, UnboundError, type Unique, __FastEventScope__, __FastEvent__ };
@@ -1,45 +1,54 @@
1
- type FastListenerExecutor = (listeners: FastListenerMeta[], message: TypedFastEventMessage, args: FastEventListenerArgs, execute: (listener: FastEventListener, message: TypedFastEventMessage, args: FastEventListenerArgs, catchErrors?: boolean) => Promise<any> | any) => Promise<any[]> | any[];
1
+ type FastListenerExecutor = (listeners: FastListenerMeta[], message: TypedFastEventMessage, args: FastEventListenerArgs, execute: (listener: TypedFastEventListener, message: TypedFastEventMessage, args: FastEventListenerArgs, catchErrors?: boolean) => Promise<any> | any) => Promise<any[]> | any[];
2
2
 
3
- type FastListenerPipe = (listener: FastEventListener) => FastEventListener;
3
+ type FastListenerPipe = (listener: TypedFastEventListener) => TypedFastEventListener;
4
4
 
5
5
  interface FastEventMeta {
6
6
  }
7
7
  interface FastEventMessageExtends {
8
8
  }
9
- type FastEventMessage = {
10
- type: string;
11
- payload: any;
12
- meta?: Record<string, any>;
13
- };
9
+ type FastEventMessage<P = any, M extends Record<string, any> = Record<string, any>, T extends string = string> = {
10
+ type: T;
11
+ payload: P;
12
+ meta?: M & Partial<FastEventMeta>;
13
+ } & FastEventMessageExtends;
14
14
  type TypedFastEventMessage<Events extends Record<string, any> = Record<string, any>, M = any> = ({
15
15
  [K in keyof Events]: {
16
16
  type: Exclude<K, number | symbol>;
17
17
  payload: Events[K];
18
18
  meta: FastEventMeta & M & Record<string, any>;
19
19
  };
20
- }[Exclude<keyof Events, number | symbol>]) & DeepPartial<FastEventMessageExtends>;
20
+ }[Exclude<keyof Events, number | symbol>]) & FastEventMessageExtends;
21
21
  type TypedFastEventMessageOptional<Events extends Record<string, any> = Record<string, any>, M = any> = ({
22
22
  [K in keyof Events]: {
23
23
  type: Exclude<K, number | symbol>;
24
24
  payload: Events[K];
25
25
  meta?: DeepPartial<FastEventMeta & M & Record<string, any>>;
26
26
  };
27
- }[Exclude<keyof Events, number | symbol>]) & DeepPartial<FastEventMessageExtends>;
27
+ }[Exclude<keyof Events, number | symbol>]) & FastEventMessageExtends;
28
28
  type FastEventEmitMessage<Events extends Record<string, any> = Record<string, any>, M = any> = ({
29
29
  [K in keyof Events]: {
30
30
  type: Exclude<K, number | symbol>;
31
31
  payload?: Events[K];
32
32
  meta?: DeepPartial<FastEventMeta & M & Record<string, any>>;
33
33
  };
34
- }[Exclude<keyof Events, number | symbol>]) & DeepPartial<FastEventMessageExtends>;
35
- type FastEventListener<T extends string = string, P = any, M = any, C = any> = (this: C, message: TypedFastEventMessage<{
34
+ }[Exclude<keyof Events, number | symbol>]) & FastEventMessageExtends;
35
+ type TypedFastEventListener<T extends string = string, P = any, M = any, C = any> = (this: C, message: TypedFastEventMessage<{
36
36
  [K in T]: P;
37
37
  }, M>, args: FastEventListenerArgs<M>) => any | Promise<any>;
38
- type FastEventAnyListener<Events extends Record<string, any> = Record<string, any>, Meta = never, Context = any> = (this: Context, message: TypedFastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => any | Promise<any>;
38
+ type TypedFastEventAnyListener<Events extends Record<string, any> = Record<string, any>, Meta = never, Context = any> = (this: Context, message: TypedFastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => any | Promise<any>;
39
+ type FastEventListeners<Events extends Record<string, any> = Record<string, any>, M = any, C = any> = ({
40
+ [K in keyof Events]: TypedFastEventListener<Exclude<K, number | symbol>, Events[K], M, C>;
41
+ });
42
+ type FastEventListener<P = any, M extends Record<string, any> = Record<string, any>, T extends string = string> = (message: FastEventMessage<P, M, T>, args: FastEventListenerArgs<M>) => any | Promise<any>;
39
43
  /**
40
- * [监听器函数引用,需要执行多少次,实际执行的次数(用于负载均衡时记录)]
44
+ * [
45
+ * 监听器函数引用,
46
+ * 需要执行多少次, =0代表不限
47
+ * 实际执行的次数(用于负载均衡时记录)
48
+ * 标签 用于调试一般可以标识监听器类型或任意信息
49
+ * ]
41
50
  */
42
- type FastListenerMeta = [FastEventListener<any, any>, number, number];
51
+ type FastListenerMeta = [TypedFastEventListener<any, any>, number, number, string?];
43
52
  type FastListenerNode = {
44
53
  __listeners: FastListenerMeta[];
45
54
  } & {
@@ -75,7 +84,7 @@ type FastEventSubscriber = {
75
84
  * scope.off(subscriber.listener) // 生效
76
85
  *
77
86
  */
78
- listener: FastEventListener<any, any, any>;
87
+ listener: TypedFastEventListener<any, any, any>;
79
88
  };
80
89
  type FastListeners = FastListenerNode;
81
90
  type FastEventOptions<Meta = Record<string, any>, Context = never> = {
@@ -85,17 +94,17 @@ type FastEventOptions<Meta = Record<string, any>, Context = never> = {
85
94
  context: Context;
86
95
  ignoreErrors: boolean;
87
96
  meta: Meta;
88
- onAddListener?: (type: string, listener: FastEventListener, options: FastEventListenOptions<Record<string, any>, Meta>) => boolean | FastEventSubscriber | void;
89
- onRemoveListener?: (type: string, listener: FastEventListener) => void;
97
+ onAddListener?: (type: string, listener: TypedFastEventListener, options: FastEventListenOptions<Record<string, any>, Meta>) => boolean | FastEventSubscriber | void;
98
+ onRemoveListener?: (type: string, listener: TypedFastEventListener) => void;
90
99
  onClearListeners?: () => void;
91
- onListenerError?: ((error: Error, listener: FastEventListener, message: TypedFastEventMessage<any, Meta>, args: FastEventListenerArgs<Meta> | undefined) => void);
100
+ onListenerError?: ((error: Error, listener: TypedFastEventListener, message: TypedFastEventMessage<any, Meta>, args: FastEventListenerArgs<Meta> | undefined) => void);
92
101
  onBeforeExecuteListener?: (message: TypedFastEventMessage<any, Meta>, args: FastEventListenerArgs<Meta>) => boolean | void | any[];
93
102
  onAfterExecuteListener?: (message: TypedFastEventMessage<any, Meta>, returns: any[], listeners: FastListenerNode[]) => void;
94
103
  /**
95
104
  * 全局执行器
96
105
  */
97
106
  executor?: FastListenerExecutor;
98
- onMessage?: FastEventListener;
107
+ onMessage?: TypedFastEventListener;
99
108
  expandEmitResults?: boolean;
100
109
  };
101
110
  interface FastEvents {
@@ -110,6 +119,15 @@ type FastEventListenOptions<Events extends Record<string, any> = Record<string,
110
119
  filter?: (message: TypedFastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => boolean;
111
120
  off?: (message: TypedFastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => boolean;
112
121
  pipes?: FastListenerPipe[];
122
+ /**
123
+ * 为监听器添加一个tag,在监听器注册表中记录,用于调试使用
124
+ *
125
+ * emitter.on(type,listener,{tag:"x"})
126
+ *
127
+ * emitter.getListeners(tag)
128
+ *
129
+ */
130
+ tag?: string;
113
131
  };
114
132
  type FastEventListenerArgs<M = Record<string, any>> = {
115
133
  retain?: boolean;
@@ -277,11 +295,11 @@ type FastEventScopeOptions<Meta = Record<string, any>, Context = never> = {
277
295
  meta: FastEventScopeMeta & FastEventMeta & Meta;
278
296
  context: Context;
279
297
  executor?: FastListenerExecutor;
280
- onMessage?: FastEventListener;
298
+ onMessage?: TypedFastEventListener;
281
299
  };
282
- type FastEventScopeMeta = {
300
+ interface FastEventScopeMeta {
283
301
  scope: string;
284
- };
302
+ }
285
303
  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> {
286
304
  __FastEventScope__: boolean;
287
305
  private _options;
@@ -290,6 +308,8 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
290
308
  meta: FinalMeta;
291
309
  context: Fallback<Context, typeof this>;
292
310
  message: TypedFastEventMessageOptional<Events, FinalMeta>;
311
+ listeners: FastEventListeners<Events, FinalMeta>;
312
+ anyListener: TypedFastEventAnyListener<Events, FinalMeta, Fallback<Context, typeof this>>;
293
313
  };
294
314
  prefix: string;
295
315
  emitter: FastEvent<Events>;
@@ -319,20 +339,20 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
319
339
  on<T extends Types = Types>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
320
340
  on<T extends string>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
321
341
  on(type: '**', options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
322
- on<T extends Types = Types>(type: T, listener: FastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
323
- on<T extends string>(type: T, listener: FastEventListener<string, any, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
324
- on(type: '**', listener: FastEventAnyListener<Events, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
342
+ on<T extends Types = Types>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
343
+ on<T extends string>(type: T, listener: TypedFastEventListener<string, any, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
344
+ on(type: '**', listener: TypedFastEventAnyListener<Events, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
325
345
  once<T extends Types = Types>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
326
346
  once<T extends string>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
327
- once<T extends Types = Types>(type: T, listener: FastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
328
- once<T extends string>(type: T, listener: FastEventListener<string, any, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
347
+ once<T extends Types = Types>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
348
+ once<T extends string>(type: T, listener: TypedFastEventListener<string, any, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
329
349
  onAny(options?: Pick<FastEventListenOptions, 'prepend'>): FastEventSubscriber;
330
- onAny<P = any>(listener: FastEventAnyListener<{
350
+ onAny<P = any>(listener: TypedFastEventAnyListener<{
331
351
  [K: string]: P;
332
352
  }, FinalMeta, Fallback<Context, typeof this>>, options?: Pick<FastEventListenOptions, 'prepend'>): FastEventSubscriber;
333
- off(listener: FastEventListener<any, any, any>): void;
334
- off(type: string, listener: FastEventListener<any, any, any>): void;
335
- off(type: Types, listener: FastEventListener<any, any, any>): void;
353
+ off(listener: TypedFastEventListener<any, any, any>): void;
354
+ off(type: string, listener: TypedFastEventListener<any, any, any>): void;
355
+ off(type: Types, listener: TypedFastEventListener<any, any, any>): void;
336
356
  off(type: string): void;
337
357
  off(type: Types): void;
338
358
  offAll(): void;
@@ -424,6 +444,8 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
424
444
  meta: Expand<FastEventMeta & Meta & Record<string, any>>;
425
445
  context: Expand<Fallback<Context, typeof this>>;
426
446
  message: TypedFastEventMessageOptional<AllEvents, Expand<FastEventMeta & Meta & Record<string, any>>>;
447
+ listeners: FastEventListeners<AllEvents, Expand<FastEventMeta & Meta & Record<string, any>>>;
448
+ anyListener: TypedFastEventAnyListener<AllEvents, Expand<FastEventMeta & Meta & Record<string, any>>, Expand<Fallback<Context, typeof this>>>;
427
449
  };
428
450
  /**
429
451
  * 创建FastEvent实例
@@ -509,9 +531,9 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
509
531
  on<T extends Types = Types>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
510
532
  on<T extends string>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
511
533
  on(type: '**', options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
512
- 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;
513
- on<T extends string>(type: T, listener: FastEventAnyListener<AllEvents, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
514
- on(type: '**', listener: FastEventAnyListener<Record<string, any>, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
534
+ on<T extends Types = Types>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, AllEvents[T], Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
535
+ on<T extends string>(type: T, listener: TypedFastEventAnyListener<AllEvents, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
536
+ on(type: '**', listener: TypedFastEventAnyListener<Record<string, any>, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
515
537
  /**
516
538
  * 注册一次性事件监听器
517
539
  * @param type - 事件类型,支持与on方法相同的格式:
@@ -535,8 +557,8 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
535
557
  */
536
558
  once<T extends Types = Types>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
537
559
  once<T extends string>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
538
- 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;
539
- once<T extends string>(type: T, listener: FastEventAnyListener<AllEvents, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
560
+ once<T extends Types = Types>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, AllEvents[T], Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
561
+ once<T extends string>(type: T, listener: TypedFastEventAnyListener<AllEvents, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
540
562
  /**
541
563
  * 注册一个监听器,用于监听所有事件
542
564
  * @param listener 事件监听器函数,可以接收任意类型的事件数据
@@ -552,7 +574,7 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
552
574
  * ```listener: FastEventAnyListener<AllEvents, Meta, Fallback<Context, typeof this>>): FastEventSubscriber
553
575
  */
554
576
  onAny(options?: Omit<FastEventListenOptions<AllEvents, Meta>, 'count'>): FastEventSubscriber;
555
- onAny<P = any>(listener: FastEventAnyListener<Record<string, P>, Meta, Fallback<Context, typeof this>>, options?: Omit<FastEventListenOptions<AllEvents, Meta>, 'count'>): FastEventSubscriber;
577
+ onAny<P = any>(listener: TypedFastEventAnyListener<Record<string, P>, Meta, Fallback<Context, typeof this>>, options?: Omit<FastEventListenOptions<AllEvents, Meta>, 'count'>): FastEventSubscriber;
556
578
  /**
557
579
  *
558
580
  * 当调用on/once/onAny时如果没有指定监听器,则调用此方法
@@ -561,9 +583,9 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
561
583
  *
562
584
  */
563
585
  onMessage(message: TypedFastEventMessage<AllEvents, Meta>, args: FastEventListenerArgs<Meta>): void;
564
- off(listener: FastEventListener<any, any, any>): void;
565
- off(type: string, listener: FastEventListener<any, any, any>): void;
566
- off(type: Types, listener: FastEventListener<any, any, any>): void;
586
+ off(listener: TypedFastEventListener<any, any, any>): void;
587
+ off(type: string, listener: TypedFastEventListener<any, any, any>): void;
588
+ off(type: Types, listener: TypedFastEventListener<any, any, any>): void;
567
589
  off(type: string): void;
568
590
  off(type: Types): void;
569
591
  /**
@@ -730,6 +752,7 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
730
752
  [K in T]: K extends Types ? AllEvents[K] : any;
731
753
  }, Meta>, options?: FastEventListenerArgs<Meta>): R[];
732
754
  emit<R = any>(message: FastEventEmitMessage<AllEvents, Meta>, options?: FastEventListenerArgs<Meta>): R[];
755
+ getListeners(type: keyof AllEvents): FastListenerMeta[];
733
756
  /**
734
757
  * 异步触发事件
735
758
  * @param type - 事件类型,可以是字符串或预定义的事件类型
@@ -1034,4 +1057,4 @@ declare const FastEventDirectives: {
1034
1057
  clearRetain: symbol;
1035
1058
  };
1036
1059
 
1037
- export { AbortError, BroadcastEvent, CancelError, type ChangeFieldType, type DeepPartial, type Dict, type Expand, type Fallback, 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 FastEventMessageExtends, 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, type TypedFastEventMessage, type TypedFastEventMessageOptional, UnboundError, type Unique, __FastEventScope__, __FastEvent__ };
1060
+ export { AbortError, BroadcastEvent, CancelError, type ChangeFieldType, type DeepPartial, type Dict, type Expand, type Fallback, FastEvent, 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 FastEventListeners, type FastEventMessage, type FastEventMessageExtends, 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, type TypedFastEventAnyListener, type TypedFastEventListener, type TypedFastEventMessage, type TypedFastEventMessageOptional, UnboundError, type Unique, __FastEventScope__, __FastEvent__ };
@@ -1,2 +1,2 @@
1
- 'use strict';var B=Object.defineProperty;var Q=(n,t,e)=>t in n?B(n,t,{enumerable:true,configurable:true,writable:true,value:e}):n[t]=e;var a=(n,t)=>B(n,"name",{value:t,configurable:true});var u=(n,t,e)=>Q(n,typeof t!="symbol"?t+"":t,e);var et=Symbol.for("__FastEvent__"),st=Symbol.for("__FastEventScope__"),$=class $ extends Error{constructor(t){super(t);}};a($,"FastEventError");var m=$,O=class O extends m{};a(O,"TimeoutError");var W=O,T=class T extends m{};a(T,"UnboundError");var y=T,j=class j extends m{};a(j,"AbortError");var g=j,F=class F extends m{};a(F,"CancelError");var v=F,R=class R extends m{};a(R,"QueueOverflowError");var V=R,d={clearRetain:Symbol.for("ClearRetain")};function E(n,t,e,s){let i,r={},o={};return typeof n[0]=="object"?(Object.assign(o,n[0]),r=typeof n[1]=="boolean"?{retain:n[1]}:n[1]||{},i=n[0].meta):(o.type=n[0],o.payload=n[1],r=typeof n[2]=="boolean"?{retain:n[2]}:n[2]||{}),i=Object.assign({},t,e,r.meta,i),Object.keys(i).length===0&&(i=void 0),o.meta=i,r.executor===void 0&&(r.executor=s),[o,r]}a(E,"parseEmitArgs");function I(n){return n?typeof n=="object"&&"__FastEventScope__"in n:false}a(I,"isFastEventScope");function L(n,t,e){let s=n[0],i=I(n[1])?n[1]:void 0,r=(i?n[2]:n[1])||{};return r.meta=Object.assign({},t,r?.meta),r.context=r.context!==void 0?r.context:e,[s,i,r]}a(L,"parseScopeArgs");function x(n,t){return Object.defineProperty(n,"name",{value:t||"anonymous",configurable:true}),n}a(x,"renameFn");var M=class M{constructor(t){u(this,"__FastEventScope__",true);u(this,"_options",{});u(this,"types",{events:void 0,meta:void 0,context:void 0,message:void 0});u(this,"prefix","");u(this,"emitter");this._options=Object.assign({},this._initOptions(t));}get context(){return this.options.context||this}get options(){return this._options}bind(t,e,s){this.emitter=t,this._options=Object.assign(this._options,{scope:e},s),e.length>0&&!e.endsWith(t.options.delimiter)&&(this.prefix=e+t.options.delimiter);}_initOptions(t){return t}_getScopeListener(t){let e=this.prefix;if(e.length===0)return t;t||(t=(this._options.onMessage||this.onMessage).bind(this));let s=this;return x(function(r,o){if(r.type.startsWith(e))return t.call(s.context,Object.assign({},r,{type:r.type.substring(e.length)}),o)},t.name)}_getScopeType(t){return t===void 0?void 0:this.prefix+t}_fixScopeType(t){return t.startsWith(this.prefix)?t.substring(this.prefix.length):t}on(){if(!this.emitter)throw new y;let t=[...arguments];return t[0]=this._getScopeType(t[0]),t[1]=this._getScopeListener(t[1]),this.emitter.on(...t)}once(){return this.on(arguments[0],arguments[1],Object.assign({},arguments[2],{count:1}))}onAny(){return this.on("**",...arguments)}off(){let t=arguments;typeof t[0]=="string"&&(t[0]=this._getScopeType(t[0])),this.emitter.off(...t);}offAll(){this.emitter.offAll(this.prefix.substring(0,this.prefix.length-1));}clear(){this.emitter.clear(this.prefix.substring(0,this.prefix.length-1));}emit(){if(arguments.length===2&&typeof arguments[0]=="string"&&arguments[1]===d.clearRetain)return this.emitter.emit(this._getScopeType(arguments[0]));let[t,e]=E(arguments,this.emitter.options.meta,this.options.meta,this.options.executor);return t.type=this._getScopeType(t.type),this.emitter.emit(t,e)}async emitAsync(){return (await Promise.allSettled(this.emit.apply(this,arguments))).map(e=>e.status==="fulfilled"?e.value:e.reason)}async waitFor(){let t=arguments[0],e=arguments[1],s=await this.emitter.waitFor(this._getScopeType(t),e);return Object.assign({},s,{type:this._fixScopeType(s.type)})}scope(){let[t,e,s]=L(arguments,this.options.meta,this.options.context),i;return e?i=e:i=new M,i.bind(this.emitter,this.prefix+t,s),i}onMessage(t,e){}};a(M,"FastEventScope");var S=M;function P(n,t){if(n.length!==t.length&&n.length>0&&t[t.length-1]!=="**")return false;let e=[...t];t.length>0&&t[t.length-1]==="**"&&e.splice(t.length-1,1,...Array.from({length:n.length-t.length+1}).fill("*"));for(let s=0;s<n.length;s++)if(e[s]!=="*"&&e[s]!==n[s])return false;return true}a(P,"isPathMatched");function X(n,t){let e=[];for(;;){let s=n.findIndex(i=>t(i));if(s===-1){e.push(s);break}n.splice(s,1);}return e}a(X,"removeItem");function p(n){return n&&typeof n=="function"}a(p,"isFunction");var q=Symbol.for("__expandable__");function z(n){return n[q]=true,n}a(z,"expandable");function G(n){return n&&n[q]}a(G,"isExpandable");function H(n){for(let t=0;t<n.length;t++){let e=n[t];Array.isArray(e)&&G(e)&&(n.splice(t,1,...e),t+=e.length-1);}return n}a(H,"expandEmitResults");function J(n){return n&&typeof n=="object"&&"off"in n&&"listener"in n}a(J,"isSubsctiber");function K(n,t){return n.catch(e=>(t&&t(e),Promise.resolve(e)))}a(K,"tryReturnError");var N=class N{constructor(t){u(this,"__FastEvent__",true);u(this,"listeners",{__listeners:[]});u(this,"_options");u(this,"_delimiter","/");u(this,"_context");u(this,"retainedMessages",new Map);u(this,"listenerCount",0);u(this,"types",{events:void 0,meta:void 0,context:void 0,message:void 0});this._options=Object.assign({debug:false,id:Math.random().toString(36).substring(2),delimiter:"/",context:null,ignoreErrors:true,meta:void 0,expandEmitResults:true},this._initOptions(t)),this._delimiter=this._options.delimiter,this._context=this._options.context,this._enableDevTools();}get options(){return this._options}get context(){return this.options.context||this}get meta(){return this.options.meta}get id(){return this._options.id}_initOptions(t){return t}_addListener(t,e,s){let{count:i,prepend:r}=s,o=0;return [this._forEachNodes(t,h=>{let f=[e,i,0];r?(h.__listeners.splice(0,0,f),o=0):(h.__listeners.push(f),o=h.__listeners.length-1),this.listenerCount++;}),o]}_enableDevTools(){this.options.debug&&globalThis.__FLEXEVENT_DEVTOOLS__&&globalThis.__FLEXEVENT_DEVTOOLS__.add(this);}_forEachNodes(t,e){if(t.length===0)return;let s=this.listeners;for(let i=0;i<t.length;i++){let r=t[i];if(r in s||(s[r]={__listeners:[]}),i===t.length-1){let o=s[r];return e(o,s),o}else s=s[r];}}_removeListener(t,e,s){s&&X(t.__listeners,i=>{i=Array.isArray(i)?i[0]:i;let r=i===s;return r&&(this.listenerCount--,p(this._options.onRemoveListener)&&this._options.onRemoveListener(e.join(this._delimiter),s)),r});}_pipeListener(t,e){return e.forEach(s=>{t=x(s(t),t.name);}),t}on(){let t=arguments[0],e=p(arguments[1])?arguments[1]:(this._options.onMessage||this.onMessage).bind(this),s=Object.assign({count:0,prepend:false},p(arguments[1])?arguments[2]:arguments[1]);if(t.length===0)throw new Error("event type cannot be empty");if(p(this._options.onAddListener)){let h=this._options.onAddListener(t,e,s);if(h===false)throw new v;if(J(h))return h}let i=t.split(this._delimiter);if(s.pipes&&s.pipes.length>0&&(e=this._pipeListener(e,s.pipes)),p(s.filter)||p(s.off)){let h=e;e=x(function(f,l){if(p(s.off)&&s.off.call(this,f,l)){c();return}if(p(s.filter)){if(s.filter.call(this,f,l))return h.call(this,f,l)}else return h.call(this,f,l)},e.name);}let[r,o]=this._addListener(i,e,s),c=a(()=>r&&this._removeListener(r,i,e),"off");return this._emitRetainMessage(t,r,o),{off:c,listener:e}}once(){return p(arguments[1])?this.on(arguments[0],arguments[1],Object.assign({},arguments[2],{count:1})):this.on(arguments[0],Object.assign({},arguments[2],{count:1}))}onAny(){return this.on("**",arguments[0],arguments[1])}onMessage(t,e){}off(){let t=arguments,e=p(t[0])?void 0:t[0],s=p(t[0])?t[0]:t[1],i=e?e.split(this._delimiter):[],r=e?e.includes("*"):false;if(e&&!r)this._traverseToPath(this.listeners,i,o=>{s?this._removeListener(o,i,s):e&&(o.__listeners=[]);});else {let o=r?[]:i;this._traverseListeners(this.listeners,o,(c,h)=>{(s!==void 0||r&&P(c,i))&&(s?this._removeListener(h,i,s):h.__listeners=[]);});}}offAll(t){if(t){let e=t.split(this._delimiter),s=0;this._traverseListeners(this.listeners,e,(i,r)=>{s+=r.__listeners.length,r.__listeners=[];}),this.listenerCount-=s,this._removeRetainedEvents(t);}else {let e=0;this._traverseListeners(this.listeners,[],(s,i)=>{e+=i.__listeners.length;}),this.listenerCount-=e,this.retainedMessages.clear(),this.listeners={__listeners:[]};}p(this._options.onClearListeners)&&this._options.onClearListeners.call(this);}_removeRetainedEvents(t){t||this.retainedMessages.clear(),t?.endsWith(this._delimiter)&&(t+=this._delimiter),this.retainedMessages.delete(t);for(let e of this.retainedMessages.keys())e.startsWith(t)&&this.retainedMessages.delete(e);}clear(t){this.offAll(t),this._removeRetainedEvents(t);}_emitRetainMessage(t,e,s){let i=[];if(t.includes("*")){let r=t.split(this._delimiter);this.retainedMessages.forEach((o,c)=>{let h=c.split(this._delimiter);P(h,r)&&i.push(o);});}else this.retainedMessages.has(t)&&i.push(this.retainedMessages.get(t));e&&i.forEach(r=>{this._executeListeners([e],r,{},o=>o[0]===e.__listeners[s][0]);});}_traverseToPath(t,e,s,i=0,r){if(i>=e.length){s(t);return}let o=e[i];if(r===true){this._traverseToPath(t,e,s,i+1,true);return}"*"in t&&this._traverseToPath(t["*"],e,s,i+1),"**"in t&&this._traverseToPath(t["**"],e,s,i+1,true),o in t&&this._traverseToPath(t[o],e,s,i+1);}_traverseListeners(t,e,s){let i=t;e&&e.length>0&&this._traverseToPath(t,e,o=>{i=o;});let r=a((o,c,h)=>{c(h,o);for(let[f,l]of Object.entries(o))f.startsWith("__")||l&&r(l,c,[...h,f]);},"traverseNodes");r(i,s,[]);}_onListenerError(t,e,s,i){if(i instanceof Error&&(i._emitter=`${t.name||"anonymous"}:${e.type}`),p(this._options.onListenerError))try{this._options.onListenerError.call(this,i,t,e,s);}catch{}if(this._options.ignoreErrors)return i;throw i}_executeListener(t,e,s,i=false){try{if(s&&s.abortSignal&&s.abortSignal.aborted)return this._onListenerError(t,e,s,new g(t.name));let r=t.call(this.context,e,s);return i&&r&&r instanceof Promise&&(r=K(r,o=>this._onListenerError(t,e,s,o))),r}catch(r){return this._onListenerError(t,e,s,r)}}_getListenerExecutor(t){if(!t)return;let e=t.executor||this._options.executor;if(p(e))return e}_executeListeners(t,e,s,i){if(!t||t.length===0)return [];let r=t.reduce((o,c)=>o.concat(c.__listeners.filter(h=>p(i)?i(h,c):true).map((h,f)=>[h,f,c.__listeners])),[]);try{let o=this._getListenerExecutor(s);if(o){let c=o(r.map(h=>h[0]),e,s,this._executeListener.bind(this));return Array.isArray(c)?c:[c]}else return r.map(c=>this._executeListener(c[0][0],e,s,!0))}finally{for(let o=r.length-1;o>=0;o--){let c=r[o][0];c[2]++,c[1]>0&&c[1]<=c[2]&&r[o][2].splice(o,1);}}}emit(){if(arguments.length===2&&typeof arguments[0]=="string"&&arguments[1]===d.clearRetain)return this.retainedMessages.delete(arguments[0]),[];let[t,e]=E(arguments,this.options.meta);p(e.parseArgs)&&e.parseArgs(t,e);let s=t.type.split(this._delimiter);e.retain&&this.retainedMessages.set(t.type,t);let i=[],r=[];if(this._traverseToPath(this.listeners,s,o=>{r.push(o);}),p(this._options.onBeforeExecuteListener)){let o=this._options.onBeforeExecuteListener.call(this,t,e);if(Array.isArray(o))return o;if(o===false)throw new g(t.type)}return i.push(...this._executeListeners(r,t,e)),p(this._options.onAfterExecuteListener)&&this._options.onAfterExecuteListener.call(this,t,i,r),this._options.expandEmitResults&&H(i),i}async emitAsync(){return (await Promise.allSettled(this.emit.apply(this,arguments))).map(e=>e.status==="fulfilled"?e.value:e.reason)}waitFor(){let t=arguments[0],e=arguments[1];return new Promise((s,i)=>{let r,o,c=a(h=>{clearTimeout(r),o.off(),s(h);},"listener");e&&e>0&&(r=setTimeout(()=>{o&&o.off(),i(new Error("wait for event<"+t+"> is timeout"));},e)),o=this.on(t,c);})}scope(){let[t,e,s]=L(arguments,this.options.meta,this.options.context),i;return e?i=e:i=new S,i.bind(this,t,s),i}};a(N,"FastEvent");var _=N;var b="::",A="@",Y="data";function Z(n){return n?typeof n=="object"&&"type"in n:false}a(Z,"isFastEventMessage");function w(n,t="/"){let e=Z(n[0]),s=Object.assign({payload:e?n[0].payload:n[1]},e?n[0]:{},{type:`${A}${t}${e?n[0].type:"data"}`}),i=e?n[1]:n[2];return [s,i]}a(w,"parseBroadcaseArgs");var D=class D extends _{constructor(e){super(e);u(this,"nodes");this.nodes=new Map;}add(...e){e.forEach(s=>{if(this.nodes.has(s.id))throw new Error(`Node with id ${s.id} already exists`);s.options.delimiter=this.options.delimiter,s.options.debug=this.options.debug,s.options.ignoreErrors=this.options.ignoreErrors,this.nodes.set(s.id,s),this.emit(`$disconnect${this.options.delimiter}${s.id}`,d.clearRetain),this.emit(`$connect${this.options.delimiter}${s.id}`,s.id,true);});}remove(e){let s=this.nodes.get(e);s&&(s.eventbus=void 0,this.nodes.delete(e),this.emit(`$connect${this.options.delimiter}${s.id}`,d.clearRetain),this.emit(`$disconnect${this.options.delimiter}${s.id}`,s.id,true));}broadcast(){let[e,s]=w(arguments,this.options.delimiter);return this.emit(e,s)}send(e,s){return e.type=`${e.to}${this.options.delimiter}${Y}`,this.emit(e,s)[0]}};a(D,"FastEventBus");var k=D;var C=class C extends _{constructor(e){super(e);u(this,"eventbus");u(this,"_subscribers",[]);this.options.onBeforeExecuteListener=this._onBeforeExecuteListener.bind(this),this.options.onAddListener=this._onAddListener.bind(this),this._subscribers.push(this.on("data"));}_onBeforeExecuteListener(e,s){if(e.type.includes(b))return e.type=e.type.replace(b,this.eventbus.options.delimiter),e.from=this.id,this.eventbus.emit(e,s)}_onAddListener(e,s,i){if(e.includes(b)){let[r,o]=e.split(b);if(r===this.id)return;let c,h=this.eventbus.on(`$connect${this.options.delimiter}*`,f=>{if(f.payload===r){let l=this.eventbus.nodes.get(r);l&&(c=l.on(o,s,i)),h.off();}});return {off:a(()=>c?c.off():h.off(),"off"),listener:c?c.listener:h.listener}}}connect(e){this.eventbus=e,this.eventbus.add(this),this._subscribers.push(this.eventbus.on(`${A}${this.eventbus.options.delimiter}**`,this.onMessage.bind(this))),this._onSubscribeForNode();}disconnect(){this.eventbus?.remove(this.id),this._subscribers.forEach(e=>e.off());}_onSubscribeForNode(){let e=`${this.id}${this.eventbus.options.delimiter}`;this._subscribers.push(this.eventbus.on(`${e}**`,(s,i)=>(s.type=s.type.substring(e.length),z(this.emit(s,i)))));}send(e,s,i){this._assertConnected();let r={type:"data",from:this.id,to:e,payload:s};return this.eventbus.send(r,i)}_assertConnected(){if(!this.eventbus)throw new Error("Node is not connected to any event bus")}broadcast(){this._assertConnected();let[e,s]=w(arguments,this.options.delimiter);return e.from=this.id,this.eventbus.broadcast(e,s)}onMessage(e,s){}};a(C,"FastEventBusNode");var U=C;exports.AbortError=g;exports.BroadcastEvent=A;exports.CancelError=v;exports.FastEvent=_;exports.FastEventBus=k;exports.FastEventBusNode=U;exports.FastEventDirectives=d;exports.FastEventError=m;exports.FastEventScope=S;exports.NamespaceDelimiter=b;exports.NodeDataEvent=Y;exports.QueueOverflowError=V;exports.TimeoutError=W;exports.UnboundError=y;exports.__FastEventScope__=st;exports.__FastEvent__=et;//# sourceMappingURL=index.js.map
1
+ 'use strict';var B=Object.defineProperty;var Q=(n,e,t)=>e in n?B(n,e,{enumerable:true,configurable:true,writable:true,value:t}):n[e]=t;var a=(n,e)=>B(n,"name",{value:e,configurable:true});var u=(n,e,t)=>Q(n,typeof e!="symbol"?e+"":e,t);var te=Symbol.for("__FastEvent__"),se=Symbol.for("__FastEventScope__"),$=class $ extends Error{constructor(e){super(e);}};a($,"FastEventError");var m=$,T=class T extends m{};a(T,"TimeoutError");var W=T,O=class O extends m{};a(O,"UnboundError");var y=O,j=class j extends m{};a(j,"AbortError");var g=j,F=class F extends m{};a(F,"CancelError");var v=F,R=class R extends m{};a(R,"QueueOverflowError");var V=R,d={clearRetain:Symbol.for("ClearRetain")};function E(n,e,t,s){let i,r={},o={};return typeof n[0]=="object"?(Object.assign(o,n[0]),r=typeof n[1]=="boolean"?{retain:n[1]}:n[1]||{},i=n[0].meta):(o.type=n[0],o.payload=n[1],r=typeof n[2]=="boolean"?{retain:n[2]}:n[2]||{}),i=Object.assign({},e,t,r.meta,i),Object.keys(i).length===0&&(i=void 0),o.meta=i,r.executor===void 0&&(r.executor=s),[o,r]}a(E,"parseEmitArgs");function I(n){return n?typeof n=="object"&&"__FastEventScope__"in n:false}a(I,"isFastEventScope");function L(n,e,t){let s=n[0],i=I(n[1])?n[1]:void 0,r=(i?n[2]:n[1])||{};return r.meta=Object.assign({},e,r?.meta),r.context=r.context!==void 0?r.context:t,[s,i,r]}a(L,"parseScopeArgs");function x(n,e){return Object.defineProperty(n,"name",{value:e||"anonymous",configurable:true}),n}a(x,"renameFn");var M=class M{constructor(e){u(this,"__FastEventScope__",true);u(this,"_options",{});u(this,"types",{events:void 0,meta:void 0,context:void 0,message:void 0,listeners:void 0,anyListener:void 0});u(this,"prefix","");u(this,"emitter");this._options=Object.assign({},this._initOptions(e));}get context(){return this.options.context||this}get options(){return this._options}bind(e,t,s){this.emitter=e,this._options=Object.assign(this._options,{scope:t},s),t.length>0&&!t.endsWith(e.options.delimiter)&&(this.prefix=t+e.options.delimiter);}_initOptions(e){return e}_getScopeListener(e){let t=this.prefix;if(t.length===0)return e;e||(e=(this._options.onMessage||this.onMessage).bind(this));let s=this;return x(function(r,o){if(r.type.startsWith(t))return e.call(s.context,Object.assign({},r,{type:r.type.substring(t.length)}),o)},e.name)}_getScopeType(e){return e===void 0?void 0:this.prefix+e}_fixScopeType(e){return e.startsWith(this.prefix)?e.substring(this.prefix.length):e}on(){if(!this.emitter)throw new y;let e=[...arguments];return e[0]=this._getScopeType(e[0]),e[1]=this._getScopeListener(e[1]),this.emitter.on(...e)}once(){return this.on(arguments[0],arguments[1],Object.assign({},arguments[2],{count:1}))}onAny(){return this.on("**",...arguments)}off(){let e=arguments;typeof e[0]=="string"&&(e[0]=this._getScopeType(e[0])),this.emitter.off(...e);}offAll(){this.emitter.offAll(this.prefix.substring(0,this.prefix.length-1));}clear(){this.emitter.clear(this.prefix.substring(0,this.prefix.length-1));}emit(){if(arguments.length===2&&typeof arguments[0]=="string"&&arguments[1]===d.clearRetain)return this.emitter.emit(this._getScopeType(arguments[0]));let[e,t]=E(arguments,this.emitter.options.meta,this.options.meta,this.options.executor);return e.type=this._getScopeType(e.type),this.emitter.emit(e,t)}async emitAsync(){return (await Promise.allSettled(this.emit.apply(this,arguments))).map(t=>t.status==="fulfilled"?t.value:t.reason)}async waitFor(){let e=arguments[0],t=arguments[1],s=await this.emitter.waitFor(this._getScopeType(e),t);return Object.assign({},s,{type:this._fixScopeType(s.type)})}scope(){let[e,t,s]=L(arguments,this.options.meta,this.options.context),i;return t?i=t:i=new M,i.bind(this.emitter,this.prefix+e,s),i}onMessage(e,t){}};a(M,"FastEventScope");var S=M;function P(n,e){if(n.length!==e.length&&n.length>0&&e[e.length-1]!=="**")return false;let t=[...e];e.length>0&&e[e.length-1]==="**"&&t.splice(e.length-1,1,...Array.from({length:n.length-e.length+1}).fill("*"));for(let s=0;s<n.length;s++)if(t[s]!=="*"&&t[s]!==n[s])return false;return true}a(P,"isPathMatched");function X(n,e){let t=[];for(;;){let s=n.findIndex(i=>e(i));if(s===-1){t.push(s);break}n.splice(s,1);}return t}a(X,"removeItem");function p(n){return n&&typeof n=="function"}a(p,"isFunction");var q=Symbol.for("__expandable__");function z(n){return n[q]=true,n}a(z,"expandable");function G(n){return n&&n[q]}a(G,"isExpandable");function H(n){for(let e=0;e<n.length;e++){let t=n[e];Array.isArray(t)&&G(t)&&(n.splice(e,1,...t),e+=t.length-1);}return n}a(H,"expandEmitResults");function J(n){return n&&typeof n=="object"&&"off"in n&&"listener"in n}a(J,"isSubsctiber");function K(n,e){return n.catch(t=>(e&&e(t),Promise.resolve(t)))}a(K,"tryReturnError");var N=class N{constructor(e){u(this,"__FastEvent__",true);u(this,"listeners",{__listeners:[]});u(this,"_options");u(this,"_delimiter","/");u(this,"_context");u(this,"retainedMessages",new Map);u(this,"listenerCount",0);u(this,"types",{events:void 0,meta:void 0,context:void 0,message:void 0,listeners:void 0,anyListener:void 0});this._options=Object.assign({debug:false,id:Math.random().toString(36).substring(2),delimiter:"/",context:null,ignoreErrors:true,meta:void 0,expandEmitResults:true},this._initOptions(e)),this._delimiter=this._options.delimiter,this._context=this._options.context,this._enableDevTools();}get options(){return this._options}get context(){return this.options.context||this}get meta(){return this.options.meta}get id(){return this._options.id}_initOptions(e){return e}_addListener(e,t,s){let{count:i,prepend:r}=s,o=0;return [this._forEachNodes(e,h=>{let f=[t,i,0];s.tag&&f.push(s.tag),r?(h.__listeners.splice(0,0,f),o=0):(h.__listeners.push(f),o=h.__listeners.length-1),this.listenerCount++;}),o]}_enableDevTools(){this.options.debug&&globalThis.__FLEXEVENT_DEVTOOLS__&&globalThis.__FLEXEVENT_DEVTOOLS__.add(this);}_forEachNodes(e,t){if(e.length===0)return;let s=this.listeners;for(let i=0;i<e.length;i++){let r=e[i];if(r in s||(s[r]={__listeners:[]}),i===e.length-1){let o=s[r];return t(o,s),o}else s=s[r];}}_removeListener(e,t,s){s&&X(e.__listeners,i=>{i=Array.isArray(i)?i[0]:i;let r=i===s;return r&&(this.listenerCount--,p(this._options.onRemoveListener)&&this._options.onRemoveListener(t.join(this._delimiter),s)),r});}_pipeListener(e,t){return t.forEach(s=>{e=x(s(e),e.name);}),e}on(){let e=arguments[0],t=p(arguments[1])?arguments[1]:(this._options.onMessage||this.onMessage).bind(this),s=Object.assign({count:0,prepend:false},p(arguments[1])?arguments[2]:arguments[1]);if(e.length===0)throw new Error("event type cannot be empty");if(p(this._options.onAddListener)){let h=this._options.onAddListener(e,t,s);if(h===false)throw new v;if(J(h))return h}let i=e.split(this._delimiter);if(s.pipes&&s.pipes.length>0&&(t=this._pipeListener(t,s.pipes)),p(s.filter)||p(s.off)){let h=t;t=x(function(f,l){if(p(s.off)&&s.off.call(this,f,l)){c();return}if(p(s.filter)){if(s.filter.call(this,f,l))return h.call(this,f,l)}else return h.call(this,f,l)},t.name);}let[r,o]=this._addListener(i,t,s),c=a(()=>r&&this._removeListener(r,i,t),"off");return this._emitRetainMessage(e,r,o),{off:c,listener:t}}once(){return p(arguments[1])?this.on(arguments[0],arguments[1],Object.assign({},arguments[2],{count:1})):this.on(arguments[0],Object.assign({},arguments[2],{count:1}))}onAny(){return this.on("**",arguments[0],arguments[1])}onMessage(e,t){}off(){let e=arguments,t=p(e[0])?void 0:e[0],s=p(e[0])?e[0]:e[1],i=t?t.split(this._delimiter):[],r=t?t.includes("*"):false;if(t&&!r)this._traverseToPath(this.listeners,i,o=>{s?this._removeListener(o,i,s):t&&(o.__listeners=[]);});else {let o=r?[]:i;this._traverseListeners(this.listeners,o,(c,h)=>{(s!==void 0||r&&P(c,i))&&(s?this._removeListener(h,i,s):h.__listeners=[]);});}}offAll(e){if(e){let t=e.split(this._delimiter),s=0;this._traverseListeners(this.listeners,t,(i,r)=>{s+=r.__listeners.length,r.__listeners=[];}),this.listenerCount-=s,this._removeRetainedEvents(e);}else {let t=0;this._traverseListeners(this.listeners,[],(s,i)=>{t+=i.__listeners.length;}),this.listenerCount-=t,this.retainedMessages.clear(),this.listeners={__listeners:[]};}p(this._options.onClearListeners)&&this._options.onClearListeners.call(this);}_removeRetainedEvents(e){e||this.retainedMessages.clear(),e?.endsWith(this._delimiter)&&(e+=this._delimiter),this.retainedMessages.delete(e);for(let t of this.retainedMessages.keys())t.startsWith(e)&&this.retainedMessages.delete(t);}clear(e){this.offAll(e),this._removeRetainedEvents(e);}_emitRetainMessage(e,t,s){let i=[];if(e.includes("*")){let r=e.split(this._delimiter);this.retainedMessages.forEach((o,c)=>{let h=c.split(this._delimiter);P(h,r)&&i.push(o);});}else this.retainedMessages.has(e)&&i.push(this.retainedMessages.get(e));t&&i.forEach(r=>{this._executeListeners([t],r,{},o=>o[0]===t.__listeners[s][0]);});}_traverseToPath(e,t,s,i=0,r){if(i>=t.length){s(e);return}let o=t[i];if(r===true){this._traverseToPath(e,t,s,i+1,true);return}"*"in e&&this._traverseToPath(e["*"],t,s,i+1),"**"in e&&this._traverseToPath(e["**"],t,s,i+1,true),o in e&&this._traverseToPath(e[o],t,s,i+1);}_traverseListeners(e,t,s){let i=e;t&&t.length>0&&this._traverseToPath(e,t,o=>{i=o;});let r=a((o,c,h)=>{c(h,o);for(let[f,l]of Object.entries(o))f.startsWith("__")||l&&r(l,c,[...h,f]);},"traverseNodes");r(i,s,[]);}_onListenerError(e,t,s,i){if(i instanceof Error&&(i._emitter=`${e.name||"anonymous"}:${t.type}`),p(this._options.onListenerError))try{this._options.onListenerError.call(this,i,e,t,s);}catch{}if(this._options.ignoreErrors)return i;throw i}_executeListener(e,t,s,i=false){try{if(s&&s.abortSignal&&s.abortSignal.aborted)return this._onListenerError(e,t,s,new g(e.name));let r=e.call(this.context,t,s);return i&&r&&r instanceof Promise&&(r=K(r,o=>this._onListenerError(e,t,s,o))),r}catch(r){return this._onListenerError(e,t,s,r)}}_getListenerExecutor(e){if(!e)return;let t=e.executor||this._options.executor;if(p(t))return t}_executeListeners(e,t,s,i){if(!e||e.length===0)return [];let r=e.reduce((o,c)=>o.concat(c.__listeners.filter(h=>p(i)?i(h,c):true).map((h,f)=>[h,f,c.__listeners])),[]);try{let o=this._getListenerExecutor(s);if(o){let c=o(r.map(h=>h[0]),t,s,this._executeListener.bind(this));return Array.isArray(c)?c:[c]}else return r.map(c=>this._executeListener(c[0][0],t,s,!0))}finally{for(let o=r.length-1;o>=0;o--){let c=r[o][0];c[2]++,c[1]>0&&c[1]<=c[2]&&r[o][2].splice(o,1);}}}emit(){if(arguments.length===2&&typeof arguments[0]=="string"&&arguments[1]===d.clearRetain)return this.retainedMessages.delete(arguments[0]),[];let[e,t]=E(arguments,this.options.meta);p(t.parseArgs)&&t.parseArgs(e,t);let s=e.type.split(this._delimiter);t.retain&&this.retainedMessages.set(e.type,e);let i=[],r=[];if(this._traverseToPath(this.listeners,s,o=>{r.push(o);}),p(this._options.onBeforeExecuteListener)){let o=this._options.onBeforeExecuteListener.call(this,e,t);if(Array.isArray(o))return o;if(o===false)throw new g(e.type)}return i.push(...this._executeListeners(r,e,t)),p(this._options.onAfterExecuteListener)&&this._options.onAfterExecuteListener.call(this,e,i,r),this._options.expandEmitResults&&H(i),i}getListeners(e){let t=[],s=e.split(this._delimiter);return this._traverseToPath(this.listeners,s,i=>{t.push(i);}),t[0].__listeners}async emitAsync(){return (await Promise.allSettled(this.emit.apply(this,arguments))).map(t=>t.status==="fulfilled"?t.value:t.reason)}waitFor(){let e=arguments[0],t=arguments[1];return new Promise((s,i)=>{let r,o,c=a(h=>{clearTimeout(r),o.off(),s(h);},"listener");t&&t>0&&(r=setTimeout(()=>{o&&o.off(),i(new Error("wait for event<"+e+"> is timeout"));},t)),o=this.on(e,c);})}scope(){let[e,t,s]=L(arguments,this.options.meta,this.options.context),i;return t?i=t:i=new S,i.bind(this,e,s),i}};a(N,"FastEvent");var _=N;var b="::",A="@",Y="data";function Z(n){return n?typeof n=="object"&&"type"in n:false}a(Z,"isFastEventMessage");function w(n,e="/"){let t=Z(n[0]),s=Object.assign({payload:t?n[0].payload:n[1]},t?n[0]:{},{type:`${A}${e}${t?n[0].type:"data"}`}),i=t?n[1]:n[2];return [s,i]}a(w,"parseBroadcaseArgs");var D=class D extends _{constructor(t){super(t);u(this,"nodes");this.nodes=new Map;}add(...t){t.forEach(s=>{if(this.nodes.has(s.id))throw new Error(`Node with id ${s.id} already exists`);s.options.delimiter=this.options.delimiter,s.options.debug=this.options.debug,s.options.ignoreErrors=this.options.ignoreErrors,this.nodes.set(s.id,s),this.emit(`$disconnect${this.options.delimiter}${s.id}`,d.clearRetain),this.emit(`$connect${this.options.delimiter}${s.id}`,s.id,true);});}remove(t){let s=this.nodes.get(t);s&&(s.eventbus=void 0,this.nodes.delete(t),this.emit(`$connect${this.options.delimiter}${s.id}`,d.clearRetain),this.emit(`$disconnect${this.options.delimiter}${s.id}`,s.id,true));}broadcast(){let[t,s]=w(arguments,this.options.delimiter);return this.emit(t,s)}send(t,s){return t.type=`${t.to}${this.options.delimiter}${Y}`,this.emit(t,s)[0]}};a(D,"FastEventBus");var k=D;var C=class C extends _{constructor(t){super(t);u(this,"eventbus");u(this,"_subscribers",[]);this.options.onBeforeExecuteListener=this._onBeforeExecuteListener.bind(this),this.options.onAddListener=this._onAddListener.bind(this),this._subscribers.push(this.on("data"));}_onBeforeExecuteListener(t,s){if(t.type.includes(b))return t.type=t.type.replace(b,this.eventbus.options.delimiter),t.from=this.id,this.eventbus.emit(t,s)}_onAddListener(t,s,i){if(t.includes(b)){let[r,o]=t.split(b);if(r===this.id)return;let c,h=this.eventbus.on(`$connect${this.options.delimiter}*`,f=>{if(f.payload===r){let l=this.eventbus.nodes.get(r);l&&(c=l.on(o,s,i)),h.off();}});return {off:a(()=>c?c.off():h.off(),"off"),listener:c?c.listener:h.listener}}}connect(t){this.eventbus=t,this.eventbus.add(this),this._subscribers.push(this.eventbus.on(`${A}${this.eventbus.options.delimiter}**`,this.onMessage.bind(this))),this._onSubscribeForNode();}disconnect(){this.eventbus?.remove(this.id),this._subscribers.forEach(t=>t.off());}_onSubscribeForNode(){let t=`${this.id}${this.eventbus.options.delimiter}`;this._subscribers.push(this.eventbus.on(`${t}**`,(s,i)=>(s.type=s.type.substring(t.length),z(this.emit(s,i)))));}send(t,s,i){this._assertConnected();let r={type:"data",from:this.id,to:t,payload:s};return this.eventbus.send(r,i)}_assertConnected(){if(!this.eventbus)throw new Error("Node is not connected to any event bus")}broadcast(){this._assertConnected();let[t,s]=w(arguments,this.options.delimiter);return t.from=this.id,this.eventbus.broadcast(t,s)}onMessage(t,s){}};a(C,"FastEventBusNode");var U=C;exports.AbortError=g;exports.BroadcastEvent=A;exports.CancelError=v;exports.FastEvent=_;exports.FastEventBus=k;exports.FastEventBusNode=U;exports.FastEventDirectives=d;exports.FastEventError=m;exports.FastEventScope=S;exports.NamespaceDelimiter=b;exports.NodeDataEvent=Y;exports.QueueOverflowError=V;exports.TimeoutError=W;exports.UnboundError=y;exports.__FastEventScope__=se;exports.__FastEvent__=te;//# sourceMappingURL=index.js.map
2
2
  //# sourceMappingURL=index.js.map