fastevent 2.1.2 → 2.1.4

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,29 +1,41 @@
1
- type FastListenerExecutor = (listeners: FastListenerMeta[], message: FastEventMessage, args: FastEventListenerArgs, execute: (listener: FastEventListener, message: FastEventMessage, args: FastEventListenerArgs, catchErrors?: boolean) => Promise<any> | any) => Promise<any[]> | any[];
1
+ type FastListenerExecutor = (listeners: FastListenerMeta[], message: TypedFastEventMessage, args: FastEventListenerArgs, execute: (listener: FastEventListener, message: TypedFastEventMessage, args: FastEventListenerArgs, catchErrors?: boolean) => Promise<any> | any) => Promise<any[]> | any[];
2
2
 
3
3
  type FastListenerPipe = (listener: FastEventListener) => FastEventListener;
4
4
 
5
- interface FaseEventMessageExtends {
6
- }
7
5
  interface FastEventMeta {
8
6
  }
9
- type FastEventMessage<Events extends Record<string, any> = Record<string, any>, M = any> = ({
7
+ interface FastEventMessageExtends {
8
+ }
9
+ type FastEventMessage = {
10
+ type: string;
11
+ payload: any;
12
+ meta?: Record<string, any>;
13
+ };
14
+ type TypedFastEventMessage<Events extends Record<string, any> = Record<string, any>, M = any> = ({
10
15
  [K in keyof Events]: {
11
16
  type: Exclude<K, number | symbol>;
12
17
  payload: Events[K];
13
18
  meta: FastEventMeta & M & Record<string, any>;
14
19
  };
15
- }[Exclude<keyof Events, number | symbol>]) & FaseEventMessageExtends;
20
+ }[Exclude<keyof Events, number | symbol>]) & DeepPartial<FastEventMessageExtends>;
21
+ type TypedFastEventMessageOptional<Events extends Record<string, any> = Record<string, any>, M = any> = ({
22
+ [K in keyof Events]: {
23
+ type: Exclude<K, number | symbol>;
24
+ payload: Events[K];
25
+ meta?: DeepPartial<FastEventMeta & M & Record<string, any>>;
26
+ };
27
+ }[Exclude<keyof Events, number | symbol>]) & DeepPartial<FastEventMessageExtends>;
16
28
  type FastEventEmitMessage<Events extends Record<string, any> = Record<string, any>, M = any> = ({
17
29
  [K in keyof Events]: {
18
30
  type: Exclude<K, number | symbol>;
19
31
  payload?: Events[K];
20
- meta?: Partial<FastEventMeta> & M & Record<string, any>;
32
+ meta?: DeepPartial<FastEventMeta & M & Record<string, any>>;
21
33
  };
22
- }[Exclude<keyof Events, number | symbol>]) & FaseEventMessageExtends;
23
- type FastEventListener<T extends string = string, P = any, M = any, C = any> = (this: C, message: FastEventMessage<{
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<{
24
36
  [K in T]: P;
25
37
  }, 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>;
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>;
27
39
  /**
28
40
  * [监听器函数引用,需要执行多少次,实际执行的次数(用于负载均衡时记录)]
29
41
  */
@@ -76,9 +88,9 @@ type FastEventOptions<Meta = Record<string, any>, Context = never> = {
76
88
  onAddListener?: (type: string, listener: FastEventListener, options: FastEventListenOptions<Record<string, any>, Meta>) => boolean | FastEventSubscriber | void;
77
89
  onRemoveListener?: (type: string, listener: FastEventListener) => void;
78
90
  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;
91
+ onListenerError?: ((error: Error, listener: FastEventListener, message: TypedFastEventMessage<any, Meta>, args: FastEventListenerArgs<Meta> | undefined) => void);
92
+ onBeforeExecuteListener?: (message: TypedFastEventMessage<any, Meta>, args: FastEventListenerArgs<Meta>) => boolean | void | any[];
93
+ onAfterExecuteListener?: (message: TypedFastEventMessage<any, Meta>, returns: any[], listeners: FastListenerNode[]) => void;
82
94
  /**
83
95
  * 全局执行器
84
96
  */
@@ -95,13 +107,13 @@ type ScopeEvents<T extends Record<string, any>, Prefix extends string> = PickSco
95
107
  type FastEventListenOptions<Events extends Record<string, any> = Record<string, any>, Meta = any> = {
96
108
  count?: number;
97
109
  prepend?: boolean;
98
- filter?: (message: FastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => boolean;
99
- off?: (message: FastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => boolean;
110
+ filter?: (message: TypedFastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => boolean;
111
+ off?: (message: TypedFastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => boolean;
100
112
  pipes?: FastListenerPipe[];
101
113
  };
102
114
  type FastEventListenerArgs<M = Record<string, any>> = {
103
115
  retain?: boolean;
104
- meta?: Partial<M> & Record<string, any>;
116
+ meta?: DeepPartial<M> & Record<string, any>;
105
117
  abortSignal?: AbortSignal;
106
118
  /**
107
119
  *
@@ -114,7 +126,7 @@ type FastEventListenerArgs<M = Record<string, any>> = {
114
126
  /**
115
127
  * 当emit参数解析完成后的回调,用于修改emit参数
116
128
  */
117
- parseArgs?: (message: FastEventMessage, args: FastEventListenerArgs) => void;
129
+ parseArgs?: (message: TypedFastEventMessage, args: FastEventListenerArgs) => void;
118
130
  };
119
131
  type Merge<T extends object, U extends object> = {
120
132
  [K in keyof T | keyof U]: K extends keyof U ? U[K] : K extends keyof T ? T[K] : never;
@@ -277,6 +289,7 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
277
289
  events: Events;
278
290
  meta: FinalMeta;
279
291
  context: Fallback<Context, typeof this>;
292
+ message: TypedFastEventMessageOptional<Events, FinalMeta>;
280
293
  };
281
294
  prefix: string;
282
295
  emitter: FastEvent<Events>;
@@ -334,14 +347,14 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
334
347
  }, FinalMeta>, options?: FastEventListenerArgs<FinalMeta>): R[];
335
348
  emitAsync<R = any>(type: string, payload?: any, options?: FastEventListenerArgs<FinalMeta>): Promise<[R | Error][]>;
336
349
  emitAsync<R = any>(type: Types, payload?: Events[Types], options?: FastEventListenerArgs<FinalMeta>): Promise<[R | Error][]>;
337
- emitAsync<R = any>(message: FastEventMessage<Events, FinalMeta>, options?: FastEventListenerArgs<FinalMeta>): Promise<[R | Error][]>;
338
- waitFor<T extends Types>(type: T, timeout?: number): Promise<FastEventMessage<{
350
+ emitAsync<R = any>(message: TypedFastEventMessage<Events, FinalMeta>, options?: FastEventListenerArgs<FinalMeta>): Promise<[R | Error][]>;
351
+ waitFor<T extends Types>(type: T, timeout?: number): Promise<TypedFastEventMessage<{
339
352
  [key in T]: Events[T];
340
353
  }, FinalMeta>>;
341
- waitFor(type: string, timeout?: number): Promise<FastEventMessage<{
354
+ waitFor(type: string, timeout?: number): Promise<TypedFastEventMessage<{
342
355
  [key: string]: any;
343
356
  }, FinalMeta>>;
344
- waitFor<P = any>(type: string, timeout?: number): Promise<FastEventMessage<{
357
+ waitFor<P = any>(type: string, timeout?: number): Promise<TypedFastEventMessage<{
345
358
  [key: string]: P;
346
359
  }, FinalMeta>>;
347
360
  /**
@@ -382,7 +395,7 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
382
395
  * 当on/once/onAny未指定监听器时,此为默认监听器
383
396
  * @param message
384
397
  */
385
- onMessage(message: FastEventMessage<Events, FinalMeta>, args: FastEventListenerArgs<FinalMeta>): void;
398
+ onMessage(message: TypedFastEventMessage<Events, FinalMeta>, args: FastEventListenerArgs<FinalMeta>): void;
386
399
  }
387
400
 
388
401
  /**
@@ -410,6 +423,7 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
410
423
  events: AllEvents;
411
424
  meta: Expand<FastEventMeta & Meta & Record<string, any>>;
412
425
  context: Expand<Fallback<Context, typeof this>>;
426
+ message: TypedFastEventMessageOptional<AllEvents, Expand<FastEventMeta & Meta & Record<string, any>>>;
413
427
  };
414
428
  /**
415
429
  * 创建FastEvent实例
@@ -546,7 +560,7 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
546
560
  * 此方法供子类继承
547
561
  *
548
562
  */
549
- onMessage(message: FastEventMessage<AllEvents, Meta>, args: FastEventListenerArgs<Meta>): void;
563
+ onMessage(message: TypedFastEventMessage<AllEvents, Meta>, args: FastEventListenerArgs<Meta>): void;
550
564
  off(listener: FastEventListener<any, any, any>): void;
551
565
  off(type: string, listener: FastEventListener<any, any, any>): void;
552
566
  off(type: Types, listener: FastEventListener<any, any, any>): void;
@@ -791,11 +805,11 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
791
805
  * console.log('服务器就绪');
792
806
  * ```
793
807
  */
794
- waitFor<T extends Types>(type: T, timeout?: number): Promise<FastEventMessage<{
808
+ waitFor<T extends Types>(type: T, timeout?: number): Promise<TypedFastEventMessage<{
795
809
  [key in T]: AllEvents[T];
796
810
  }, Meta>>;
797
- waitFor(type: string, timeout?: number): Promise<FastEventMessage<AllEvents, Meta>>;
798
- waitFor<P = any>(type: string, timeout?: number): Promise<FastEventMessage<{
811
+ waitFor(type: string, timeout?: number): Promise<TypedFastEventMessage<AllEvents, Meta>>;
812
+ waitFor<P = any>(type: string, timeout?: number): Promise<TypedFastEventMessage<{
799
813
  [key: string]: P;
800
814
  }, Meta>>;
801
815
  /**
@@ -1020,4 +1034,4 @@ declare const FastEventDirectives: {
1020
1034
  clearRetain: symbol;
1021
1035
  };
1022
1036
 
1023
- 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__ };
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__ };
@@ -1,29 +1,41 @@
1
- type FastListenerExecutor = (listeners: FastListenerMeta[], message: FastEventMessage, args: FastEventListenerArgs, execute: (listener: FastEventListener, message: FastEventMessage, args: FastEventListenerArgs, catchErrors?: boolean) => Promise<any> | any) => Promise<any[]> | any[];
1
+ type FastListenerExecutor = (listeners: FastListenerMeta[], message: TypedFastEventMessage, args: FastEventListenerArgs, execute: (listener: FastEventListener, message: TypedFastEventMessage, args: FastEventListenerArgs, catchErrors?: boolean) => Promise<any> | any) => Promise<any[]> | any[];
2
2
 
3
3
  type FastListenerPipe = (listener: FastEventListener) => FastEventListener;
4
4
 
5
- interface FaseEventMessageExtends {
6
- }
7
5
  interface FastEventMeta {
8
6
  }
9
- type FastEventMessage<Events extends Record<string, any> = Record<string, any>, M = any> = ({
7
+ interface FastEventMessageExtends {
8
+ }
9
+ type FastEventMessage = {
10
+ type: string;
11
+ payload: any;
12
+ meta?: Record<string, any>;
13
+ };
14
+ type TypedFastEventMessage<Events extends Record<string, any> = Record<string, any>, M = any> = ({
10
15
  [K in keyof Events]: {
11
16
  type: Exclude<K, number | symbol>;
12
17
  payload: Events[K];
13
18
  meta: FastEventMeta & M & Record<string, any>;
14
19
  };
15
- }[Exclude<keyof Events, number | symbol>]) & FaseEventMessageExtends;
20
+ }[Exclude<keyof Events, number | symbol>]) & DeepPartial<FastEventMessageExtends>;
21
+ type TypedFastEventMessageOptional<Events extends Record<string, any> = Record<string, any>, M = any> = ({
22
+ [K in keyof Events]: {
23
+ type: Exclude<K, number | symbol>;
24
+ payload: Events[K];
25
+ meta?: DeepPartial<FastEventMeta & M & Record<string, any>>;
26
+ };
27
+ }[Exclude<keyof Events, number | symbol>]) & DeepPartial<FastEventMessageExtends>;
16
28
  type FastEventEmitMessage<Events extends Record<string, any> = Record<string, any>, M = any> = ({
17
29
  [K in keyof Events]: {
18
30
  type: Exclude<K, number | symbol>;
19
31
  payload?: Events[K];
20
- meta?: Partial<FastEventMeta> & M & Record<string, any>;
32
+ meta?: DeepPartial<FastEventMeta & M & Record<string, any>>;
21
33
  };
22
- }[Exclude<keyof Events, number | symbol>]) & FaseEventMessageExtends;
23
- type FastEventListener<T extends string = string, P = any, M = any, C = any> = (this: C, message: FastEventMessage<{
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<{
24
36
  [K in T]: P;
25
37
  }, 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>;
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>;
27
39
  /**
28
40
  * [监听器函数引用,需要执行多少次,实际执行的次数(用于负载均衡时记录)]
29
41
  */
@@ -76,9 +88,9 @@ type FastEventOptions<Meta = Record<string, any>, Context = never> = {
76
88
  onAddListener?: (type: string, listener: FastEventListener, options: FastEventListenOptions<Record<string, any>, Meta>) => boolean | FastEventSubscriber | void;
77
89
  onRemoveListener?: (type: string, listener: FastEventListener) => void;
78
90
  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;
91
+ onListenerError?: ((error: Error, listener: FastEventListener, message: TypedFastEventMessage<any, Meta>, args: FastEventListenerArgs<Meta> | undefined) => void);
92
+ onBeforeExecuteListener?: (message: TypedFastEventMessage<any, Meta>, args: FastEventListenerArgs<Meta>) => boolean | void | any[];
93
+ onAfterExecuteListener?: (message: TypedFastEventMessage<any, Meta>, returns: any[], listeners: FastListenerNode[]) => void;
82
94
  /**
83
95
  * 全局执行器
84
96
  */
@@ -95,13 +107,13 @@ type ScopeEvents<T extends Record<string, any>, Prefix extends string> = PickSco
95
107
  type FastEventListenOptions<Events extends Record<string, any> = Record<string, any>, Meta = any> = {
96
108
  count?: number;
97
109
  prepend?: boolean;
98
- filter?: (message: FastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => boolean;
99
- off?: (message: FastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => boolean;
110
+ filter?: (message: TypedFastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => boolean;
111
+ off?: (message: TypedFastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => boolean;
100
112
  pipes?: FastListenerPipe[];
101
113
  };
102
114
  type FastEventListenerArgs<M = Record<string, any>> = {
103
115
  retain?: boolean;
104
- meta?: Partial<M> & Record<string, any>;
116
+ meta?: DeepPartial<M> & Record<string, any>;
105
117
  abortSignal?: AbortSignal;
106
118
  /**
107
119
  *
@@ -114,7 +126,7 @@ type FastEventListenerArgs<M = Record<string, any>> = {
114
126
  /**
115
127
  * 当emit参数解析完成后的回调,用于修改emit参数
116
128
  */
117
- parseArgs?: (message: FastEventMessage, args: FastEventListenerArgs) => void;
129
+ parseArgs?: (message: TypedFastEventMessage, args: FastEventListenerArgs) => void;
118
130
  };
119
131
  type Merge<T extends object, U extends object> = {
120
132
  [K in keyof T | keyof U]: K extends keyof U ? U[K] : K extends keyof T ? T[K] : never;
@@ -277,6 +289,7 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
277
289
  events: Events;
278
290
  meta: FinalMeta;
279
291
  context: Fallback<Context, typeof this>;
292
+ message: TypedFastEventMessageOptional<Events, FinalMeta>;
280
293
  };
281
294
  prefix: string;
282
295
  emitter: FastEvent<Events>;
@@ -334,14 +347,14 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
334
347
  }, FinalMeta>, options?: FastEventListenerArgs<FinalMeta>): R[];
335
348
  emitAsync<R = any>(type: string, payload?: any, options?: FastEventListenerArgs<FinalMeta>): Promise<[R | Error][]>;
336
349
  emitAsync<R = any>(type: Types, payload?: Events[Types], options?: FastEventListenerArgs<FinalMeta>): Promise<[R | Error][]>;
337
- emitAsync<R = any>(message: FastEventMessage<Events, FinalMeta>, options?: FastEventListenerArgs<FinalMeta>): Promise<[R | Error][]>;
338
- waitFor<T extends Types>(type: T, timeout?: number): Promise<FastEventMessage<{
350
+ emitAsync<R = any>(message: TypedFastEventMessage<Events, FinalMeta>, options?: FastEventListenerArgs<FinalMeta>): Promise<[R | Error][]>;
351
+ waitFor<T extends Types>(type: T, timeout?: number): Promise<TypedFastEventMessage<{
339
352
  [key in T]: Events[T];
340
353
  }, FinalMeta>>;
341
- waitFor(type: string, timeout?: number): Promise<FastEventMessage<{
354
+ waitFor(type: string, timeout?: number): Promise<TypedFastEventMessage<{
342
355
  [key: string]: any;
343
356
  }, FinalMeta>>;
344
- waitFor<P = any>(type: string, timeout?: number): Promise<FastEventMessage<{
357
+ waitFor<P = any>(type: string, timeout?: number): Promise<TypedFastEventMessage<{
345
358
  [key: string]: P;
346
359
  }, FinalMeta>>;
347
360
  /**
@@ -382,7 +395,7 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
382
395
  * 当on/once/onAny未指定监听器时,此为默认监听器
383
396
  * @param message
384
397
  */
385
- onMessage(message: FastEventMessage<Events, FinalMeta>, args: FastEventListenerArgs<FinalMeta>): void;
398
+ onMessage(message: TypedFastEventMessage<Events, FinalMeta>, args: FastEventListenerArgs<FinalMeta>): void;
386
399
  }
387
400
 
388
401
  /**
@@ -410,6 +423,7 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
410
423
  events: AllEvents;
411
424
  meta: Expand<FastEventMeta & Meta & Record<string, any>>;
412
425
  context: Expand<Fallback<Context, typeof this>>;
426
+ message: TypedFastEventMessageOptional<AllEvents, Expand<FastEventMeta & Meta & Record<string, any>>>;
413
427
  };
414
428
  /**
415
429
  * 创建FastEvent实例
@@ -546,7 +560,7 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
546
560
  * 此方法供子类继承
547
561
  *
548
562
  */
549
- onMessage(message: FastEventMessage<AllEvents, Meta>, args: FastEventListenerArgs<Meta>): void;
563
+ onMessage(message: TypedFastEventMessage<AllEvents, Meta>, args: FastEventListenerArgs<Meta>): void;
550
564
  off(listener: FastEventListener<any, any, any>): void;
551
565
  off(type: string, listener: FastEventListener<any, any, any>): void;
552
566
  off(type: Types, listener: FastEventListener<any, any, any>): void;
@@ -791,11 +805,11 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
791
805
  * console.log('服务器就绪');
792
806
  * ```
793
807
  */
794
- waitFor<T extends Types>(type: T, timeout?: number): Promise<FastEventMessage<{
808
+ waitFor<T extends Types>(type: T, timeout?: number): Promise<TypedFastEventMessage<{
795
809
  [key in T]: AllEvents[T];
796
810
  }, Meta>>;
797
- waitFor(type: string, timeout?: number): Promise<FastEventMessage<AllEvents, Meta>>;
798
- waitFor<P = any>(type: string, timeout?: number): Promise<FastEventMessage<{
811
+ waitFor(type: string, timeout?: number): Promise<TypedFastEventMessage<AllEvents, Meta>>;
812
+ waitFor<P = any>(type: string, timeout?: number): Promise<TypedFastEventMessage<{
799
813
  [key: string]: P;
800
814
  }, Meta>>;
801
815
  /**
@@ -1020,4 +1034,4 @@ declare const FastEventDirectives: {
1020
1034
  clearRetain: symbol;
1021
1035
  };
1022
1036
 
1023
- 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__ };
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__ };
@@ -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});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});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,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
2
2
  //# sourceMappingURL=index.js.map