fastevent 2.1.4 → 2.1.5
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 +48 -40
- package/dist/eventbus/index.d.ts +48 -40
- 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 +4 -4
- package/dist/executors/index.d.ts +4 -4
- package/dist/executors/index.js.map +1 -1
- package/dist/executors/index.mjs.map +1 -1
- package/dist/index.d.mts +48 -40
- package/dist/index.d.ts +48 -40
- 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 +5 -5
- package/dist/pipes/index.d.ts +5 -5
- package/dist/pipes/index.js.map +1 -1
- package/dist/pipes/index.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -1,45 +1,49 @@
|
|
|
1
|
-
type FastListenerExecutor = (listeners: FastListenerMeta[], message: TypedFastEventMessage, args: FastEventListenerArgs, execute: (listener:
|
|
1
|
+
type FastListenerExecutor = (listeners: FastListenerMeta[], message: TypedFastEventMessage, args: FastEventListenerArgs, execute: (listener: TypedFastEventListener, message: TypedFastEventMessage, args: FastEventListenerArgs, catchErrors?: boolean) => Promise<any> | any) => Promise<any[]> | any[];
|
|
2
2
|
|
|
3
|
-
type FastListenerPipe = (listener:
|
|
3
|
+
type FastListenerPipe = (listener: TypedFastEventListener) => TypedFastEventListener;
|
|
4
4
|
|
|
5
5
|
interface FastEventMeta {
|
|
6
6
|
}
|
|
7
7
|
interface FastEventMessageExtends {
|
|
8
8
|
}
|
|
9
|
-
type FastEventMessage = {
|
|
10
|
-
type:
|
|
11
|
-
payload:
|
|
12
|
-
meta?:
|
|
13
|
-
};
|
|
9
|
+
type FastEventMessage<P = any, M extends Record<string, any> = Record<string, any>, T extends string = string> = {
|
|
10
|
+
type: T;
|
|
11
|
+
payload: P;
|
|
12
|
+
meta?: M & Partial<FastEventMeta>;
|
|
13
|
+
} & FastEventMessageExtends;
|
|
14
14
|
type TypedFastEventMessage<Events extends Record<string, any> = Record<string, any>, M = any> = ({
|
|
15
15
|
[K in keyof Events]: {
|
|
16
16
|
type: Exclude<K, number | symbol>;
|
|
17
17
|
payload: Events[K];
|
|
18
18
|
meta: FastEventMeta & M & Record<string, any>;
|
|
19
19
|
};
|
|
20
|
-
}[Exclude<keyof Events, number | symbol>]) &
|
|
20
|
+
}[Exclude<keyof Events, number | symbol>]) & FastEventMessageExtends;
|
|
21
21
|
type TypedFastEventMessageOptional<Events extends Record<string, any> = Record<string, any>, M = any> = ({
|
|
22
22
|
[K in keyof Events]: {
|
|
23
23
|
type: Exclude<K, number | symbol>;
|
|
24
24
|
payload: Events[K];
|
|
25
25
|
meta?: DeepPartial<FastEventMeta & M & Record<string, any>>;
|
|
26
26
|
};
|
|
27
|
-
}[Exclude<keyof Events, number | symbol>]) &
|
|
27
|
+
}[Exclude<keyof Events, number | symbol>]) & FastEventMessageExtends;
|
|
28
28
|
type FastEventEmitMessage<Events extends Record<string, any> = Record<string, any>, M = any> = ({
|
|
29
29
|
[K in keyof Events]: {
|
|
30
30
|
type: Exclude<K, number | symbol>;
|
|
31
31
|
payload?: Events[K];
|
|
32
32
|
meta?: DeepPartial<FastEventMeta & M & Record<string, any>>;
|
|
33
33
|
};
|
|
34
|
-
}[Exclude<keyof Events, number | symbol>]) &
|
|
35
|
-
type
|
|
34
|
+
}[Exclude<keyof Events, number | symbol>]) & FastEventMessageExtends;
|
|
35
|
+
type TypedFastEventListener<T extends string = string, P = any, M = any, C = any> = (this: C, message: TypedFastEventMessage<{
|
|
36
36
|
[K in T]: P;
|
|
37
37
|
}, M>, args: FastEventListenerArgs<M>) => any | Promise<any>;
|
|
38
|
-
type
|
|
38
|
+
type TypedFastEventAnyListener<Events extends Record<string, any> = Record<string, any>, Meta = never, Context = any> = (this: Context, message: TypedFastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => any | Promise<any>;
|
|
39
|
+
type FastEventListeners<Events extends Record<string, any> = Record<string, any>, M = any, C = any> = ({
|
|
40
|
+
[K in keyof Events]: TypedFastEventListener<Exclude<K, number | symbol>, Events[K], M, C>;
|
|
41
|
+
});
|
|
42
|
+
type FastEventListener<P = any, M extends Record<string, any> = Record<string, any>, T extends string = string> = (message: FastEventMessage<P, M, T>, args: FastEventListenerArgs<M>) => any | Promise<any>;
|
|
39
43
|
/**
|
|
40
44
|
* [监听器函数引用,需要执行多少次,实际执行的次数(用于负载均衡时记录)]
|
|
41
45
|
*/
|
|
42
|
-
type FastListenerMeta = [
|
|
46
|
+
type FastListenerMeta = [TypedFastEventListener<any, any>, number, number];
|
|
43
47
|
type FastListenerNode = {
|
|
44
48
|
__listeners: FastListenerMeta[];
|
|
45
49
|
} & {
|
|
@@ -75,7 +79,7 @@ type FastEventSubscriber = {
|
|
|
75
79
|
* scope.off(subscriber.listener) // 生效
|
|
76
80
|
*
|
|
77
81
|
*/
|
|
78
|
-
listener:
|
|
82
|
+
listener: TypedFastEventListener<any, any, any>;
|
|
79
83
|
};
|
|
80
84
|
type FastListeners = FastListenerNode;
|
|
81
85
|
type FastEventOptions<Meta = Record<string, any>, Context = never> = {
|
|
@@ -85,17 +89,17 @@ type FastEventOptions<Meta = Record<string, any>, Context = never> = {
|
|
|
85
89
|
context: Context;
|
|
86
90
|
ignoreErrors: boolean;
|
|
87
91
|
meta: Meta;
|
|
88
|
-
onAddListener?: (type: string, listener:
|
|
89
|
-
onRemoveListener?: (type: string, listener:
|
|
92
|
+
onAddListener?: (type: string, listener: TypedFastEventListener, options: FastEventListenOptions<Record<string, any>, Meta>) => boolean | FastEventSubscriber | void;
|
|
93
|
+
onRemoveListener?: (type: string, listener: TypedFastEventListener) => void;
|
|
90
94
|
onClearListeners?: () => void;
|
|
91
|
-
onListenerError?: ((error: Error, listener:
|
|
95
|
+
onListenerError?: ((error: Error, listener: TypedFastEventListener, message: TypedFastEventMessage<any, Meta>, args: FastEventListenerArgs<Meta> | undefined) => void);
|
|
92
96
|
onBeforeExecuteListener?: (message: TypedFastEventMessage<any, Meta>, args: FastEventListenerArgs<Meta>) => boolean | void | any[];
|
|
93
97
|
onAfterExecuteListener?: (message: TypedFastEventMessage<any, Meta>, returns: any[], listeners: FastListenerNode[]) => void;
|
|
94
98
|
/**
|
|
95
99
|
* 全局执行器
|
|
96
100
|
*/
|
|
97
101
|
executor?: FastListenerExecutor;
|
|
98
|
-
onMessage?:
|
|
102
|
+
onMessage?: TypedFastEventListener;
|
|
99
103
|
expandEmitResults?: boolean;
|
|
100
104
|
};
|
|
101
105
|
interface FastEvents {
|
|
@@ -277,11 +281,11 @@ type FastEventScopeOptions<Meta = Record<string, any>, Context = never> = {
|
|
|
277
281
|
meta: FastEventScopeMeta & FastEventMeta & Meta;
|
|
278
282
|
context: Context;
|
|
279
283
|
executor?: FastListenerExecutor;
|
|
280
|
-
onMessage?:
|
|
284
|
+
onMessage?: TypedFastEventListener;
|
|
281
285
|
};
|
|
282
|
-
|
|
286
|
+
interface FastEventScopeMeta {
|
|
283
287
|
scope: string;
|
|
284
|
-
}
|
|
288
|
+
}
|
|
285
289
|
declare class FastEventScope<Events extends Record<string, any> = Record<string, any>, Meta extends Record<string, any> = Record<string, any>, Context = never, Types extends keyof Events = keyof Events, FinalMeta extends Record<string, any> = FastEventMeta & FastEventScopeMeta & Meta> {
|
|
286
290
|
__FastEventScope__: boolean;
|
|
287
291
|
private _options;
|
|
@@ -290,6 +294,8 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
290
294
|
meta: FinalMeta;
|
|
291
295
|
context: Fallback<Context, typeof this>;
|
|
292
296
|
message: TypedFastEventMessageOptional<Events, FinalMeta>;
|
|
297
|
+
listeners: FastEventListeners<Events, FinalMeta>;
|
|
298
|
+
anyListener: TypedFastEventAnyListener<Events, FinalMeta, Fallback<Context, typeof this>>;
|
|
293
299
|
};
|
|
294
300
|
prefix: string;
|
|
295
301
|
emitter: FastEvent<Events>;
|
|
@@ -319,20 +325,20 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
319
325
|
on<T extends Types = Types>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
320
326
|
on<T extends string>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
321
327
|
on(type: '**', options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
322
|
-
on<T extends Types = Types>(type: T, listener:
|
|
323
|
-
on<T extends string>(type: T, listener:
|
|
324
|
-
on(type: '**', listener:
|
|
328
|
+
on<T extends Types = Types>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
329
|
+
on<T extends string>(type: T, listener: TypedFastEventListener<string, any, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
330
|
+
on(type: '**', listener: TypedFastEventAnyListener<Events, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
325
331
|
once<T extends Types = Types>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
326
332
|
once<T extends string>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
327
|
-
once<T extends Types = Types>(type: T, listener:
|
|
328
|
-
once<T extends string>(type: T, listener:
|
|
333
|
+
once<T extends Types = Types>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
334
|
+
once<T extends string>(type: T, listener: TypedFastEventListener<string, any, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
329
335
|
onAny(options?: Pick<FastEventListenOptions, 'prepend'>): FastEventSubscriber;
|
|
330
|
-
onAny<P = any>(listener:
|
|
336
|
+
onAny<P = any>(listener: TypedFastEventAnyListener<{
|
|
331
337
|
[K: string]: P;
|
|
332
338
|
}, FinalMeta, Fallback<Context, typeof this>>, options?: Pick<FastEventListenOptions, 'prepend'>): FastEventSubscriber;
|
|
333
|
-
off(listener:
|
|
334
|
-
off(type: string, listener:
|
|
335
|
-
off(type: Types, listener:
|
|
339
|
+
off(listener: TypedFastEventListener<any, any, any>): void;
|
|
340
|
+
off(type: string, listener: TypedFastEventListener<any, any, any>): void;
|
|
341
|
+
off(type: Types, listener: TypedFastEventListener<any, any, any>): void;
|
|
336
342
|
off(type: string): void;
|
|
337
343
|
off(type: Types): void;
|
|
338
344
|
offAll(): void;
|
|
@@ -424,6 +430,8 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
424
430
|
meta: Expand<FastEventMeta & Meta & Record<string, any>>;
|
|
425
431
|
context: Expand<Fallback<Context, typeof this>>;
|
|
426
432
|
message: TypedFastEventMessageOptional<AllEvents, Expand<FastEventMeta & Meta & Record<string, any>>>;
|
|
433
|
+
listeners: FastEventListeners<AllEvents, Expand<FastEventMeta & Meta & Record<string, any>>>;
|
|
434
|
+
anyListener: TypedFastEventAnyListener<AllEvents, Expand<FastEventMeta & Meta & Record<string, any>>, Expand<Fallback<Context, typeof this>>>;
|
|
427
435
|
};
|
|
428
436
|
/**
|
|
429
437
|
* 创建FastEvent实例
|
|
@@ -509,9 +517,9 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
509
517
|
on<T extends Types = Types>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
510
518
|
on<T extends string>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
511
519
|
on(type: '**', options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
512
|
-
on<T extends Types = Types>(type: T, listener:
|
|
513
|
-
on<T extends string>(type: T, listener:
|
|
514
|
-
on(type: '**', listener:
|
|
520
|
+
on<T extends Types = Types>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, AllEvents[T], Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
521
|
+
on<T extends string>(type: T, listener: TypedFastEventAnyListener<AllEvents, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
522
|
+
on(type: '**', listener: TypedFastEventAnyListener<Record<string, any>, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
515
523
|
/**
|
|
516
524
|
* 注册一次性事件监听器
|
|
517
525
|
* @param type - 事件类型,支持与on方法相同的格式:
|
|
@@ -535,8 +543,8 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
535
543
|
*/
|
|
536
544
|
once<T extends Types = Types>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
537
545
|
once<T extends string>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
538
|
-
once<T extends Types = Types>(type: T, listener:
|
|
539
|
-
once<T extends string>(type: T, listener:
|
|
546
|
+
once<T extends Types = Types>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, AllEvents[T], Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
547
|
+
once<T extends string>(type: T, listener: TypedFastEventAnyListener<AllEvents, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
540
548
|
/**
|
|
541
549
|
* 注册一个监听器,用于监听所有事件
|
|
542
550
|
* @param listener 事件监听器函数,可以接收任意类型的事件数据
|
|
@@ -552,7 +560,7 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
552
560
|
* ```listener: FastEventAnyListener<AllEvents, Meta, Fallback<Context, typeof this>>): FastEventSubscriber
|
|
553
561
|
*/
|
|
554
562
|
onAny(options?: Omit<FastEventListenOptions<AllEvents, Meta>, 'count'>): FastEventSubscriber;
|
|
555
|
-
onAny<P = any>(listener:
|
|
563
|
+
onAny<P = any>(listener: TypedFastEventAnyListener<Record<string, P>, Meta, Fallback<Context, typeof this>>, options?: Omit<FastEventListenOptions<AllEvents, Meta>, 'count'>): FastEventSubscriber;
|
|
556
564
|
/**
|
|
557
565
|
*
|
|
558
566
|
* 当调用on/once/onAny时如果没有指定监听器,则调用此方法
|
|
@@ -561,9 +569,9 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
561
569
|
*
|
|
562
570
|
*/
|
|
563
571
|
onMessage(message: TypedFastEventMessage<AllEvents, Meta>, args: FastEventListenerArgs<Meta>): void;
|
|
564
|
-
off(listener:
|
|
565
|
-
off(type: string, listener:
|
|
566
|
-
off(type: Types, listener:
|
|
572
|
+
off(listener: TypedFastEventListener<any, any, any>): void;
|
|
573
|
+
off(type: string, listener: TypedFastEventListener<any, any, any>): void;
|
|
574
|
+
off(type: Types, listener: TypedFastEventListener<any, any, any>): void;
|
|
567
575
|
off(type: string): void;
|
|
568
576
|
off(type: Types): void;
|
|
569
577
|
/**
|
|
@@ -1034,4 +1042,4 @@ declare const FastEventDirectives: {
|
|
|
1034
1042
|
clearRetain: symbol;
|
|
1035
1043
|
};
|
|
1036
1044
|
|
|
1037
|
-
export { AbortError, BroadcastEvent, CancelError, type ChangeFieldType, type DeepPartial, type Dict, type Expand, type Fallback, FastEvent,
|
|
1045
|
+
export { AbortError, BroadcastEvent, CancelError, type ChangeFieldType, type DeepPartial, type Dict, type Expand, type Fallback, FastEvent, FastEventBus, type FastEventBusEventTypes, type FastEventBusEvents, type FastEventBusMessage, FastEventBusNode, type FastEventBusNodeIds, type FastEventBusNodeOptions, type FastEventBusNodes, type FastEventBusOptions, FastEventDirectives, type FastEventEmitMessage, FastEventError, type FastEventListenOptions, type FastEventListener, type FastEventListenerArgs, type FastEventListeners, type FastEventMessage, type FastEventMessageExtends, type FastEventMeta, type FastEventOptions, FastEventScope, type FastEventScopeMeta, type FastEventScopeOptions, type FastEventSubscriber, type FastEvents, type FastListenerMeta, type FastListenerNode, type FastListeners, type Merge, NamespaceDelimiter, NodeDataEvent, type ObjectKeys, type OptionalItems, type Overloads, type OverrideOptions, type PickScopeEvents, QueueOverflowError, type RequiredItems, type ScopeEvents, TimeoutError, type TypedFastEventAnyListener, type TypedFastEventListener, type TypedFastEventMessage, type TypedFastEventMessageOptional, UnboundError, type Unique, __FastEventScope__, __FastEvent__ };
|
package/dist/eventbus/index.d.ts
CHANGED
|
@@ -1,45 +1,49 @@
|
|
|
1
|
-
type FastListenerExecutor = (listeners: FastListenerMeta[], message: TypedFastEventMessage, args: FastEventListenerArgs, execute: (listener:
|
|
1
|
+
type FastListenerExecutor = (listeners: FastListenerMeta[], message: TypedFastEventMessage, args: FastEventListenerArgs, execute: (listener: TypedFastEventListener, message: TypedFastEventMessage, args: FastEventListenerArgs, catchErrors?: boolean) => Promise<any> | any) => Promise<any[]> | any[];
|
|
2
2
|
|
|
3
|
-
type FastListenerPipe = (listener:
|
|
3
|
+
type FastListenerPipe = (listener: TypedFastEventListener) => TypedFastEventListener;
|
|
4
4
|
|
|
5
5
|
interface FastEventMeta {
|
|
6
6
|
}
|
|
7
7
|
interface FastEventMessageExtends {
|
|
8
8
|
}
|
|
9
|
-
type FastEventMessage = {
|
|
10
|
-
type:
|
|
11
|
-
payload:
|
|
12
|
-
meta?:
|
|
13
|
-
};
|
|
9
|
+
type FastEventMessage<P = any, M extends Record<string, any> = Record<string, any>, T extends string = string> = {
|
|
10
|
+
type: T;
|
|
11
|
+
payload: P;
|
|
12
|
+
meta?: M & Partial<FastEventMeta>;
|
|
13
|
+
} & FastEventMessageExtends;
|
|
14
14
|
type TypedFastEventMessage<Events extends Record<string, any> = Record<string, any>, M = any> = ({
|
|
15
15
|
[K in keyof Events]: {
|
|
16
16
|
type: Exclude<K, number | symbol>;
|
|
17
17
|
payload: Events[K];
|
|
18
18
|
meta: FastEventMeta & M & Record<string, any>;
|
|
19
19
|
};
|
|
20
|
-
}[Exclude<keyof Events, number | symbol>]) &
|
|
20
|
+
}[Exclude<keyof Events, number | symbol>]) & FastEventMessageExtends;
|
|
21
21
|
type TypedFastEventMessageOptional<Events extends Record<string, any> = Record<string, any>, M = any> = ({
|
|
22
22
|
[K in keyof Events]: {
|
|
23
23
|
type: Exclude<K, number | symbol>;
|
|
24
24
|
payload: Events[K];
|
|
25
25
|
meta?: DeepPartial<FastEventMeta & M & Record<string, any>>;
|
|
26
26
|
};
|
|
27
|
-
}[Exclude<keyof Events, number | symbol>]) &
|
|
27
|
+
}[Exclude<keyof Events, number | symbol>]) & FastEventMessageExtends;
|
|
28
28
|
type FastEventEmitMessage<Events extends Record<string, any> = Record<string, any>, M = any> = ({
|
|
29
29
|
[K in keyof Events]: {
|
|
30
30
|
type: Exclude<K, number | symbol>;
|
|
31
31
|
payload?: Events[K];
|
|
32
32
|
meta?: DeepPartial<FastEventMeta & M & Record<string, any>>;
|
|
33
33
|
};
|
|
34
|
-
}[Exclude<keyof Events, number | symbol>]) &
|
|
35
|
-
type
|
|
34
|
+
}[Exclude<keyof Events, number | symbol>]) & FastEventMessageExtends;
|
|
35
|
+
type TypedFastEventListener<T extends string = string, P = any, M = any, C = any> = (this: C, message: TypedFastEventMessage<{
|
|
36
36
|
[K in T]: P;
|
|
37
37
|
}, M>, args: FastEventListenerArgs<M>) => any | Promise<any>;
|
|
38
|
-
type
|
|
38
|
+
type TypedFastEventAnyListener<Events extends Record<string, any> = Record<string, any>, Meta = never, Context = any> = (this: Context, message: TypedFastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => any | Promise<any>;
|
|
39
|
+
type FastEventListeners<Events extends Record<string, any> = Record<string, any>, M = any, C = any> = ({
|
|
40
|
+
[K in keyof Events]: TypedFastEventListener<Exclude<K, number | symbol>, Events[K], M, C>;
|
|
41
|
+
});
|
|
42
|
+
type FastEventListener<P = any, M extends Record<string, any> = Record<string, any>, T extends string = string> = (message: FastEventMessage<P, M, T>, args: FastEventListenerArgs<M>) => any | Promise<any>;
|
|
39
43
|
/**
|
|
40
44
|
* [监听器函数引用,需要执行多少次,实际执行的次数(用于负载均衡时记录)]
|
|
41
45
|
*/
|
|
42
|
-
type FastListenerMeta = [
|
|
46
|
+
type FastListenerMeta = [TypedFastEventListener<any, any>, number, number];
|
|
43
47
|
type FastListenerNode = {
|
|
44
48
|
__listeners: FastListenerMeta[];
|
|
45
49
|
} & {
|
|
@@ -75,7 +79,7 @@ type FastEventSubscriber = {
|
|
|
75
79
|
* scope.off(subscriber.listener) // 生效
|
|
76
80
|
*
|
|
77
81
|
*/
|
|
78
|
-
listener:
|
|
82
|
+
listener: TypedFastEventListener<any, any, any>;
|
|
79
83
|
};
|
|
80
84
|
type FastListeners = FastListenerNode;
|
|
81
85
|
type FastEventOptions<Meta = Record<string, any>, Context = never> = {
|
|
@@ -85,17 +89,17 @@ type FastEventOptions<Meta = Record<string, any>, Context = never> = {
|
|
|
85
89
|
context: Context;
|
|
86
90
|
ignoreErrors: boolean;
|
|
87
91
|
meta: Meta;
|
|
88
|
-
onAddListener?: (type: string, listener:
|
|
89
|
-
onRemoveListener?: (type: string, listener:
|
|
92
|
+
onAddListener?: (type: string, listener: TypedFastEventListener, options: FastEventListenOptions<Record<string, any>, Meta>) => boolean | FastEventSubscriber | void;
|
|
93
|
+
onRemoveListener?: (type: string, listener: TypedFastEventListener) => void;
|
|
90
94
|
onClearListeners?: () => void;
|
|
91
|
-
onListenerError?: ((error: Error, listener:
|
|
95
|
+
onListenerError?: ((error: Error, listener: TypedFastEventListener, message: TypedFastEventMessage<any, Meta>, args: FastEventListenerArgs<Meta> | undefined) => void);
|
|
92
96
|
onBeforeExecuteListener?: (message: TypedFastEventMessage<any, Meta>, args: FastEventListenerArgs<Meta>) => boolean | void | any[];
|
|
93
97
|
onAfterExecuteListener?: (message: TypedFastEventMessage<any, Meta>, returns: any[], listeners: FastListenerNode[]) => void;
|
|
94
98
|
/**
|
|
95
99
|
* 全局执行器
|
|
96
100
|
*/
|
|
97
101
|
executor?: FastListenerExecutor;
|
|
98
|
-
onMessage?:
|
|
102
|
+
onMessage?: TypedFastEventListener;
|
|
99
103
|
expandEmitResults?: boolean;
|
|
100
104
|
};
|
|
101
105
|
interface FastEvents {
|
|
@@ -277,11 +281,11 @@ type FastEventScopeOptions<Meta = Record<string, any>, Context = never> = {
|
|
|
277
281
|
meta: FastEventScopeMeta & FastEventMeta & Meta;
|
|
278
282
|
context: Context;
|
|
279
283
|
executor?: FastListenerExecutor;
|
|
280
|
-
onMessage?:
|
|
284
|
+
onMessage?: TypedFastEventListener;
|
|
281
285
|
};
|
|
282
|
-
|
|
286
|
+
interface FastEventScopeMeta {
|
|
283
287
|
scope: string;
|
|
284
|
-
}
|
|
288
|
+
}
|
|
285
289
|
declare class FastEventScope<Events extends Record<string, any> = Record<string, any>, Meta extends Record<string, any> = Record<string, any>, Context = never, Types extends keyof Events = keyof Events, FinalMeta extends Record<string, any> = FastEventMeta & FastEventScopeMeta & Meta> {
|
|
286
290
|
__FastEventScope__: boolean;
|
|
287
291
|
private _options;
|
|
@@ -290,6 +294,8 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
290
294
|
meta: FinalMeta;
|
|
291
295
|
context: Fallback<Context, typeof this>;
|
|
292
296
|
message: TypedFastEventMessageOptional<Events, FinalMeta>;
|
|
297
|
+
listeners: FastEventListeners<Events, FinalMeta>;
|
|
298
|
+
anyListener: TypedFastEventAnyListener<Events, FinalMeta, Fallback<Context, typeof this>>;
|
|
293
299
|
};
|
|
294
300
|
prefix: string;
|
|
295
301
|
emitter: FastEvent<Events>;
|
|
@@ -319,20 +325,20 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
319
325
|
on<T extends Types = Types>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
320
326
|
on<T extends string>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
321
327
|
on(type: '**', options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
322
|
-
on<T extends Types = Types>(type: T, listener:
|
|
323
|
-
on<T extends string>(type: T, listener:
|
|
324
|
-
on(type: '**', listener:
|
|
328
|
+
on<T extends Types = Types>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
329
|
+
on<T extends string>(type: T, listener: TypedFastEventListener<string, any, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
330
|
+
on(type: '**', listener: TypedFastEventAnyListener<Events, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
325
331
|
once<T extends Types = Types>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
326
332
|
once<T extends string>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
327
|
-
once<T extends Types = Types>(type: T, listener:
|
|
328
|
-
once<T extends string>(type: T, listener:
|
|
333
|
+
once<T extends Types = Types>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
334
|
+
once<T extends string>(type: T, listener: TypedFastEventListener<string, any, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
329
335
|
onAny(options?: Pick<FastEventListenOptions, 'prepend'>): FastEventSubscriber;
|
|
330
|
-
onAny<P = any>(listener:
|
|
336
|
+
onAny<P = any>(listener: TypedFastEventAnyListener<{
|
|
331
337
|
[K: string]: P;
|
|
332
338
|
}, FinalMeta, Fallback<Context, typeof this>>, options?: Pick<FastEventListenOptions, 'prepend'>): FastEventSubscriber;
|
|
333
|
-
off(listener:
|
|
334
|
-
off(type: string, listener:
|
|
335
|
-
off(type: Types, listener:
|
|
339
|
+
off(listener: TypedFastEventListener<any, any, any>): void;
|
|
340
|
+
off(type: string, listener: TypedFastEventListener<any, any, any>): void;
|
|
341
|
+
off(type: Types, listener: TypedFastEventListener<any, any, any>): void;
|
|
336
342
|
off(type: string): void;
|
|
337
343
|
off(type: Types): void;
|
|
338
344
|
offAll(): void;
|
|
@@ -424,6 +430,8 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
424
430
|
meta: Expand<FastEventMeta & Meta & Record<string, any>>;
|
|
425
431
|
context: Expand<Fallback<Context, typeof this>>;
|
|
426
432
|
message: TypedFastEventMessageOptional<AllEvents, Expand<FastEventMeta & Meta & Record<string, any>>>;
|
|
433
|
+
listeners: FastEventListeners<AllEvents, Expand<FastEventMeta & Meta & Record<string, any>>>;
|
|
434
|
+
anyListener: TypedFastEventAnyListener<AllEvents, Expand<FastEventMeta & Meta & Record<string, any>>, Expand<Fallback<Context, typeof this>>>;
|
|
427
435
|
};
|
|
428
436
|
/**
|
|
429
437
|
* 创建FastEvent实例
|
|
@@ -509,9 +517,9 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
509
517
|
on<T extends Types = Types>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
510
518
|
on<T extends string>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
511
519
|
on(type: '**', options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
512
|
-
on<T extends Types = Types>(type: T, listener:
|
|
513
|
-
on<T extends string>(type: T, listener:
|
|
514
|
-
on(type: '**', listener:
|
|
520
|
+
on<T extends Types = Types>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, AllEvents[T], Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
521
|
+
on<T extends string>(type: T, listener: TypedFastEventAnyListener<AllEvents, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
522
|
+
on(type: '**', listener: TypedFastEventAnyListener<Record<string, any>, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
515
523
|
/**
|
|
516
524
|
* 注册一次性事件监听器
|
|
517
525
|
* @param type - 事件类型,支持与on方法相同的格式:
|
|
@@ -535,8 +543,8 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
535
543
|
*/
|
|
536
544
|
once<T extends Types = Types>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
537
545
|
once<T extends string>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
538
|
-
once<T extends Types = Types>(type: T, listener:
|
|
539
|
-
once<T extends string>(type: T, listener:
|
|
546
|
+
once<T extends Types = Types>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, AllEvents[T], Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
547
|
+
once<T extends string>(type: T, listener: TypedFastEventAnyListener<AllEvents, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
540
548
|
/**
|
|
541
549
|
* 注册一个监听器,用于监听所有事件
|
|
542
550
|
* @param listener 事件监听器函数,可以接收任意类型的事件数据
|
|
@@ -552,7 +560,7 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
552
560
|
* ```listener: FastEventAnyListener<AllEvents, Meta, Fallback<Context, typeof this>>): FastEventSubscriber
|
|
553
561
|
*/
|
|
554
562
|
onAny(options?: Omit<FastEventListenOptions<AllEvents, Meta>, 'count'>): FastEventSubscriber;
|
|
555
|
-
onAny<P = any>(listener:
|
|
563
|
+
onAny<P = any>(listener: TypedFastEventAnyListener<Record<string, P>, Meta, Fallback<Context, typeof this>>, options?: Omit<FastEventListenOptions<AllEvents, Meta>, 'count'>): FastEventSubscriber;
|
|
556
564
|
/**
|
|
557
565
|
*
|
|
558
566
|
* 当调用on/once/onAny时如果没有指定监听器,则调用此方法
|
|
@@ -561,9 +569,9 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
561
569
|
*
|
|
562
570
|
*/
|
|
563
571
|
onMessage(message: TypedFastEventMessage<AllEvents, Meta>, args: FastEventListenerArgs<Meta>): void;
|
|
564
|
-
off(listener:
|
|
565
|
-
off(type: string, listener:
|
|
566
|
-
off(type: Types, listener:
|
|
572
|
+
off(listener: TypedFastEventListener<any, any, any>): void;
|
|
573
|
+
off(type: string, listener: TypedFastEventListener<any, any, any>): void;
|
|
574
|
+
off(type: Types, listener: TypedFastEventListener<any, any, any>): void;
|
|
567
575
|
off(type: string): void;
|
|
568
576
|
off(type: Types): void;
|
|
569
577
|
/**
|
|
@@ -1034,4 +1042,4 @@ declare const FastEventDirectives: {
|
|
|
1034
1042
|
clearRetain: symbol;
|
|
1035
1043
|
};
|
|
1036
1044
|
|
|
1037
|
-
export { AbortError, BroadcastEvent, CancelError, type ChangeFieldType, type DeepPartial, type Dict, type Expand, type Fallback, FastEvent,
|
|
1045
|
+
export { AbortError, BroadcastEvent, CancelError, type ChangeFieldType, type DeepPartial, type Dict, type Expand, type Fallback, FastEvent, FastEventBus, type FastEventBusEventTypes, type FastEventBusEvents, type FastEventBusMessage, FastEventBusNode, type FastEventBusNodeIds, type FastEventBusNodeOptions, type FastEventBusNodes, type FastEventBusOptions, FastEventDirectives, type FastEventEmitMessage, FastEventError, type FastEventListenOptions, type FastEventListener, type FastEventListenerArgs, type FastEventListeners, type FastEventMessage, type FastEventMessageExtends, type FastEventMeta, type FastEventOptions, FastEventScope, type FastEventScopeMeta, type FastEventScopeOptions, type FastEventSubscriber, type FastEvents, type FastListenerMeta, type FastListenerNode, type FastListeners, type Merge, NamespaceDelimiter, NodeDataEvent, type ObjectKeys, type OptionalItems, type Overloads, type OverrideOptions, type PickScopeEvents, QueueOverflowError, type RequiredItems, type ScopeEvents, TimeoutError, type TypedFastEventAnyListener, type TypedFastEventListener, type TypedFastEventMessage, type TypedFastEventMessageOptional, UnboundError, type Unique, __FastEventScope__, __FastEvent__ };
|
package/dist/eventbus/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
'use strict';var B=Object.defineProperty;var Q=(n,t,e)=>t in n?B(n,t,{enumerable:true,configurable:true,writable:true,value:e}):n[t]=e;var a=(n,t)=>B(n,"name",{value:t,configurable:true});var u=(n,t,e)=>Q(n,typeof t!="symbol"?t+"":t,e);var et=Symbol.for("__FastEvent__"),st=Symbol.for("__FastEventScope__"),$=class $ extends Error{constructor(t){super(t);}};a($,"FastEventError");var m=$,O=class O extends m{};a(O,"TimeoutError");var W=O,T=class T extends m{};a(T,"UnboundError");var y=T,j=class j extends m{};a(j,"AbortError");var g=j,F=class F extends m{};a(F,"CancelError");var v=F,R=class R extends m{};a(R,"QueueOverflowError");var V=R,d={clearRetain:Symbol.for("ClearRetain")};function E(n,t,e,s){let i,r={},o={};return typeof n[0]=="object"?(Object.assign(o,n[0]),r=typeof n[1]=="boolean"?{retain:n[1]}:n[1]||{},i=n[0].meta):(o.type=n[0],o.payload=n[1],r=typeof n[2]=="boolean"?{retain:n[2]}:n[2]||{}),i=Object.assign({},t,e,r.meta,i),Object.keys(i).length===0&&(i=void 0),o.meta=i,r.executor===void 0&&(r.executor=s),[o,r]}a(E,"parseEmitArgs");function I(n){return n?typeof n=="object"&&"__FastEventScope__"in n:false}a(I,"isFastEventScope");function L(n,t,e){let s=n[0],i=I(n[1])?n[1]:void 0,r=(i?n[2]:n[1])||{};return r.meta=Object.assign({},t,r?.meta),r.context=r.context!==void 0?r.context:e,[s,i,r]}a(L,"parseScopeArgs");function x(n,t){return Object.defineProperty(n,"name",{value:t||"anonymous",configurable:true}),n}a(x,"renameFn");var M=class M{constructor(t){u(this,"__FastEventScope__",true);u(this,"_options",{});u(this,"types",{events:void 0,meta:void 0,context:void 0,message:void 0});u(this,"prefix","");u(this,"emitter");this._options=Object.assign({},this._initOptions(t));}get context(){return this.options.context||this}get options(){return this._options}bind(t,e,s){this.emitter=t,this._options=Object.assign(this._options,{scope:e},s),e.length>0&&!e.endsWith(t.options.delimiter)&&(this.prefix=e+t.options.delimiter);}_initOptions(t){return t}_getScopeListener(t){let e=this.prefix;if(e.length===0)return t;t||(t=(this._options.onMessage||this.onMessage).bind(this));let s=this;return x(function(r,o){if(r.type.startsWith(e))return t.call(s.context,Object.assign({},r,{type:r.type.substring(e.length)}),o)},t.name)}_getScopeType(t){return t===void 0?void 0:this.prefix+t}_fixScopeType(t){return t.startsWith(this.prefix)?t.substring(this.prefix.length):t}on(){if(!this.emitter)throw new y;let t=[...arguments];return t[0]=this._getScopeType(t[0]),t[1]=this._getScopeListener(t[1]),this.emitter.on(...t)}once(){return this.on(arguments[0],arguments[1],Object.assign({},arguments[2],{count:1}))}onAny(){return this.on("**",...arguments)}off(){let t=arguments;typeof t[0]=="string"&&(t[0]=this._getScopeType(t[0])),this.emitter.off(...t);}offAll(){this.emitter.offAll(this.prefix.substring(0,this.prefix.length-1));}clear(){this.emitter.clear(this.prefix.substring(0,this.prefix.length-1));}emit(){if(arguments.length===2&&typeof arguments[0]=="string"&&arguments[1]===d.clearRetain)return this.emitter.emit(this._getScopeType(arguments[0]));let[t,e]=E(arguments,this.emitter.options.meta,this.options.meta,this.options.executor);return t.type=this._getScopeType(t.type),this.emitter.emit(t,e)}async emitAsync(){return (await Promise.allSettled(this.emit.apply(this,arguments))).map(e=>e.status==="fulfilled"?e.value:e.reason)}async waitFor(){let t=arguments[0],e=arguments[1],s=await this.emitter.waitFor(this._getScopeType(t),e);return Object.assign({},s,{type:this._fixScopeType(s.type)})}scope(){let[t,e,s]=L(arguments,this.options.meta,this.options.context),i;return e?i=e:i=new M,i.bind(this.emitter,this.prefix+t,s),i}onMessage(t,e){}};a(M,"FastEventScope");var S=M;function P(n,t){if(n.length!==t.length&&n.length>0&&t[t.length-1]!=="**")return false;let e=[...t];t.length>0&&t[t.length-1]==="**"&&e.splice(t.length-1,1,...Array.from({length:n.length-t.length+1}).fill("*"));for(let s=0;s<n.length;s++)if(e[s]!=="*"&&e[s]!==n[s])return false;return true}a(P,"isPathMatched");function X(n,t){let e=[];for(;;){let s=n.findIndex(i=>t(i));if(s===-1){e.push(s);break}n.splice(s,1);}return e}a(X,"removeItem");function p(n){return n&&typeof n=="function"}a(p,"isFunction");var q=Symbol.for("__expandable__");function z(n){return n[q]=true,n}a(z,"expandable");function G(n){return n&&n[q]}a(G,"isExpandable");function H(n){for(let t=0;t<n.length;t++){let e=n[t];Array.isArray(e)&&G(e)&&(n.splice(t,1,...e),t+=e.length-1);}return n}a(H,"expandEmitResults");function J(n){return n&&typeof n=="object"&&"off"in n&&"listener"in n}a(J,"isSubsctiber");function K(n,t){return n.catch(e=>(t&&t(e),Promise.resolve(e)))}a(K,"tryReturnError");var N=class N{constructor(t){u(this,"__FastEvent__",true);u(this,"listeners",{__listeners:[]});u(this,"_options");u(this,"_delimiter","/");u(this,"_context");u(this,"retainedMessages",new Map);u(this,"listenerCount",0);u(this,"types",{events:void 0,meta:void 0,context:void 0,message:void 0});this._options=Object.assign({debug:false,id:Math.random().toString(36).substring(2),delimiter:"/",context:null,ignoreErrors:true,meta:void 0,expandEmitResults:true},this._initOptions(t)),this._delimiter=this._options.delimiter,this._context=this._options.context,this._enableDevTools();}get options(){return this._options}get context(){return this.options.context||this}get meta(){return this.options.meta}get id(){return this._options.id}_initOptions(t){return t}_addListener(t,e,s){let{count:i,prepend:r}=s,o=0;return [this._forEachNodes(t,h=>{let f=[e,i,0];r?(h.__listeners.splice(0,0,f),o=0):(h.__listeners.push(f),o=h.__listeners.length-1),this.listenerCount++;}),o]}_enableDevTools(){this.options.debug&&globalThis.__FLEXEVENT_DEVTOOLS__&&globalThis.__FLEXEVENT_DEVTOOLS__.add(this);}_forEachNodes(t,e){if(t.length===0)return;let s=this.listeners;for(let i=0;i<t.length;i++){let r=t[i];if(r in s||(s[r]={__listeners:[]}),i===t.length-1){let o=s[r];return e(o,s),o}else s=s[r];}}_removeListener(t,e,s){s&&X(t.__listeners,i=>{i=Array.isArray(i)?i[0]:i;let r=i===s;return r&&(this.listenerCount--,p(this._options.onRemoveListener)&&this._options.onRemoveListener(e.join(this._delimiter),s)),r});}_pipeListener(t,e){return e.forEach(s=>{t=x(s(t),t.name);}),t}on(){let t=arguments[0],e=p(arguments[1])?arguments[1]:(this._options.onMessage||this.onMessage).bind(this),s=Object.assign({count:0,prepend:false},p(arguments[1])?arguments[2]:arguments[1]);if(t.length===0)throw new Error("event type cannot be empty");if(p(this._options.onAddListener)){let h=this._options.onAddListener(t,e,s);if(h===false)throw new v;if(J(h))return h}let i=t.split(this._delimiter);if(s.pipes&&s.pipes.length>0&&(e=this._pipeListener(e,s.pipes)),p(s.filter)||p(s.off)){let h=e;e=x(function(f,l){if(p(s.off)&&s.off.call(this,f,l)){c();return}if(p(s.filter)){if(s.filter.call(this,f,l))return h.call(this,f,l)}else return h.call(this,f,l)},e.name);}let[r,o]=this._addListener(i,e,s),c=a(()=>r&&this._removeListener(r,i,e),"off");return this._emitRetainMessage(t,r,o),{off:c,listener:e}}once(){return p(arguments[1])?this.on(arguments[0],arguments[1],Object.assign({},arguments[2],{count:1})):this.on(arguments[0],Object.assign({},arguments[2],{count:1}))}onAny(){return this.on("**",arguments[0],arguments[1])}onMessage(t,e){}off(){let t=arguments,e=p(t[0])?void 0:t[0],s=p(t[0])?t[0]:t[1],i=e?e.split(this._delimiter):[],r=e?e.includes("*"):false;if(e&&!r)this._traverseToPath(this.listeners,i,o=>{s?this._removeListener(o,i,s):e&&(o.__listeners=[]);});else {let o=r?[]:i;this._traverseListeners(this.listeners,o,(c,h)=>{(s!==void 0||r&&P(c,i))&&(s?this._removeListener(h,i,s):h.__listeners=[]);});}}offAll(t){if(t){let e=t.split(this._delimiter),s=0;this._traverseListeners(this.listeners,e,(i,r)=>{s+=r.__listeners.length,r.__listeners=[];}),this.listenerCount-=s,this._removeRetainedEvents(t);}else {let e=0;this._traverseListeners(this.listeners,[],(s,i)=>{e+=i.__listeners.length;}),this.listenerCount-=e,this.retainedMessages.clear(),this.listeners={__listeners:[]};}p(this._options.onClearListeners)&&this._options.onClearListeners.call(this);}_removeRetainedEvents(t){t||this.retainedMessages.clear(),t?.endsWith(this._delimiter)&&(t+=this._delimiter),this.retainedMessages.delete(t);for(let e of this.retainedMessages.keys())e.startsWith(t)&&this.retainedMessages.delete(e);}clear(t){this.offAll(t),this._removeRetainedEvents(t);}_emitRetainMessage(t,e,s){let i=[];if(t.includes("*")){let r=t.split(this._delimiter);this.retainedMessages.forEach((o,c)=>{let h=c.split(this._delimiter);P(h,r)&&i.push(o);});}else this.retainedMessages.has(t)&&i.push(this.retainedMessages.get(t));e&&i.forEach(r=>{this._executeListeners([e],r,{},o=>o[0]===e.__listeners[s][0]);});}_traverseToPath(t,e,s,i=0,r){if(i>=e.length){s(t);return}let o=e[i];if(r===true){this._traverseToPath(t,e,s,i+1,true);return}"*"in t&&this._traverseToPath(t["*"],e,s,i+1),"**"in t&&this._traverseToPath(t["**"],e,s,i+1,true),o in t&&this._traverseToPath(t[o],e,s,i+1);}_traverseListeners(t,e,s){let i=t;e&&e.length>0&&this._traverseToPath(t,e,o=>{i=o;});let r=a((o,c,h)=>{c(h,o);for(let[f,l]of Object.entries(o))f.startsWith("__")||l&&r(l,c,[...h,f]);},"traverseNodes");r(i,s,[]);}_onListenerError(t,e,s,i){if(i instanceof Error&&(i._emitter=`${t.name||"anonymous"}:${e.type}`),p(this._options.onListenerError))try{this._options.onListenerError.call(this,i,t,e,s);}catch{}if(this._options.ignoreErrors)return i;throw i}_executeListener(t,e,s,i=false){try{if(s&&s.abortSignal&&s.abortSignal.aborted)return this._onListenerError(t,e,s,new g(t.name));let r=t.call(this.context,e,s);return i&&r&&r instanceof Promise&&(r=K(r,o=>this._onListenerError(t,e,s,o))),r}catch(r){return this._onListenerError(t,e,s,r)}}_getListenerExecutor(t){if(!t)return;let e=t.executor||this._options.executor;if(p(e))return e}_executeListeners(t,e,s,i){if(!t||t.length===0)return [];let r=t.reduce((o,c)=>o.concat(c.__listeners.filter(h=>p(i)?i(h,c):true).map((h,f)=>[h,f,c.__listeners])),[]);try{let o=this._getListenerExecutor(s);if(o){let c=o(r.map(h=>h[0]),e,s,this._executeListener.bind(this));return Array.isArray(c)?c:[c]}else return r.map(c=>this._executeListener(c[0][0],e,s,!0))}finally{for(let o=r.length-1;o>=0;o--){let c=r[o][0];c[2]++,c[1]>0&&c[1]<=c[2]&&r[o][2].splice(o,1);}}}emit(){if(arguments.length===2&&typeof arguments[0]=="string"&&arguments[1]===d.clearRetain)return this.retainedMessages.delete(arguments[0]),[];let[t,e]=E(arguments,this.options.meta);p(e.parseArgs)&&e.parseArgs(t,e);let s=t.type.split(this._delimiter);e.retain&&this.retainedMessages.set(t.type,t);let i=[],r=[];if(this._traverseToPath(this.listeners,s,o=>{r.push(o);}),p(this._options.onBeforeExecuteListener)){let o=this._options.onBeforeExecuteListener.call(this,t,e);if(Array.isArray(o))return o;if(o===false)throw new g(t.type)}return i.push(...this._executeListeners(r,t,e)),p(this._options.onAfterExecuteListener)&&this._options.onAfterExecuteListener.call(this,t,i,r),this._options.expandEmitResults&&H(i),i}async emitAsync(){return (await Promise.allSettled(this.emit.apply(this,arguments))).map(e=>e.status==="fulfilled"?e.value:e.reason)}waitFor(){let t=arguments[0],e=arguments[1];return new Promise((s,i)=>{let r,o,c=a(h=>{clearTimeout(r),o.off(),s(h);},"listener");e&&e>0&&(r=setTimeout(()=>{o&&o.off(),i(new Error("wait for event<"+t+"> is timeout"));},e)),o=this.on(t,c);})}scope(){let[t,e,s]=L(arguments,this.options.meta,this.options.context),i;return e?i=e:i=new S,i.bind(this,t,s),i}};a(N,"FastEvent");var _=N;var b="::",A="@",Y="data";function Z(n){return n?typeof n=="object"&&"type"in n:false}a(Z,"isFastEventMessage");function w(n,t="/"){let e=Z(n[0]),s=Object.assign({payload:e?n[0].payload:n[1]},e?n[0]:{},{type:`${A}${t}${e?n[0].type:"data"}`}),i=e?n[1]:n[2];return [s,i]}a(w,"parseBroadcaseArgs");var D=class D extends _{constructor(e){super(e);u(this,"nodes");this.nodes=new Map;}add(...e){e.forEach(s=>{if(this.nodes.has(s.id))throw new Error(`Node with id ${s.id} already exists`);s.options.delimiter=this.options.delimiter,s.options.debug=this.options.debug,s.options.ignoreErrors=this.options.ignoreErrors,this.nodes.set(s.id,s),this.emit(`$disconnect${this.options.delimiter}${s.id}`,d.clearRetain),this.emit(`$connect${this.options.delimiter}${s.id}`,s.id,true);});}remove(e){let s=this.nodes.get(e);s&&(s.eventbus=void 0,this.nodes.delete(e),this.emit(`$connect${this.options.delimiter}${s.id}`,d.clearRetain),this.emit(`$disconnect${this.options.delimiter}${s.id}`,s.id,true));}broadcast(){let[e,s]=w(arguments,this.options.delimiter);return this.emit(e,s)}send(e,s){return e.type=`${e.to}${this.options.delimiter}${Y}`,this.emit(e,s)[0]}};a(D,"FastEventBus");var k=D;var C=class C extends _{constructor(e){super(e);u(this,"eventbus");u(this,"_subscribers",[]);this.options.onBeforeExecuteListener=this._onBeforeExecuteListener.bind(this),this.options.onAddListener=this._onAddListener.bind(this),this._subscribers.push(this.on("data"));}_onBeforeExecuteListener(e,s){if(e.type.includes(b))return e.type=e.type.replace(b,this.eventbus.options.delimiter),e.from=this.id,this.eventbus.emit(e,s)}_onAddListener(e,s,i){if(e.includes(b)){let[r,o]=e.split(b);if(r===this.id)return;let c,h=this.eventbus.on(`$connect${this.options.delimiter}*`,f=>{if(f.payload===r){let l=this.eventbus.nodes.get(r);l&&(c=l.on(o,s,i)),h.off();}});return {off:a(()=>c?c.off():h.off(),"off"),listener:c?c.listener:h.listener}}}connect(e){this.eventbus=e,this.eventbus.add(this),this._subscribers.push(this.eventbus.on(`${A}${this.eventbus.options.delimiter}**`,this.onMessage.bind(this))),this._onSubscribeForNode();}disconnect(){this.eventbus?.remove(this.id),this._subscribers.forEach(e=>e.off());}_onSubscribeForNode(){let e=`${this.id}${this.eventbus.options.delimiter}`;this._subscribers.push(this.eventbus.on(`${e}**`,(s,i)=>(s.type=s.type.substring(e.length),z(this.emit(s,i)))));}send(e,s,i){this._assertConnected();let r={type:"data",from:this.id,to:e,payload:s};return this.eventbus.send(r,i)}_assertConnected(){if(!this.eventbus)throw new Error("Node is not connected to any event bus")}broadcast(){this._assertConnected();let[e,s]=w(arguments,this.options.delimiter);return e.from=this.id,this.eventbus.broadcast(e,s)}onMessage(e,s){}};a(C,"FastEventBusNode");var U=C;exports.AbortError=g;exports.BroadcastEvent=A;exports.CancelError=v;exports.FastEvent=_;exports.FastEventBus=k;exports.FastEventBusNode=U;exports.FastEventDirectives=d;exports.FastEventError=m;exports.FastEventScope=S;exports.NamespaceDelimiter=b;exports.NodeDataEvent=Y;exports.QueueOverflowError=V;exports.TimeoutError=W;exports.UnboundError=y;exports.__FastEventScope__=st;exports.__FastEvent__=et;//# sourceMappingURL=index.js.map
|
|
1
|
+
'use strict';var B=Object.defineProperty;var Q=(n,e,t)=>e in n?B(n,e,{enumerable:true,configurable:true,writable:true,value:t}):n[e]=t;var a=(n,e)=>B(n,"name",{value:e,configurable:true});var u=(n,e,t)=>Q(n,typeof e!="symbol"?e+"":e,t);var te=Symbol.for("__FastEvent__"),se=Symbol.for("__FastEventScope__"),$=class $ extends Error{constructor(e){super(e);}};a($,"FastEventError");var m=$,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,e,t,s){let i,r={},o={};return typeof n[0]=="object"?(Object.assign(o,n[0]),r=typeof n[1]=="boolean"?{retain:n[1]}:n[1]||{},i=n[0].meta):(o.type=n[0],o.payload=n[1],r=typeof n[2]=="boolean"?{retain:n[2]}:n[2]||{}),i=Object.assign({},e,t,r.meta,i),Object.keys(i).length===0&&(i=void 0),o.meta=i,r.executor===void 0&&(r.executor=s),[o,r]}a(E,"parseEmitArgs");function I(n){return n?typeof n=="object"&&"__FastEventScope__"in n:false}a(I,"isFastEventScope");function L(n,e,t){let s=n[0],i=I(n[1])?n[1]:void 0,r=(i?n[2]:n[1])||{};return r.meta=Object.assign({},e,r?.meta),r.context=r.context!==void 0?r.context:t,[s,i,r]}a(L,"parseScopeArgs");function x(n,e){return Object.defineProperty(n,"name",{value:e||"anonymous",configurable:true}),n}a(x,"renameFn");var M=class M{constructor(e){u(this,"__FastEventScope__",true);u(this,"_options",{});u(this,"types",{events:void 0,meta:void 0,context:void 0,message:void 0,listeners:void 0,anyListener:void 0});u(this,"prefix","");u(this,"emitter");this._options=Object.assign({},this._initOptions(e));}get context(){return this.options.context||this}get options(){return this._options}bind(e,t,s){this.emitter=e,this._options=Object.assign(this._options,{scope:t},s),t.length>0&&!t.endsWith(e.options.delimiter)&&(this.prefix=t+e.options.delimiter);}_initOptions(e){return e}_getScopeListener(e){let t=this.prefix;if(t.length===0)return e;e||(e=(this._options.onMessage||this.onMessage).bind(this));let s=this;return x(function(r,o){if(r.type.startsWith(t))return e.call(s.context,Object.assign({},r,{type:r.type.substring(t.length)}),o)},e.name)}_getScopeType(e){return e===void 0?void 0:this.prefix+e}_fixScopeType(e){return e.startsWith(this.prefix)?e.substring(this.prefix.length):e}on(){if(!this.emitter)throw new y;let e=[...arguments];return e[0]=this._getScopeType(e[0]),e[1]=this._getScopeListener(e[1]),this.emitter.on(...e)}once(){return this.on(arguments[0],arguments[1],Object.assign({},arguments[2],{count:1}))}onAny(){return this.on("**",...arguments)}off(){let e=arguments;typeof e[0]=="string"&&(e[0]=this._getScopeType(e[0])),this.emitter.off(...e);}offAll(){this.emitter.offAll(this.prefix.substring(0,this.prefix.length-1));}clear(){this.emitter.clear(this.prefix.substring(0,this.prefix.length-1));}emit(){if(arguments.length===2&&typeof arguments[0]=="string"&&arguments[1]===d.clearRetain)return this.emitter.emit(this._getScopeType(arguments[0]));let[e,t]=E(arguments,this.emitter.options.meta,this.options.meta,this.options.executor);return e.type=this._getScopeType(e.type),this.emitter.emit(e,t)}async emitAsync(){return (await Promise.allSettled(this.emit.apply(this,arguments))).map(t=>t.status==="fulfilled"?t.value:t.reason)}async waitFor(){let e=arguments[0],t=arguments[1],s=await this.emitter.waitFor(this._getScopeType(e),t);return Object.assign({},s,{type:this._fixScopeType(s.type)})}scope(){let[e,t,s]=L(arguments,this.options.meta,this.options.context),i;return t?i=t:i=new M,i.bind(this.emitter,this.prefix+e,s),i}onMessage(e,t){}};a(M,"FastEventScope");var S=M;function P(n,e){if(n.length!==e.length&&n.length>0&&e[e.length-1]!=="**")return false;let t=[...e];e.length>0&&e[e.length-1]==="**"&&t.splice(e.length-1,1,...Array.from({length:n.length-e.length+1}).fill("*"));for(let s=0;s<n.length;s++)if(t[s]!=="*"&&t[s]!==n[s])return false;return true}a(P,"isPathMatched");function X(n,e){let t=[];for(;;){let s=n.findIndex(i=>e(i));if(s===-1){t.push(s);break}n.splice(s,1);}return t}a(X,"removeItem");function p(n){return n&&typeof n=="function"}a(p,"isFunction");var q=Symbol.for("__expandable__");function z(n){return n[q]=true,n}a(z,"expandable");function G(n){return n&&n[q]}a(G,"isExpandable");function H(n){for(let e=0;e<n.length;e++){let t=n[e];Array.isArray(t)&&G(t)&&(n.splice(e,1,...t),e+=t.length-1);}return n}a(H,"expandEmitResults");function J(n){return n&&typeof n=="object"&&"off"in n&&"listener"in n}a(J,"isSubsctiber");function K(n,e){return n.catch(t=>(e&&e(t),Promise.resolve(t)))}a(K,"tryReturnError");var N=class N{constructor(e){u(this,"__FastEvent__",true);u(this,"listeners",{__listeners:[]});u(this,"_options");u(this,"_delimiter","/");u(this,"_context");u(this,"retainedMessages",new Map);u(this,"listenerCount",0);u(this,"types",{events:void 0,meta:void 0,context:void 0,message:void 0,listeners:void 0,anyListener:void 0});this._options=Object.assign({debug:false,id:Math.random().toString(36).substring(2),delimiter:"/",context:null,ignoreErrors:true,meta:void 0,expandEmitResults:true},this._initOptions(e)),this._delimiter=this._options.delimiter,this._context=this._options.context,this._enableDevTools();}get options(){return this._options}get context(){return this.options.context||this}get meta(){return this.options.meta}get id(){return this._options.id}_initOptions(e){return e}_addListener(e,t,s){let{count:i,prepend:r}=s,o=0;return [this._forEachNodes(e,h=>{let f=[t,i,0];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
|
|
2
2
|
//# sourceMappingURL=index.js.map
|