fastevent 2.1.1 → 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.
- package/dist/eventbus/index.d.mts +60 -28
- package/dist/eventbus/index.d.ts +60 -28
- package/dist/eventbus/index.js +1 -1
- package/dist/eventbus/index.js.map +1 -1
- package/dist/eventbus/index.mjs +1 -1
- package/dist/eventbus/index.mjs.map +1 -1
- package/dist/executors/index.d.mts +13 -10
- package/dist/executors/index.d.ts +13 -10
- package/dist/executors/index.js.map +1 -1
- package/dist/executors/index.mjs.map +1 -1
- package/dist/index.d.mts +60 -28
- package/dist/index.d.ts +60 -28
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/pipes/index.d.mts +18 -15
- package/dist/pipes/index.d.ts +18 -15
- package/dist/pipes/index.js.map +1 -1
- package/dist/pipes/index.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -1,29 +1,41 @@
|
|
|
1
|
-
type FastListenerExecutor = (listeners: FastListenerMeta[], message:
|
|
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
|
-
|
|
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>]) &
|
|
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?:
|
|
32
|
+
meta?: DeepPartial<FastEventMeta & M & Record<string, any>>;
|
|
21
33
|
};
|
|
22
|
-
}[Exclude<keyof Events, number | symbol>]) &
|
|
23
|
-
type FastEventListener<T extends string = string, P = any, M = any, C = any> = (this: C, message:
|
|
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:
|
|
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
|
*/
|
|
@@ -66,7 +78,7 @@ type FastEventSubscriber = {
|
|
|
66
78
|
listener: FastEventListener<any, any, any>;
|
|
67
79
|
};
|
|
68
80
|
type FastListeners = FastListenerNode;
|
|
69
|
-
type FastEventOptions<Meta = Record<string, any>, Context =
|
|
81
|
+
type FastEventOptions<Meta = Record<string, any>, Context = never> = {
|
|
70
82
|
id: string;
|
|
71
83
|
debug: boolean;
|
|
72
84
|
delimiter: string;
|
|
@@ -76,9 +88,9 @@ type FastEventOptions<Meta = Record<string, any>, Context = any> = {
|
|
|
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:
|
|
80
|
-
onBeforeExecuteListener?: (message:
|
|
81
|
-
onAfterExecuteListener?: (message:
|
|
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:
|
|
99
|
-
off?: (message:
|
|
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?:
|
|
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:
|
|
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;
|
|
@@ -261,7 +273,7 @@ interface FastEventBusNodes {
|
|
|
261
273
|
}
|
|
262
274
|
type FastEventBusNodeIds = keyof FastEventBusNodes;
|
|
263
275
|
|
|
264
|
-
type FastEventScopeOptions<Meta = Record<string, any>, Context =
|
|
276
|
+
type FastEventScopeOptions<Meta = Record<string, any>, Context = never> = {
|
|
265
277
|
meta: FastEventScopeMeta & FastEventMeta & Meta;
|
|
266
278
|
context: Context;
|
|
267
279
|
executor?: FastListenerExecutor;
|
|
@@ -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>;
|
|
@@ -284,6 +297,15 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
284
297
|
get context(): Fallback<Context, typeof this>;
|
|
285
298
|
get options(): FastEventScopeOptions<FinalMeta, Context>;
|
|
286
299
|
bind(emitter: FastEvent<any>, prefix: string, options?: DeepPartial<FastEventScopeOptions<FinalMeta, Context>>): void;
|
|
300
|
+
/**
|
|
301
|
+
* 初始化选项
|
|
302
|
+
*
|
|
303
|
+
* 本方法主要供子类重载
|
|
304
|
+
*
|
|
305
|
+
* @param initial - 可选的初始字典对象
|
|
306
|
+
* @returns 返回传入的初始字典对象
|
|
307
|
+
*/
|
|
308
|
+
_initOptions(initial: Dict | undefined): Dict | undefined;
|
|
287
309
|
/**
|
|
288
310
|
* 获取作用域监听器
|
|
289
311
|
* 当启用作用域时,对原始监听器进行包装,添加作用域前缀处理逻辑
|
|
@@ -325,14 +347,14 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
325
347
|
}, FinalMeta>, options?: FastEventListenerArgs<FinalMeta>): R[];
|
|
326
348
|
emitAsync<R = any>(type: string, payload?: any, options?: FastEventListenerArgs<FinalMeta>): Promise<[R | Error][]>;
|
|
327
349
|
emitAsync<R = any>(type: Types, payload?: Events[Types], options?: FastEventListenerArgs<FinalMeta>): Promise<[R | Error][]>;
|
|
328
|
-
emitAsync<R = any>(message:
|
|
329
|
-
waitFor<T extends Types>(type: T, timeout?: number): Promise<
|
|
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<{
|
|
330
352
|
[key in T]: Events[T];
|
|
331
353
|
}, FinalMeta>>;
|
|
332
|
-
waitFor(type: string, timeout?: number): Promise<
|
|
354
|
+
waitFor(type: string, timeout?: number): Promise<TypedFastEventMessage<{
|
|
333
355
|
[key: string]: any;
|
|
334
356
|
}, FinalMeta>>;
|
|
335
|
-
waitFor<P = any>(type: string, timeout?: number): Promise<
|
|
357
|
+
waitFor<P = any>(type: string, timeout?: number): Promise<TypedFastEventMessage<{
|
|
336
358
|
[key: string]: P;
|
|
337
359
|
}, FinalMeta>>;
|
|
338
360
|
/**
|
|
@@ -373,7 +395,7 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
373
395
|
* 当on/once/onAny未指定监听器时,此为默认监听器
|
|
374
396
|
* @param message
|
|
375
397
|
*/
|
|
376
|
-
onMessage(message:
|
|
398
|
+
onMessage(message: TypedFastEventMessage<Events, FinalMeta>, args: FastEventListenerArgs<FinalMeta>): void;
|
|
377
399
|
}
|
|
378
400
|
|
|
379
401
|
/**
|
|
@@ -401,6 +423,7 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
401
423
|
events: AllEvents;
|
|
402
424
|
meta: Expand<FastEventMeta & Meta & Record<string, any>>;
|
|
403
425
|
context: Expand<Fallback<Context, typeof this>>;
|
|
426
|
+
message: TypedFastEventMessageOptional<AllEvents, Expand<FastEventMeta & Meta & Record<string, any>>>;
|
|
404
427
|
};
|
|
405
428
|
/**
|
|
406
429
|
* 创建FastEvent实例
|
|
@@ -420,6 +443,15 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
420
443
|
get meta(): Meta;
|
|
421
444
|
/** 获取事件发射器的唯一标识符 */
|
|
422
445
|
get id(): string;
|
|
446
|
+
/**
|
|
447
|
+
* 初始化选项
|
|
448
|
+
*
|
|
449
|
+
* 本方法主要供子类重载
|
|
450
|
+
*
|
|
451
|
+
* @param initial - 可选的初始字典对象
|
|
452
|
+
* @returns 返回传入的初始字典对象
|
|
453
|
+
*/
|
|
454
|
+
_initOptions(initial?: Partial<FastEventOptions<Meta, Context>>): Partial<FastEventOptions<Meta, Context>> | undefined;
|
|
423
455
|
/**
|
|
424
456
|
* 添加事件监听器到事件树中
|
|
425
457
|
* @param parts - 事件路径数组
|
|
@@ -528,7 +560,7 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
528
560
|
* 此方法供子类继承
|
|
529
561
|
*
|
|
530
562
|
*/
|
|
531
|
-
onMessage(message:
|
|
563
|
+
onMessage(message: TypedFastEventMessage<AllEvents, Meta>, args: FastEventListenerArgs<Meta>): void;
|
|
532
564
|
off(listener: FastEventListener<any, any, any>): void;
|
|
533
565
|
off(type: string, listener: FastEventListener<any, any, any>): void;
|
|
534
566
|
off(type: Types, listener: FastEventListener<any, any, any>): void;
|
|
@@ -773,11 +805,11 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
773
805
|
* console.log('服务器就绪');
|
|
774
806
|
* ```
|
|
775
807
|
*/
|
|
776
|
-
waitFor<T extends Types>(type: T, timeout?: number): Promise<
|
|
808
|
+
waitFor<T extends Types>(type: T, timeout?: number): Promise<TypedFastEventMessage<{
|
|
777
809
|
[key in T]: AllEvents[T];
|
|
778
810
|
}, Meta>>;
|
|
779
|
-
waitFor(type: string, timeout?: number): Promise<
|
|
780
|
-
waitFor<P = any>(type: string, timeout?: number): Promise<
|
|
811
|
+
waitFor(type: string, timeout?: number): Promise<TypedFastEventMessage<AllEvents, Meta>>;
|
|
812
|
+
waitFor<P = any>(type: string, timeout?: number): Promise<TypedFastEventMessage<{
|
|
781
813
|
[key: string]: P;
|
|
782
814
|
}, Meta>>;
|
|
783
815
|
/**
|
|
@@ -1002,4 +1034,4 @@ declare const FastEventDirectives: {
|
|
|
1002
1034
|
clearRetain: symbol;
|
|
1003
1035
|
};
|
|
1004
1036
|
|
|
1005
|
-
export { AbortError, BroadcastEvent, CancelError, type ChangeFieldType, type DeepPartial, type Dict, type Expand, type Fallback,
|
|
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__ };
|
package/dist/eventbus/index.d.ts
CHANGED
|
@@ -1,29 +1,41 @@
|
|
|
1
|
-
type FastListenerExecutor = (listeners: FastListenerMeta[], message:
|
|
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
|
-
|
|
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>]) &
|
|
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?:
|
|
32
|
+
meta?: DeepPartial<FastEventMeta & M & Record<string, any>>;
|
|
21
33
|
};
|
|
22
|
-
}[Exclude<keyof Events, number | symbol>]) &
|
|
23
|
-
type FastEventListener<T extends string = string, P = any, M = any, C = any> = (this: C, message:
|
|
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:
|
|
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
|
*/
|
|
@@ -66,7 +78,7 @@ type FastEventSubscriber = {
|
|
|
66
78
|
listener: FastEventListener<any, any, any>;
|
|
67
79
|
};
|
|
68
80
|
type FastListeners = FastListenerNode;
|
|
69
|
-
type FastEventOptions<Meta = Record<string, any>, Context =
|
|
81
|
+
type FastEventOptions<Meta = Record<string, any>, Context = never> = {
|
|
70
82
|
id: string;
|
|
71
83
|
debug: boolean;
|
|
72
84
|
delimiter: string;
|
|
@@ -76,9 +88,9 @@ type FastEventOptions<Meta = Record<string, any>, Context = any> = {
|
|
|
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:
|
|
80
|
-
onBeforeExecuteListener?: (message:
|
|
81
|
-
onAfterExecuteListener?: (message:
|
|
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:
|
|
99
|
-
off?: (message:
|
|
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?:
|
|
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:
|
|
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;
|
|
@@ -261,7 +273,7 @@ interface FastEventBusNodes {
|
|
|
261
273
|
}
|
|
262
274
|
type FastEventBusNodeIds = keyof FastEventBusNodes;
|
|
263
275
|
|
|
264
|
-
type FastEventScopeOptions<Meta = Record<string, any>, Context =
|
|
276
|
+
type FastEventScopeOptions<Meta = Record<string, any>, Context = never> = {
|
|
265
277
|
meta: FastEventScopeMeta & FastEventMeta & Meta;
|
|
266
278
|
context: Context;
|
|
267
279
|
executor?: FastListenerExecutor;
|
|
@@ -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>;
|
|
@@ -284,6 +297,15 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
284
297
|
get context(): Fallback<Context, typeof this>;
|
|
285
298
|
get options(): FastEventScopeOptions<FinalMeta, Context>;
|
|
286
299
|
bind(emitter: FastEvent<any>, prefix: string, options?: DeepPartial<FastEventScopeOptions<FinalMeta, Context>>): void;
|
|
300
|
+
/**
|
|
301
|
+
* 初始化选项
|
|
302
|
+
*
|
|
303
|
+
* 本方法主要供子类重载
|
|
304
|
+
*
|
|
305
|
+
* @param initial - 可选的初始字典对象
|
|
306
|
+
* @returns 返回传入的初始字典对象
|
|
307
|
+
*/
|
|
308
|
+
_initOptions(initial: Dict | undefined): Dict | undefined;
|
|
287
309
|
/**
|
|
288
310
|
* 获取作用域监听器
|
|
289
311
|
* 当启用作用域时,对原始监听器进行包装,添加作用域前缀处理逻辑
|
|
@@ -325,14 +347,14 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
325
347
|
}, FinalMeta>, options?: FastEventListenerArgs<FinalMeta>): R[];
|
|
326
348
|
emitAsync<R = any>(type: string, payload?: any, options?: FastEventListenerArgs<FinalMeta>): Promise<[R | Error][]>;
|
|
327
349
|
emitAsync<R = any>(type: Types, payload?: Events[Types], options?: FastEventListenerArgs<FinalMeta>): Promise<[R | Error][]>;
|
|
328
|
-
emitAsync<R = any>(message:
|
|
329
|
-
waitFor<T extends Types>(type: T, timeout?: number): Promise<
|
|
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<{
|
|
330
352
|
[key in T]: Events[T];
|
|
331
353
|
}, FinalMeta>>;
|
|
332
|
-
waitFor(type: string, timeout?: number): Promise<
|
|
354
|
+
waitFor(type: string, timeout?: number): Promise<TypedFastEventMessage<{
|
|
333
355
|
[key: string]: any;
|
|
334
356
|
}, FinalMeta>>;
|
|
335
|
-
waitFor<P = any>(type: string, timeout?: number): Promise<
|
|
357
|
+
waitFor<P = any>(type: string, timeout?: number): Promise<TypedFastEventMessage<{
|
|
336
358
|
[key: string]: P;
|
|
337
359
|
}, FinalMeta>>;
|
|
338
360
|
/**
|
|
@@ -373,7 +395,7 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
373
395
|
* 当on/once/onAny未指定监听器时,此为默认监听器
|
|
374
396
|
* @param message
|
|
375
397
|
*/
|
|
376
|
-
onMessage(message:
|
|
398
|
+
onMessage(message: TypedFastEventMessage<Events, FinalMeta>, args: FastEventListenerArgs<FinalMeta>): void;
|
|
377
399
|
}
|
|
378
400
|
|
|
379
401
|
/**
|
|
@@ -401,6 +423,7 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
401
423
|
events: AllEvents;
|
|
402
424
|
meta: Expand<FastEventMeta & Meta & Record<string, any>>;
|
|
403
425
|
context: Expand<Fallback<Context, typeof this>>;
|
|
426
|
+
message: TypedFastEventMessageOptional<AllEvents, Expand<FastEventMeta & Meta & Record<string, any>>>;
|
|
404
427
|
};
|
|
405
428
|
/**
|
|
406
429
|
* 创建FastEvent实例
|
|
@@ -420,6 +443,15 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
420
443
|
get meta(): Meta;
|
|
421
444
|
/** 获取事件发射器的唯一标识符 */
|
|
422
445
|
get id(): string;
|
|
446
|
+
/**
|
|
447
|
+
* 初始化选项
|
|
448
|
+
*
|
|
449
|
+
* 本方法主要供子类重载
|
|
450
|
+
*
|
|
451
|
+
* @param initial - 可选的初始字典对象
|
|
452
|
+
* @returns 返回传入的初始字典对象
|
|
453
|
+
*/
|
|
454
|
+
_initOptions(initial?: Partial<FastEventOptions<Meta, Context>>): Partial<FastEventOptions<Meta, Context>> | undefined;
|
|
423
455
|
/**
|
|
424
456
|
* 添加事件监听器到事件树中
|
|
425
457
|
* @param parts - 事件路径数组
|
|
@@ -528,7 +560,7 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
528
560
|
* 此方法供子类继承
|
|
529
561
|
*
|
|
530
562
|
*/
|
|
531
|
-
onMessage(message:
|
|
563
|
+
onMessage(message: TypedFastEventMessage<AllEvents, Meta>, args: FastEventListenerArgs<Meta>): void;
|
|
532
564
|
off(listener: FastEventListener<any, any, any>): void;
|
|
533
565
|
off(type: string, listener: FastEventListener<any, any, any>): void;
|
|
534
566
|
off(type: Types, listener: FastEventListener<any, any, any>): void;
|
|
@@ -773,11 +805,11 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
773
805
|
* console.log('服务器就绪');
|
|
774
806
|
* ```
|
|
775
807
|
*/
|
|
776
|
-
waitFor<T extends Types>(type: T, timeout?: number): Promise<
|
|
808
|
+
waitFor<T extends Types>(type: T, timeout?: number): Promise<TypedFastEventMessage<{
|
|
777
809
|
[key in T]: AllEvents[T];
|
|
778
810
|
}, Meta>>;
|
|
779
|
-
waitFor(type: string, timeout?: number): Promise<
|
|
780
|
-
waitFor<P = any>(type: string, timeout?: number): Promise<
|
|
811
|
+
waitFor(type: string, timeout?: number): Promise<TypedFastEventMessage<AllEvents, Meta>>;
|
|
812
|
+
waitFor<P = any>(type: string, timeout?: number): Promise<TypedFastEventMessage<{
|
|
781
813
|
[key: string]: P;
|
|
782
814
|
}, Meta>>;
|
|
783
815
|
/**
|
|
@@ -1002,4 +1034,4 @@ declare const FastEventDirectives: {
|
|
|
1002
1034
|
clearRetain: symbol;
|
|
1003
1035
|
};
|
|
1004
1036
|
|
|
1005
|
-
export { AbortError, BroadcastEvent, CancelError, type ChangeFieldType, type DeepPartial, type Dict, type Expand, type Fallback,
|
|
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__ };
|
package/dist/eventbus/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
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,j=class j extends m{};a(j,"UnboundError");var y=j,O=class O extends m{};a(O,"AbortError");var g=O,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});u(this,"prefix","");u(this,"emitter");this._options=Object.assign({},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);}_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});this._options=Object.assign({debug:false,id:Math.random().toString(36).substring(2),delimiter:"/",context:null,ignoreErrors:true,meta:void 0,expandEmitResults:true},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}_addListener(e,t,s){let{count:i,prepend:r}=s,o=0;return [this._forEachNodes(e,h=>{let f=[t,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(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}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
|
|
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
|