fastevent 2.3.1 → 2.3.3
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 +39 -21
- package/dist/eventbus/index.d.ts +39 -21
- 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/index.d.mts +39 -21
- package/dist/index.d.ts +39 -21
- 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/package.json +1 -1
|
@@ -14,6 +14,17 @@ type ExpandWildcard<T extends Record<string, any>> = Expand$1<T & {
|
|
|
14
14
|
[K in WildcardKeys<T> as ReplaceWildcard<K>]: T[K];
|
|
15
15
|
}>;
|
|
16
16
|
|
|
17
|
+
type UnionToTuple<T> = UnionToTupleRec<T, []>;
|
|
18
|
+
type UnionToTupleRec<T, R extends any[]> = [T] extends [never] ? R : UnionToTupleRec<Exclude<T, LastOfUnion<T>>, [LastOfUnion<T>, ...R]>;
|
|
19
|
+
type LastOfUnion<T> = UnionToIntersection<T extends any ? (x: T) => 0 : never> extends (x: infer L) => 0 ? L : never;
|
|
20
|
+
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
21
|
+
type Keys<T extends Record<string, any>> = UnionToTuple<keyof T>;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 保留对象中第一项
|
|
25
|
+
*/
|
|
26
|
+
type FirstObjectItem<T extends Record<string, any>> = Pick<T, Keys<T> extends any[] ? Keys<T>[0] : never>;
|
|
27
|
+
|
|
17
28
|
type MergeUnion<T> = (T extends any ? (x: T) => void : never) extends (x: infer U) => void ? {
|
|
18
29
|
[K in keyof U]: U[K];
|
|
19
30
|
} : never;
|
|
@@ -24,14 +35,25 @@ type MatchPattern<T extends string, Pattern extends string> = MatchPatternArray<
|
|
|
24
35
|
[K in Pattern]: any;
|
|
25
36
|
} : never;
|
|
26
37
|
type Fallback$1<T, F> = [T] extends [never] ? F : T extends undefined ? F : T;
|
|
27
|
-
|
|
38
|
+
/**
|
|
39
|
+
*
|
|
40
|
+
* 返回所有匹配事件的类型
|
|
41
|
+
*
|
|
42
|
+
* 支持通配符
|
|
43
|
+
*
|
|
44
|
+
* @param T 事件名称
|
|
45
|
+
* @param Events 事件类型
|
|
46
|
+
* @returns
|
|
47
|
+
*
|
|
48
|
+
*/
|
|
49
|
+
type WildcardEvents<Events extends Record<string, any>, T extends string> = MergeUnion<Fallback$1<{
|
|
28
50
|
[K in keyof Events]: MatchPattern<T, K & string> extends never ? never : {
|
|
29
51
|
[P in K]: Events[K];
|
|
30
52
|
};
|
|
31
53
|
}[keyof Events] extends infer Result ? Result extends Record<string, any> ? Result : Record<string, any> : Record<string, any>, {
|
|
32
54
|
[K in T]: any;
|
|
33
55
|
}>>;
|
|
34
|
-
type
|
|
56
|
+
type ClosestWildcardEvents<Events extends Record<string, any>, T extends string> = FirstObjectItem<WildcardEvents<Events, T>>;
|
|
35
57
|
|
|
36
58
|
type Split<S extends string, Delimiter extends string> = S extends `${infer Head}${Delimiter}${infer Tail}` ? [Head, ...Split<Tail, Delimiter>] : [S];
|
|
37
59
|
type MatchPatternAndGetRemainder<KeyParts extends string[], PrefixParts extends string[], Result extends string[] = []> = PrefixParts['length'] extends 0 ? KeyParts['length'] extends 0 ? never : KeyParts : KeyParts['length'] extends 0 ? never : KeyParts[0] extends PrefixParts[0] | '*' ? MatchPatternAndGetRemainder<Slice<KeyParts, 1>, Slice<PrefixParts, 1>, Result> : never;
|
|
@@ -288,9 +310,6 @@ type Overloads<T> = Unique<T extends {
|
|
|
288
310
|
(...args: infer A1): infer R1;
|
|
289
311
|
} ? [(...args: A1) => R1] : [T]>;
|
|
290
312
|
type Dict<V = any> = Record<Exclude<string, number | symbol>, V>;
|
|
291
|
-
type Union<T> = T extends infer O ? {
|
|
292
|
-
[K in keyof O]: O[K];
|
|
293
|
-
} : never;
|
|
294
313
|
type RecordValues<R extends Record<string, any>> = R[keyof R];
|
|
295
314
|
type RecordPrefix<P extends string, R extends Record<string, any>> = {
|
|
296
315
|
[K in keyof R as K extends `${P}/${infer S}` ? S : never]: R[K];
|
|
@@ -384,7 +403,6 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
384
403
|
meta: FinalMeta;
|
|
385
404
|
context: Fallback<Context, typeof this>;
|
|
386
405
|
message: TypedFastEventMessageOptional<Events, FinalMeta>;
|
|
387
|
-
listeners: FastEventListeners<Events, FinalMeta>;
|
|
388
406
|
anyListener: TypedFastEventAnyListener<Events, FinalMeta, Fallback<Context, typeof this>>;
|
|
389
407
|
};
|
|
390
408
|
prefix: string;
|
|
@@ -416,14 +434,14 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
416
434
|
on<T extends Exclude<string, Types>>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
417
435
|
on(type: '**', options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
418
436
|
on<T extends keyof OmitTransformedEvents<Events>>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
419
|
-
on<T extends keyof PickTransformedEvents<Events>>(type: T, listener: (message: PickPayload<RecordValues<
|
|
420
|
-
on<T extends Exclude<string, Types>>(type: T, listener: TypedFastEventAnyListener<
|
|
437
|
+
on<T extends keyof PickTransformedEvents<Events>>(type: T, listener: (message: PickPayload<RecordValues<ClosestWildcardEvents<Events, Exclude<T, number | symbol>>>>, args: FastEventListenerArgs<Meta>) => any | Promise<any>, options?: FastEventListenOptions<Events, Meta>): FastEventSubscriber;
|
|
438
|
+
on<T extends Exclude<string, Types>>(type: T, listener: TypedFastEventAnyListener<ClosestWildcardEvents<Events, T>, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
421
439
|
on(type: '**', listener: TypedFastEventAnyListener<Events, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
422
440
|
once<T extends Types = Types>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
423
441
|
once<T extends Exclude<string, Types>>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
424
442
|
once<T extends keyof OmitTransformedEvents<Events>>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
425
|
-
once<T extends keyof PickTransformedEvents<Events>>(type: T, listener: (message: PickPayload<RecordValues<
|
|
426
|
-
once<T extends Exclude<string, Types>>(type: T, listener: TypedFastEventAnyListener<
|
|
443
|
+
once<T extends keyof PickTransformedEvents<Events>>(type: T, listener: (message: PickPayload<RecordValues<WildcardEvents<Events, Exclude<T, number | symbol>>>>, args: FastEventListenerArgs<Meta>) => any | Promise<any>, options?: FastEventListenOptions<Events, Meta>): FastEventSubscriber;
|
|
444
|
+
once<T extends Exclude<string, Types>>(type: T, listener: TypedFastEventAnyListener<WildcardEvents<Events, T>, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
427
445
|
onAny(options?: Pick<FastEventListenOptions, 'prepend'>): FastEventSubscriber;
|
|
428
446
|
onAny<P = any>(listener: TypedFastEventAnyListener<{
|
|
429
447
|
[K: string]: P;
|
|
@@ -438,8 +456,8 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
438
456
|
emit(type: Types, directive: symbol): void;
|
|
439
457
|
emit(type: string, directive: symbol): void;
|
|
440
458
|
emit<R = any, T extends Types = Types>(type: T, payload?: PickPayload<Events[T]>, retain?: boolean): R[];
|
|
441
|
-
emit<R = any, T extends string = string>(type: T, payload?: PickPayload<T extends Types ? Events[T] : RecordValues<
|
|
442
|
-
emit<R = any, T extends string = string>(type: T, payload?: PickPayload<T extends Types ? Events[T] : RecordValues<
|
|
459
|
+
emit<R = any, T extends string = string>(type: T, payload?: PickPayload<T extends Types ? Events[T] : RecordValues<WildcardEvents<Events, T>>>, retain?: boolean): R[];
|
|
460
|
+
emit<R = any, T extends string = string>(type: T, payload?: PickPayload<T extends Types ? Events[T] : RecordValues<WildcardEvents<Events, T>>>, options?: FastEventListenerArgs<FinalMeta>): R[];
|
|
443
461
|
emit<R = any>(message: FastEventEmitMessage<AtPayloads<Events>, FinalMeta>, options?: FastEventListenerArgs<FinalMeta>): R[];
|
|
444
462
|
emit<R = any, T extends string = string>(message: FastEventEmitMessage<{
|
|
445
463
|
[K in T]: PickPayload<K extends Types ? Events[K] : any>;
|
|
@@ -494,7 +512,7 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
494
512
|
* ```
|
|
495
513
|
*/
|
|
496
514
|
scope<E extends Record<string, any> = Record<string, any>, P extends string = string, M extends Record<string, any> = Record<string, any>, C = Context>(prefix: P, options?: DeepPartial<FastEventScopeOptions<Partial<FinalMeta> & M, C>>): FastEventScope<ScopeEvents<Events, P> & E, FinalMeta & M, C>;
|
|
497
|
-
scope<E extends Record<string, any> = Record<string, any>, P extends string = string, M extends Record<string, any> = Record<string, any>, C = Context, ScopeInstance extends FastEventScope<
|
|
515
|
+
scope<E extends Record<string, any> = Record<string, any>, P extends string = string, M extends Record<string, any> = Record<string, any>, C = Context, ScopeInstance extends FastEventScope<Record<string, any>, any, any> = FastEventScope<Record<string, any>, any, any>>(prefix: P, scopeObj: ScopeInstance, options?: DeepPartial<FastEventScopeOptions<Partial<FinalMeta> & M, C>>): ScopeInstance & FastEventScope<ScopeEvents<Events, P> & E, FinalMeta & M, C>;
|
|
498
516
|
/**
|
|
499
517
|
* 当on/once/onAny未指定监听器时,此为默认监听器
|
|
500
518
|
* @param message
|
|
@@ -616,8 +634,8 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
616
634
|
on<T extends string>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
617
635
|
on(type: '**', options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
618
636
|
on<T extends keyof OmitTransformedEvents<AllEvents>>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, AllEvents[T], Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
619
|
-
on<T extends keyof PickTransformedEvents<AllEvents>>(type: T, listener: (message: PickPayload<RecordValues<
|
|
620
|
-
on<T extends Exclude<string, Types>>(type: T, listener: TypedFastEventAnyListener<
|
|
637
|
+
on<T extends keyof PickTransformedEvents<AllEvents>>(type: T, listener: (message: PickPayload<RecordValues<ClosestWildcardEvents<AllEvents, Exclude<T, number | symbol>>>>, args: FastEventListenerArgs<Meta>) => any | Promise<any>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
638
|
+
on<T extends Exclude<string, Types>>(type: T, listener: TypedFastEventAnyListener<ClosestWildcardEvents<AllEvents, T>, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
621
639
|
on(type: '**', listener: TypedFastEventAnyListener<Record<string, any>, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
622
640
|
/**
|
|
623
641
|
* 注册一次性事件监听器
|
|
@@ -643,8 +661,8 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
643
661
|
once<T extends Types = Types>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
644
662
|
once<T extends string>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
645
663
|
once<T extends keyof OmitTransformedEvents<AllEvents>>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, AllEvents[T], Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
646
|
-
once<T extends keyof PickTransformedEvents<AllEvents>>(type: T, listener: (message: PickPayload<RecordValues<
|
|
647
|
-
once<T extends Exclude<string, Types>>(type: T, listener: TypedFastEventAnyListener<
|
|
664
|
+
once<T extends keyof PickTransformedEvents<AllEvents>>(type: T, listener: (message: PickPayload<RecordValues<ClosestWildcardEvents<AllEvents, Exclude<T, number | symbol>>>>, args: FastEventListenerArgs<Meta>) => any | Promise<any>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
665
|
+
once<T extends Exclude<string, Types>>(type: T, listener: TypedFastEventAnyListener<ClosestWildcardEvents<AllEvents, T>, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
648
666
|
/**
|
|
649
667
|
* 注册一个监听器,用于监听所有事件
|
|
650
668
|
* @param listener 事件监听器函数,可以接收任意类型的事件数据
|
|
@@ -834,13 +852,13 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
834
852
|
emit<T extends Types = Types>(type: T, directive: symbol): any[];
|
|
835
853
|
emit(type: string, directive: symbol): any[];
|
|
836
854
|
emit<R = any, T extends Types = Types>(type: T, payload?: PickPayload<AllEvents[T]>, retain?: boolean): R[];
|
|
837
|
-
emit<R = any, T extends string = string>(type: T, payload: PickPayload<T extends Types ? AllEvents[T] : RecordValues<
|
|
855
|
+
emit<R = any, T extends string = string>(type: T, payload: PickPayload<T extends Types ? AllEvents[T] : RecordValues<WildcardEvents<AllEvents, T>>>, retain?: boolean): R[];
|
|
838
856
|
emit<R = any, T extends string = string>(message: FastEventEmitMessage<{
|
|
839
857
|
[K in T]: K extends Types ? AllEvents[K] : any;
|
|
840
858
|
}, Meta>, retain?: boolean): R[];
|
|
841
859
|
emit<R = any>(message: FastEventEmitMessage<AllEvents, Meta>, retain?: boolean): R[];
|
|
842
860
|
emit<R = any, T extends Types = Types>(type: T, payload?: PickPayload<AllEvents[T]>, options?: FastEventListenerArgs<Meta>): R[];
|
|
843
|
-
emit<R = any, T extends string = string>(type: T, payload: PickPayload<T extends Types ? AllEvents[T] : RecordValues<
|
|
861
|
+
emit<R = any, T extends string = string>(type: T, payload: PickPayload<T extends Types ? AllEvents[T] : RecordValues<WildcardEvents<AllEvents, T>>>, options?: FastEventListenerArgs<Meta>): R[];
|
|
844
862
|
emit<R = any, T extends string = string>(message: FastEventEmitMessage<{
|
|
845
863
|
[K in T]: PickPayload<K extends Types ? AllEvents[K] : any>;
|
|
846
864
|
}, Meta>, options?: FastEventListenerArgs<Meta>): R[];
|
|
@@ -975,7 +993,7 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
975
993
|
*/
|
|
976
994
|
scope<P extends string>(prefix: P, options?: DeepPartial<FastEventScopeOptions<Meta, Context>>): FastEventScope<ScopeEvents<AllEvents, P>, Meta, Context>;
|
|
977
995
|
scope<E extends Record<string, any> = Record<string, any>, P extends string = string, M extends Record<string, any> = Record<string, any>, C = Context>(prefix: P, options?: DeepPartial<FastEventScopeOptions<Meta & M, C>>): FastEventScope<ScopeEvents<AllEvents, P> & E, Meta & M, C>;
|
|
978
|
-
scope<E extends Record<string, any> = Record<string, any>, P extends string = string, M extends Record<string, any> = Record<string, any>, C = Context, ScopeObject extends FastEventScope<any, any, any> = FastEventScope<
|
|
996
|
+
scope<E extends Record<string, any> = Record<string, any>, P extends string = string, M extends Record<string, any> = Record<string, any>, C = Context, ScopeObject extends FastEventScope<any, any, any> = FastEventScope<any, any, any>>(prefix: P, scopeObj: ScopeObject, options?: DeepPartial<FastEventScopeOptions<Meta & M, C>>): ScopeObject & FastEventScope<ScopeEvents<AllEvents, P> & E, Meta & M, C>;
|
|
979
997
|
}
|
|
980
998
|
|
|
981
999
|
type FastEventBusNodeOptions<Meta = Record<string, any>, Context = any> = FastEventOptions<Meta, Context> & {};
|
|
@@ -1234,4 +1252,4 @@ declare function isClass(target: unknown): target is new (...args: any[]) => any
|
|
|
1234
1252
|
|
|
1235
1253
|
declare function isFastEvent(target: any): target is FastEvent;
|
|
1236
1254
|
|
|
1237
|
-
export { AbortError, type AssertFastMessage, type AtPayloads, BroadcastEvent, CancelError, type ChangeFieldType, type DeepPartial, type Dict, type Expand, type ExpandWildcard, 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, FastEventListenerFlags, 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 FastMessagePayload, type
|
|
1255
|
+
export { AbortError, type AssertFastMessage, type AtPayloads, BroadcastEvent, CancelError, type ChangeFieldType, type ClosestWildcardEvents, type DeepPartial, type Dict, type Expand, type ExpandWildcard, 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, FastEventListenerFlags, 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 FastMessagePayload, type Keys, type Merge, type MergeUnion, NamespaceDelimiter, NodeDataEvent, type NotPayload, type ObjectKeys, type OmitTransformedEvents, type OptionalItems, type Overloads, type OverrideOptions, type PickPayload, type PickTransformedEvents, QueueOverflowError, type RecordPrefix, type RecordValues, type RequiredItems, type ScopeEvents, TimeoutError, type TransformedEvents, type TypedFastEventAnyListener, type TypedFastEventListener, type TypedFastEventMessage, type TypedFastEventMessageOptional, UnboundError, type Unique, type WildcardEvents, __FastEventScope__, __FastEvent__, __expandable__, expandable, isClass, isExpandable, isFastEvent, isFastEventMessage, isFastEventScope, isFunction, isPathMatched, isString, isSubsctiber };
|
package/dist/eventbus/index.d.ts
CHANGED
|
@@ -14,6 +14,17 @@ type ExpandWildcard<T extends Record<string, any>> = Expand$1<T & {
|
|
|
14
14
|
[K in WildcardKeys<T> as ReplaceWildcard<K>]: T[K];
|
|
15
15
|
}>;
|
|
16
16
|
|
|
17
|
+
type UnionToTuple<T> = UnionToTupleRec<T, []>;
|
|
18
|
+
type UnionToTupleRec<T, R extends any[]> = [T] extends [never] ? R : UnionToTupleRec<Exclude<T, LastOfUnion<T>>, [LastOfUnion<T>, ...R]>;
|
|
19
|
+
type LastOfUnion<T> = UnionToIntersection<T extends any ? (x: T) => 0 : never> extends (x: infer L) => 0 ? L : never;
|
|
20
|
+
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
21
|
+
type Keys<T extends Record<string, any>> = UnionToTuple<keyof T>;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 保留对象中第一项
|
|
25
|
+
*/
|
|
26
|
+
type FirstObjectItem<T extends Record<string, any>> = Pick<T, Keys<T> extends any[] ? Keys<T>[0] : never>;
|
|
27
|
+
|
|
17
28
|
type MergeUnion<T> = (T extends any ? (x: T) => void : never) extends (x: infer U) => void ? {
|
|
18
29
|
[K in keyof U]: U[K];
|
|
19
30
|
} : never;
|
|
@@ -24,14 +35,25 @@ type MatchPattern<T extends string, Pattern extends string> = MatchPatternArray<
|
|
|
24
35
|
[K in Pattern]: any;
|
|
25
36
|
} : never;
|
|
26
37
|
type Fallback$1<T, F> = [T] extends [never] ? F : T extends undefined ? F : T;
|
|
27
|
-
|
|
38
|
+
/**
|
|
39
|
+
*
|
|
40
|
+
* 返回所有匹配事件的类型
|
|
41
|
+
*
|
|
42
|
+
* 支持通配符
|
|
43
|
+
*
|
|
44
|
+
* @param T 事件名称
|
|
45
|
+
* @param Events 事件类型
|
|
46
|
+
* @returns
|
|
47
|
+
*
|
|
48
|
+
*/
|
|
49
|
+
type WildcardEvents<Events extends Record<string, any>, T extends string> = MergeUnion<Fallback$1<{
|
|
28
50
|
[K in keyof Events]: MatchPattern<T, K & string> extends never ? never : {
|
|
29
51
|
[P in K]: Events[K];
|
|
30
52
|
};
|
|
31
53
|
}[keyof Events] extends infer Result ? Result extends Record<string, any> ? Result : Record<string, any> : Record<string, any>, {
|
|
32
54
|
[K in T]: any;
|
|
33
55
|
}>>;
|
|
34
|
-
type
|
|
56
|
+
type ClosestWildcardEvents<Events extends Record<string, any>, T extends string> = FirstObjectItem<WildcardEvents<Events, T>>;
|
|
35
57
|
|
|
36
58
|
type Split<S extends string, Delimiter extends string> = S extends `${infer Head}${Delimiter}${infer Tail}` ? [Head, ...Split<Tail, Delimiter>] : [S];
|
|
37
59
|
type MatchPatternAndGetRemainder<KeyParts extends string[], PrefixParts extends string[], Result extends string[] = []> = PrefixParts['length'] extends 0 ? KeyParts['length'] extends 0 ? never : KeyParts : KeyParts['length'] extends 0 ? never : KeyParts[0] extends PrefixParts[0] | '*' ? MatchPatternAndGetRemainder<Slice<KeyParts, 1>, Slice<PrefixParts, 1>, Result> : never;
|
|
@@ -288,9 +310,6 @@ type Overloads<T> = Unique<T extends {
|
|
|
288
310
|
(...args: infer A1): infer R1;
|
|
289
311
|
} ? [(...args: A1) => R1] : [T]>;
|
|
290
312
|
type Dict<V = any> = Record<Exclude<string, number | symbol>, V>;
|
|
291
|
-
type Union<T> = T extends infer O ? {
|
|
292
|
-
[K in keyof O]: O[K];
|
|
293
|
-
} : never;
|
|
294
313
|
type RecordValues<R extends Record<string, any>> = R[keyof R];
|
|
295
314
|
type RecordPrefix<P extends string, R extends Record<string, any>> = {
|
|
296
315
|
[K in keyof R as K extends `${P}/${infer S}` ? S : never]: R[K];
|
|
@@ -384,7 +403,6 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
384
403
|
meta: FinalMeta;
|
|
385
404
|
context: Fallback<Context, typeof this>;
|
|
386
405
|
message: TypedFastEventMessageOptional<Events, FinalMeta>;
|
|
387
|
-
listeners: FastEventListeners<Events, FinalMeta>;
|
|
388
406
|
anyListener: TypedFastEventAnyListener<Events, FinalMeta, Fallback<Context, typeof this>>;
|
|
389
407
|
};
|
|
390
408
|
prefix: string;
|
|
@@ -416,14 +434,14 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
416
434
|
on<T extends Exclude<string, Types>>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
417
435
|
on(type: '**', options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
418
436
|
on<T extends keyof OmitTransformedEvents<Events>>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
419
|
-
on<T extends keyof PickTransformedEvents<Events>>(type: T, listener: (message: PickPayload<RecordValues<
|
|
420
|
-
on<T extends Exclude<string, Types>>(type: T, listener: TypedFastEventAnyListener<
|
|
437
|
+
on<T extends keyof PickTransformedEvents<Events>>(type: T, listener: (message: PickPayload<RecordValues<ClosestWildcardEvents<Events, Exclude<T, number | symbol>>>>, args: FastEventListenerArgs<Meta>) => any | Promise<any>, options?: FastEventListenOptions<Events, Meta>): FastEventSubscriber;
|
|
438
|
+
on<T extends Exclude<string, Types>>(type: T, listener: TypedFastEventAnyListener<ClosestWildcardEvents<Events, T>, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
421
439
|
on(type: '**', listener: TypedFastEventAnyListener<Events, FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
422
440
|
once<T extends Types = Types>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
423
441
|
once<T extends Exclude<string, Types>>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
424
442
|
once<T extends keyof OmitTransformedEvents<Events>>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
425
|
-
once<T extends keyof PickTransformedEvents<Events>>(type: T, listener: (message: PickPayload<RecordValues<
|
|
426
|
-
once<T extends Exclude<string, Types>>(type: T, listener: TypedFastEventAnyListener<
|
|
443
|
+
once<T extends keyof PickTransformedEvents<Events>>(type: T, listener: (message: PickPayload<RecordValues<WildcardEvents<Events, Exclude<T, number | symbol>>>>, args: FastEventListenerArgs<Meta>) => any | Promise<any>, options?: FastEventListenOptions<Events, Meta>): FastEventSubscriber;
|
|
444
|
+
once<T extends Exclude<string, Types>>(type: T, listener: TypedFastEventAnyListener<WildcardEvents<Events, T>, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
427
445
|
onAny(options?: Pick<FastEventListenOptions, 'prepend'>): FastEventSubscriber;
|
|
428
446
|
onAny<P = any>(listener: TypedFastEventAnyListener<{
|
|
429
447
|
[K: string]: P;
|
|
@@ -438,8 +456,8 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
438
456
|
emit(type: Types, directive: symbol): void;
|
|
439
457
|
emit(type: string, directive: symbol): void;
|
|
440
458
|
emit<R = any, T extends Types = Types>(type: T, payload?: PickPayload<Events[T]>, retain?: boolean): R[];
|
|
441
|
-
emit<R = any, T extends string = string>(type: T, payload?: PickPayload<T extends Types ? Events[T] : RecordValues<
|
|
442
|
-
emit<R = any, T extends string = string>(type: T, payload?: PickPayload<T extends Types ? Events[T] : RecordValues<
|
|
459
|
+
emit<R = any, T extends string = string>(type: T, payload?: PickPayload<T extends Types ? Events[T] : RecordValues<WildcardEvents<Events, T>>>, retain?: boolean): R[];
|
|
460
|
+
emit<R = any, T extends string = string>(type: T, payload?: PickPayload<T extends Types ? Events[T] : RecordValues<WildcardEvents<Events, T>>>, options?: FastEventListenerArgs<FinalMeta>): R[];
|
|
443
461
|
emit<R = any>(message: FastEventEmitMessage<AtPayloads<Events>, FinalMeta>, options?: FastEventListenerArgs<FinalMeta>): R[];
|
|
444
462
|
emit<R = any, T extends string = string>(message: FastEventEmitMessage<{
|
|
445
463
|
[K in T]: PickPayload<K extends Types ? Events[K] : any>;
|
|
@@ -494,7 +512,7 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
494
512
|
* ```
|
|
495
513
|
*/
|
|
496
514
|
scope<E extends Record<string, any> = Record<string, any>, P extends string = string, M extends Record<string, any> = Record<string, any>, C = Context>(prefix: P, options?: DeepPartial<FastEventScopeOptions<Partial<FinalMeta> & M, C>>): FastEventScope<ScopeEvents<Events, P> & E, FinalMeta & M, C>;
|
|
497
|
-
scope<E extends Record<string, any> = Record<string, any>, P extends string = string, M extends Record<string, any> = Record<string, any>, C = Context, ScopeInstance extends FastEventScope<
|
|
515
|
+
scope<E extends Record<string, any> = Record<string, any>, P extends string = string, M extends Record<string, any> = Record<string, any>, C = Context, ScopeInstance extends FastEventScope<Record<string, any>, any, any> = FastEventScope<Record<string, any>, any, any>>(prefix: P, scopeObj: ScopeInstance, options?: DeepPartial<FastEventScopeOptions<Partial<FinalMeta> & M, C>>): ScopeInstance & FastEventScope<ScopeEvents<Events, P> & E, FinalMeta & M, C>;
|
|
498
516
|
/**
|
|
499
517
|
* 当on/once/onAny未指定监听器时,此为默认监听器
|
|
500
518
|
* @param message
|
|
@@ -616,8 +634,8 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
616
634
|
on<T extends string>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
617
635
|
on(type: '**', options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
618
636
|
on<T extends keyof OmitTransformedEvents<AllEvents>>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, AllEvents[T], Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
619
|
-
on<T extends keyof PickTransformedEvents<AllEvents>>(type: T, listener: (message: PickPayload<RecordValues<
|
|
620
|
-
on<T extends Exclude<string, Types>>(type: T, listener: TypedFastEventAnyListener<
|
|
637
|
+
on<T extends keyof PickTransformedEvents<AllEvents>>(type: T, listener: (message: PickPayload<RecordValues<ClosestWildcardEvents<AllEvents, Exclude<T, number | symbol>>>>, args: FastEventListenerArgs<Meta>) => any | Promise<any>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
638
|
+
on<T extends Exclude<string, Types>>(type: T, listener: TypedFastEventAnyListener<ClosestWildcardEvents<AllEvents, T>, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
621
639
|
on(type: '**', listener: TypedFastEventAnyListener<Record<string, any>, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
622
640
|
/**
|
|
623
641
|
* 注册一次性事件监听器
|
|
@@ -643,8 +661,8 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
643
661
|
once<T extends Types = Types>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
644
662
|
once<T extends string>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
645
663
|
once<T extends keyof OmitTransformedEvents<AllEvents>>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, AllEvents[T], Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
646
|
-
once<T extends keyof PickTransformedEvents<AllEvents>>(type: T, listener: (message: PickPayload<RecordValues<
|
|
647
|
-
once<T extends Exclude<string, Types>>(type: T, listener: TypedFastEventAnyListener<
|
|
664
|
+
once<T extends keyof PickTransformedEvents<AllEvents>>(type: T, listener: (message: PickPayload<RecordValues<ClosestWildcardEvents<AllEvents, Exclude<T, number | symbol>>>>, args: FastEventListenerArgs<Meta>) => any | Promise<any>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
665
|
+
once<T extends Exclude<string, Types>>(type: T, listener: TypedFastEventAnyListener<ClosestWildcardEvents<AllEvents, T>, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
648
666
|
/**
|
|
649
667
|
* 注册一个监听器,用于监听所有事件
|
|
650
668
|
* @param listener 事件监听器函数,可以接收任意类型的事件数据
|
|
@@ -834,13 +852,13 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
834
852
|
emit<T extends Types = Types>(type: T, directive: symbol): any[];
|
|
835
853
|
emit(type: string, directive: symbol): any[];
|
|
836
854
|
emit<R = any, T extends Types = Types>(type: T, payload?: PickPayload<AllEvents[T]>, retain?: boolean): R[];
|
|
837
|
-
emit<R = any, T extends string = string>(type: T, payload: PickPayload<T extends Types ? AllEvents[T] : RecordValues<
|
|
855
|
+
emit<R = any, T extends string = string>(type: T, payload: PickPayload<T extends Types ? AllEvents[T] : RecordValues<WildcardEvents<AllEvents, T>>>, retain?: boolean): R[];
|
|
838
856
|
emit<R = any, T extends string = string>(message: FastEventEmitMessage<{
|
|
839
857
|
[K in T]: K extends Types ? AllEvents[K] : any;
|
|
840
858
|
}, Meta>, retain?: boolean): R[];
|
|
841
859
|
emit<R = any>(message: FastEventEmitMessage<AllEvents, Meta>, retain?: boolean): R[];
|
|
842
860
|
emit<R = any, T extends Types = Types>(type: T, payload?: PickPayload<AllEvents[T]>, options?: FastEventListenerArgs<Meta>): R[];
|
|
843
|
-
emit<R = any, T extends string = string>(type: T, payload: PickPayload<T extends Types ? AllEvents[T] : RecordValues<
|
|
861
|
+
emit<R = any, T extends string = string>(type: T, payload: PickPayload<T extends Types ? AllEvents[T] : RecordValues<WildcardEvents<AllEvents, T>>>, options?: FastEventListenerArgs<Meta>): R[];
|
|
844
862
|
emit<R = any, T extends string = string>(message: FastEventEmitMessage<{
|
|
845
863
|
[K in T]: PickPayload<K extends Types ? AllEvents[K] : any>;
|
|
846
864
|
}, Meta>, options?: FastEventListenerArgs<Meta>): R[];
|
|
@@ -975,7 +993,7 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
975
993
|
*/
|
|
976
994
|
scope<P extends string>(prefix: P, options?: DeepPartial<FastEventScopeOptions<Meta, Context>>): FastEventScope<ScopeEvents<AllEvents, P>, Meta, Context>;
|
|
977
995
|
scope<E extends Record<string, any> = Record<string, any>, P extends string = string, M extends Record<string, any> = Record<string, any>, C = Context>(prefix: P, options?: DeepPartial<FastEventScopeOptions<Meta & M, C>>): FastEventScope<ScopeEvents<AllEvents, P> & E, Meta & M, C>;
|
|
978
|
-
scope<E extends Record<string, any> = Record<string, any>, P extends string = string, M extends Record<string, any> = Record<string, any>, C = Context, ScopeObject extends FastEventScope<any, any, any> = FastEventScope<
|
|
996
|
+
scope<E extends Record<string, any> = Record<string, any>, P extends string = string, M extends Record<string, any> = Record<string, any>, C = Context, ScopeObject extends FastEventScope<any, any, any> = FastEventScope<any, any, any>>(prefix: P, scopeObj: ScopeObject, options?: DeepPartial<FastEventScopeOptions<Meta & M, C>>): ScopeObject & FastEventScope<ScopeEvents<AllEvents, P> & E, Meta & M, C>;
|
|
979
997
|
}
|
|
980
998
|
|
|
981
999
|
type FastEventBusNodeOptions<Meta = Record<string, any>, Context = any> = FastEventOptions<Meta, Context> & {};
|
|
@@ -1234,4 +1252,4 @@ declare function isClass(target: unknown): target is new (...args: any[]) => any
|
|
|
1234
1252
|
|
|
1235
1253
|
declare function isFastEvent(target: any): target is FastEvent;
|
|
1236
1254
|
|
|
1237
|
-
export { AbortError, type AssertFastMessage, type AtPayloads, BroadcastEvent, CancelError, type ChangeFieldType, type DeepPartial, type Dict, type Expand, type ExpandWildcard, 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, FastEventListenerFlags, 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 FastMessagePayload, type
|
|
1255
|
+
export { AbortError, type AssertFastMessage, type AtPayloads, BroadcastEvent, CancelError, type ChangeFieldType, type ClosestWildcardEvents, type DeepPartial, type Dict, type Expand, type ExpandWildcard, 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, FastEventListenerFlags, 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 FastMessagePayload, type Keys, type Merge, type MergeUnion, NamespaceDelimiter, NodeDataEvent, type NotPayload, type ObjectKeys, type OmitTransformedEvents, type OptionalItems, type Overloads, type OverrideOptions, type PickPayload, type PickTransformedEvents, QueueOverflowError, type RecordPrefix, type RecordValues, type RequiredItems, type ScopeEvents, TimeoutError, type TransformedEvents, type TypedFastEventAnyListener, type TypedFastEventListener, type TypedFastEventMessage, type TypedFastEventMessageOptional, UnboundError, type Unique, type WildcardEvents, __FastEventScope__, __FastEvent__, __expandable__, expandable, isClass, isExpandable, isFastEvent, isFastEventMessage, isFastEventScope, isFunction, isPathMatched, isString, isSubsctiber };
|
package/dist/eventbus/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
'use strict';var B=Object.defineProperty;var tt=(i,t,e)=>t in i?B(i,t,{enumerable:true,configurable:true,writable:true,value:e}):i[t]=e;var a=(i,t)=>B(i,"name",{value:t,configurable:true});var h=(i,t,e)=>tt(i,typeof t!="symbol"?t+"":t,e);var st=Symbol.for("__FastEvent__"),it=Symbol.for("__FastEventScope__"),$=class $ extends Error{constructor(t){super(t);}};a($,"FastEventError");var m=$,F=class F extends m{};a(F,"TimeoutError");var V=F,j=class j extends m{};a(j,"UnboundError");var v=j,O=class O extends m{};a(O,"AbortError");var x=O,R=class R extends m{};a(R,"CancelError");var E=R,P=class P extends m{};a(P,"QueueOverflowError");var I=P,d={clearRetain:Symbol.for("ClearRetain")};var _=function(i){return i[i.Transformed=1]="Transformed",i}({});function L(i,t,e,s){let n,r={},o={};return typeof i[0]=="object"?(Object.assign(o,i[0]),r=typeof i[1]=="boolean"?{retain:i[1]}:i[1]||{},n=i[0].meta):(o.type=i[0],o.payload=i[1],r=typeof i[2]=="boolean"?{retain:i[2]}:i[2]||{}),n=Object.assign({},t,e,r.meta,n),Object.keys(n).length===0&&(n=void 0),o.meta=n,r.executor===void 0&&(r.executor=s),[o,r]}a(L,"parseEmitArgs");function X(i){return i?typeof i=="object"&&"__FastEventScope__"in i:false}a(X,"isFastEventScope");function T(i,t,e){let s=i[0],n=X(i[1])?i[1]:void 0,r=(n?i[2]:i[1])||{};return r.meta=Object.assign({},t,r?.meta),r.context=r.context!==void 0?r.context:e,[s,n,r]}a(T,"parseScopeArgs");function b(i,t){return Object.defineProperty(i,"name",{value:t||"anonymous",configurable:true}),i}a(b,"renameFn");function c(i){return i&&typeof i=="function"}a(c,"isFunction");var M=class M{constructor(t){h(this,"__FastEventScope__",true);h(this,"_options",{});h(this,"types",{events:void 0,meta:void 0,context:void 0,message:void 0,listeners:void 0,anyListener:void 0});h(this,"prefix","");h(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 b(function(r,o){let f=o.rawEventType||r.type;if(f.startsWith(e)){let u=((o.flags||0)&_.Transformed)>0?r:Object.assign({},r,{type:f.substring(e.length)});return t.call(s.context,u,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 v;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]=L(arguments,this.emitter.options.meta,this.options.meta,this.options.executor);return t.type=this._getScopeType(t.type),this._transformMessage(t,e),this.emitter.emit(t,e)}_transformMessage(t,e){return c(this._options.transform)&&(e.rawEventType=t.type,e.flags=(e.flags||0)|_.Transformed,t.payload=this._options.transform.call(this,t)),t}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]=T(arguments,this.options.meta,this.options.context),n;return e?n=e:n=new M,n.bind(this.emitter,this.prefix+t,s),n}onMessage(t,e){}};a(M,"FastEventScope");var S=M;function C(i,t){if(i.length!==t.length&&i.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:i.length-t.length+1}).fill("*"));for(let s=0;s<i.length;s++)if(e[s]!=="*"&&e[s]!==i[s])return false;return true}a(C,"isPathMatched");function q(i,t){let e=[];for(;;){let s=i.findIndex(n=>t(n));if(s===-1){e.push(s);break}i.splice(s,1);}return e}a(q,"removeItem");var z=Symbol.for("__expandable__");function G(i){return i[z]=true,i}a(G,"expandable");function H(i){return i&&i[z]}a(H,"isExpandable");function J(i){for(let t=0;t<i.length;t++){let e=i[t];Array.isArray(e)&&H(e)&&(i.splice(t,1,...e),t+=e.length-1);}return i}a(J,"expandEmitResults");function K(i){return i&&typeof i=="object"&&"off"in i&&"listener"in i}a(K,"isSubsctiber");function Y(i,t){return i.catch(e=>(t&&t(e),Promise.resolve(e)))}a(Y,"tryReturnError");var N=class N{constructor(t){h(this,"__FastEvent__",true);h(this,"listeners",{__listeners:[]});h(this,"_options");h(this,"_delimiter","/");h(this,"_context");h(this,"retainedMessages",new Map);h(this,"listenerCount",0);h(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(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:n,prepend:r}=s,o=0;return [this._forEachNodes(t,p=>{let u=[e,n,0,s.tag,s.flags];r?(p.__listeners.splice(0,0,u),o=0):(p.__listeners.push(u),o=p.__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 n=0;n<t.length;n++){let r=t[n];if(r in s||(s[r]={__listeners:[]}),n===t.length-1){let o=s[r];return e(o,s),o}else s=s[r];}}_removeListener(t,e,s){s&&q(t.__listeners,n=>{n=Array.isArray(n)?n[0]:n;let r=n===s;return r&&(this.listenerCount--,c(this._options.onRemoveListener)&&this._options.onRemoveListener(e.join(this._delimiter),s)),r});}_pipeListener(t,e){return e.forEach(s=>{t=b(s(t),t.name);}),t}on(){let t=arguments[0],e=c(arguments[1])?arguments[1]:(this._options.onMessage||this.onMessage).bind(this),s=Object.assign({count:0,flags:0,prepend:false},c(arguments[1])?arguments[2]:arguments[1]);if(t.length===0)throw new Error("event type cannot be empty");if(c(this._options.onAddListener)){let p=this._options.onAddListener(t,e,s);if(p===false)throw new E;if(K(p))return p}let n=t.split(this._delimiter);if(s.pipes&&s.pipes.length>0&&(e=this._pipeListener(e,s.pipes)),c(s.filter)||c(s.off)){let p=e;e=b(function(u,l){if(c(s.off)&&s.off.call(this,u,l)){f();return}if(c(s.filter)){if(s.filter.call(this,u,l))return p.call(this,u,l)}else return p.call(this,u,l)},e.name);}let[r,o]=this._addListener(n,e,s),f=a(()=>r&&this._removeListener(r,n,e),"off");return this._emitRetainMessage(t,r,o),{off:f,listener:e}}once(){return c(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=c(t[0])?void 0:t[0],s=c(t[0])?t[0]:t[1],n=e?e.split(this._delimiter):[],r=e?e.includes("*"):false;if(e&&!r)this._traverseToPath(this.listeners,n,o=>{s?this._removeListener(o,n,s):e&&(o.__listeners=[]);});else {let o=r?[]:n;this._traverseListeners(this.listeners,o,(f,p)=>{(s!==void 0||r&&C(f,n))&&(s?this._removeListener(p,n,s):p.__listeners=[]);});}}offAll(t){if(t){let e=t.split(this._delimiter),s=0;this._traverseListeners(this.listeners,e,(n,r)=>{s+=r.__listeners.length,r.__listeners=[];}),this.listenerCount-=s,this._removeRetainedEvents(t);}else {let e=0;this._traverseListeners(this.listeners,[],(s,n)=>{e+=n.__listeners.length;}),this.listenerCount-=e,this.retainedMessages.clear(),this.listeners={__listeners:[]};}c(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 n=[];if(t.includes("*")){let r=t.split(this._delimiter);this.retainedMessages.forEach((o,f)=>{let p=f.split(this._delimiter);C(p,r)&&n.push(o);});}else this.retainedMessages.has(t)&&n.push(this.retainedMessages.get(t));e&&n.forEach(r=>{this._executeListeners([e],r,{},o=>o[0]===e.__listeners[s][0]);});}_traverseToPath(t,e,s,n=0,r){if(n>=e.length){s(t);return}let o=e[n];if(r===true){this._traverseToPath(t,e,s,n+1,true);return}"*"in t&&this._traverseToPath(t["*"],e,s,n+1),"**"in t&&this._traverseToPath(t["**"],e,s,n+1,true),o in t&&this._traverseToPath(t[o],e,s,n+1);}_traverseListeners(t,e,s){let n=t;e&&e.length>0&&this._traverseToPath(t,e,o=>{n=o;});let r=a((o,f,p)=>{f(p,o);for(let[u,l]of Object.entries(o))u.startsWith("__")||l&&r(l,f,[...p,u]);},"traverseNodes");r(n,s,[]);}_onListenerError(t,e,s,n){if(n instanceof Error&&(n._emitter=`${t.name||"anonymous"}:${e.type}`),c(this._options.onListenerError))try{this._options.onListenerError.call(this,n,t,e,s);}catch{}if(this._options.ignoreErrors)return n;throw n}_setListenerFlags(t,e){return !t||t===0?e:t|e}_executeListener(t,e,s,n=false){try{if(s&&s.abortSignal&&s.abortSignal.aborted)return this._onListenerError(t,e,s,new x(t.name));let r=((s?.flags||0)&_.Transformed)>0,o=t.call(this.context,r?e.payload:e,s);return n&&o&&o instanceof Promise&&(o=Y(o,f=>this._onListenerError(t,e,s,f))),o}catch(r){return this._onListenerError(t,e,s,r)}}_getListenerExecutor(t){if(!t)return;let e=t.executor||this._options.executor;if(c(e))return e}_executeListeners(t,e,s,n){if(!t||t.length===0)return [];let r=t.reduce((f,p)=>f.concat(p.__listeners.filter(u=>c(n)?n(u,p):true).map((u,l)=>[u,l,p.__listeners])),[]);c(this._options.transform)&&(s||(s={}),s.rawEventType=e.type,e.payload=this._options.transform.call(this,e),s.flags=this._setListenerFlags(s.flags,_.Transformed)),this._decListenerExecCount(r);let o=this._getListenerExecutor(s);if(o){let f=o(r.map(p=>p[0]),e,s,this._executeListener.bind(this));return Array.isArray(f)?f:[f]}else return r.map(f=>this._executeListener(f[0][0],e,s,true))}_decListenerExecCount(t){for(let e=t.length-1;e>=0;e--){let s=t[e][0];s[2]++,s[1]>0&&s[1]<=s[2]&&t[e][2].splice(e,1);}}getListeners(t){let e=[],s=t.split(this._delimiter);return this._traverseToPath(this.listeners,s,n=>{e.push(n);}),e[0].__listeners}emit(){if(arguments.length===2&&typeof arguments[0]=="string"&&arguments[1]===d.clearRetain)return this.retainedMessages.delete(arguments[0]),[];let[t,e]=L(arguments,this.options.meta);c(e.parseArgs)&&e.parseArgs(t,e);let s=t.type.split(this._delimiter);e.retain&&this.retainedMessages.set(t.type,t);let n=[],r=[];if(this._traverseToPath(this.listeners,s,o=>{r.push(o);}),c(this._options.onBeforeExecuteListener)){let o=this._options.onBeforeExecuteListener.call(this,t,e);if(Array.isArray(o))return o;if(o===false)throw new x(t.type)}return c(this._options.transform)&&(t.payload=this._options.transform.call(this,t),e.rawEventType=t.type,e.flags=(e.flags||0)|_.Transformed),n.push(...this._executeListeners(r,t,e)),c(this._options.onAfterExecuteListener)&&this._options.onAfterExecuteListener.call(this,t,n,r),this._options.expandEmitResults&&J(n),n}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,n)=>{let r,o,f=a(p=>{clearTimeout(r),o&&o.off(),s(p);},"listener");e&&e>0&&(r=setTimeout(()=>{o&&o.off(),n(new Error("wait for event<"+t+"> is timeout"));},e)),o=this.on(t,f);})}scope(){let[t,e,s]=T(arguments,this.options.meta,this.options.context),n;return e?n=e:n=new S,n.bind(this,t,s),n}};a(N,"FastEvent");var g=N;var y="::",w="@",Z="data";function k(i){return i?typeof i=="object"&&"type"in i:false}a(k,"isFastEventMessage");function A(i,t="/"){let e=k(i[0]),s=Object.assign({payload:e?i[0].payload:i[1]},e?i[0]:{},{type:`${w}${t}${e?i[0].type:"data"}`}),n=e?i[1]:i[2];return [s,n]}a(A,"parseBroadcaseArgs");var D=class D extends g{constructor(e){super(e);h(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]=A(arguments,this.options.delimiter);return this.emit(e,s)}send(e,s){return e.type=`${e.to}${this.options.delimiter}${Z}`,this.emit(e,s)[0]}};a(D,"FastEventBus");var U=D;var W=class W extends g{constructor(e){super(e);h(this,"eventbus");h(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(y))return e.type=e.type.replace(y,this.eventbus.options.delimiter),e.from=this.id,this.eventbus.emit(e,s)}_onAddListener(e,s,n){if(e.includes(y)){let[r,o]=e.split(y);if(r===this.id)return;let f,p=this.eventbus.on(`$connect${this.options.delimiter}*`,u=>{if(u.payload===r){let l=this.eventbus.nodes.get(r);l&&(f=l.on(o,s,n)),p.off();}});return {off:a(()=>f?f.off():p.off(),"off"),listener:f?f.listener:p.listener}}}connect(e){this.eventbus=e,this.eventbus.add(this),this._subscribers.push(this.eventbus.on(`${w}${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,n)=>(s.type=s.type.substring(e.length),G(this.emit(s,n)))));}send(e,s,n){this._assertConnected();let r={type:"data",from:this.id,to:e,payload:s};return this.eventbus.send(r,n)}_assertConnected(){if(!this.eventbus)throw new Error("Node is not connected to any event bus")}broadcast(){this._assertConnected();let[e,s]=A(arguments,this.options.delimiter);return e.from=this.id,this.eventbus.broadcast(e,s)}onMessage(e,s){}};a(W,"FastEventBusNode");var Q=W;function ve(i){return i&&typeof i=="string"}a(ve,"isString");function Te(i){return typeof i=="function"&&(i.toString().startsWith("class ")||i.prototype?.constructor===i)}a(Te,"isClass");function we(i){return i?typeof i=="object"&&"__FastEvent__"in i:false}a(we,"isFastEvent");exports.AbortError=x;exports.BroadcastEvent=w;exports.CancelError=E;exports.FastEvent=g;exports.FastEventBus=U;exports.FastEventBusNode=Q;exports.FastEventDirectives=d;exports.FastEventError=m;exports.FastEventListenerFlags=_;exports.FastEventScope=S;exports.NamespaceDelimiter=y;exports.NodeDataEvent=Z;exports.QueueOverflowError=I;exports.TimeoutError=V;exports.UnboundError=v;exports.__FastEventScope__=it;exports.__FastEvent__=st;exports.__expandable__=z;exports.expandable=G;exports.isClass=Te;exports.isExpandable=H;exports.isFastEvent=we;exports.isFastEventMessage=k;exports.isFastEventScope=X;exports.isFunction=c;exports.isPathMatched=C;exports.isString=ve;exports.isSubsctiber=K;//# sourceMappingURL=index.js.map
|
|
1
|
+
'use strict';var B=Object.defineProperty;var tt=(i,t,e)=>t in i?B(i,t,{enumerable:true,configurable:true,writable:true,value:e}):i[t]=e;var a=(i,t)=>B(i,"name",{value:t,configurable:true});var h=(i,t,e)=>tt(i,typeof t!="symbol"?t+"":t,e);var st=Symbol.for("__FastEvent__"),it=Symbol.for("__FastEventScope__"),$=class $ extends Error{constructor(t){super(t);}};a($,"FastEventError");var m=$,F=class F extends m{};a(F,"TimeoutError");var V=F,j=class j extends m{};a(j,"UnboundError");var v=j,O=class O extends m{};a(O,"AbortError");var x=O,R=class R extends m{};a(R,"CancelError");var E=R,P=class P extends m{};a(P,"QueueOverflowError");var I=P,d={clearRetain:Symbol.for("ClearRetain")};var _=function(i){return i[i.Transformed=1]="Transformed",i}({});function L(i,t,e,s){let n,r={},o={};return typeof i[0]=="object"?(Object.assign(o,i[0]),r=typeof i[1]=="boolean"?{retain:i[1]}:i[1]||{},n=i[0].meta):(o.type=i[0],o.payload=i[1],r=typeof i[2]=="boolean"?{retain:i[2]}:i[2]||{}),n=Object.assign({},t,e,r.meta,n),Object.keys(n).length===0&&(n=void 0),o.meta=n,r.executor===void 0&&(r.executor=s),[o,r]}a(L,"parseEmitArgs");function X(i){return i?typeof i=="object"&&"__FastEventScope__"in i:false}a(X,"isFastEventScope");function T(i,t,e){let s=i[0],n=X(i[1])?i[1]:void 0,r=(n?i[2]:i[1])||{};return r.meta=Object.assign({},t,r?.meta),r.context=r.context!==void 0?r.context:e,[s,n,r]}a(T,"parseScopeArgs");function b(i,t){return Object.defineProperty(i,"name",{value:t||"anonymous",configurable:true}),i}a(b,"renameFn");function c(i){return i&&typeof i=="function"}a(c,"isFunction");var M=class M{constructor(t){h(this,"__FastEventScope__",true);h(this,"_options",{});h(this,"types",{events:void 0,meta:void 0,context:void 0,message:void 0,anyListener:void 0});h(this,"prefix","");h(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 b(function(r,o){let p=o.rawEventType||r.type;if(p.startsWith(e)){let u=((o.flags||0)&_.Transformed)>0?r:Object.assign({},r,{type:p.substring(e.length)});return t.call(s.context,u,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 v;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]=L(arguments,this.emitter.options.meta,this.options.meta,this.options.executor);return this._transformMessage(t,e),t.type=this._getScopeType(t.type),this.emitter.emit(t,e)}_transformMessage(t,e){c(this._options.transform)&&(e.rawEventType=this._getScopeType(t.type),e.flags=(e.flags||0)|_.Transformed,t.payload=this._options.transform.call(this,t));}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]=T(arguments,this.options.meta,this.options.context),n;return e?n=e:n=new M,n.bind(this.emitter,this.prefix+t,s),n}onMessage(t,e){}};a(M,"FastEventScope");var S=M;function C(i,t){if(i.length!==t.length&&i.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:i.length-t.length+1}).fill("*"));for(let s=0;s<i.length;s++)if(e[s]!=="*"&&e[s]!==i[s])return false;return true}a(C,"isPathMatched");function q(i,t){let e=[];for(;;){let s=i.findIndex(n=>t(n));if(s===-1){e.push(s);break}i.splice(s,1);}return e}a(q,"removeItem");var z=Symbol.for("__expandable__");function G(i){return i[z]=true,i}a(G,"expandable");function H(i){return i&&i[z]}a(H,"isExpandable");function J(i){for(let t=0;t<i.length;t++){let e=i[t];Array.isArray(e)&&H(e)&&(i.splice(t,1,...e),t+=e.length-1);}return i}a(J,"expandEmitResults");function K(i){return i&&typeof i=="object"&&"off"in i&&"listener"in i}a(K,"isSubsctiber");function Y(i,t){return i.catch(e=>(t&&t(e),Promise.resolve(e)))}a(Y,"tryReturnError");var N=class N{constructor(t){h(this,"__FastEvent__",true);h(this,"listeners",{__listeners:[]});h(this,"_options");h(this,"_delimiter","/");h(this,"_context");h(this,"retainedMessages",new Map);h(this,"listenerCount",0);h(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(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:n,prepend:r}=s,o=0;return [this._forEachNodes(t,f=>{let u=[e,n,0,s.tag,s.flags];r?(f.__listeners.splice(0,0,u),o=0):(f.__listeners.push(u),o=f.__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 n=0;n<t.length;n++){let r=t[n];if(r in s||(s[r]={__listeners:[]}),n===t.length-1){let o=s[r];return e(o,s),o}else s=s[r];}}_removeListener(t,e,s){s&&q(t.__listeners,n=>{n=Array.isArray(n)?n[0]:n;let r=n===s;return r&&(this.listenerCount--,c(this._options.onRemoveListener)&&this._options.onRemoveListener(e.join(this._delimiter),s)),r});}_pipeListener(t,e){return e.forEach(s=>{t=b(s(t),t.name);}),t}on(){let t=arguments[0],e=c(arguments[1])?arguments[1]:(this._options.onMessage||this.onMessage).bind(this),s=Object.assign({count:0,flags:0,prepend:false},c(arguments[1])?arguments[2]:arguments[1]);if(t.length===0)throw new Error("event type cannot be empty");if(c(this._options.onAddListener)){let f=this._options.onAddListener(t,e,s);if(f===false)throw new E;if(K(f))return f}let n=t.split(this._delimiter);if(s.pipes&&s.pipes.length>0&&(e=this._pipeListener(e,s.pipes)),c(s.filter)||c(s.off)){let f=e;e=b(function(u,l){if(c(s.off)&&s.off.call(this,u,l)){p();return}if(c(s.filter)){if(s.filter.call(this,u,l))return f.call(this,u,l)}else return f.call(this,u,l)},e.name);}let[r,o]=this._addListener(n,e,s),p=a(()=>r&&this._removeListener(r,n,e),"off");return this._emitRetainMessage(t,r,o),{off:p,listener:e}}once(){return c(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=c(t[0])?void 0:t[0],s=c(t[0])?t[0]:t[1],n=e?e.split(this._delimiter):[],r=e?e.includes("*"):false;if(e&&!r)this._traverseToPath(this.listeners,n,o=>{s?this._removeListener(o,n,s):e&&(o.__listeners=[]);});else {let o=r?[]:n;this._traverseListeners(this.listeners,o,(p,f)=>{(s!==void 0||r&&C(p,n))&&(s?this._removeListener(f,n,s):f.__listeners=[]);});}}offAll(t){if(t){let e=t.split(this._delimiter),s=0;this._traverseListeners(this.listeners,e,(n,r)=>{s+=r.__listeners.length,r.__listeners=[];}),this.listenerCount-=s,this._removeRetainedEvents(t);}else {let e=0;this._traverseListeners(this.listeners,[],(s,n)=>{e+=n.__listeners.length;}),this.listenerCount-=e,this.retainedMessages.clear(),this.listeners={__listeners:[]};}c(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 n=[];if(t.includes("*")){let r=t.split(this._delimiter);this.retainedMessages.forEach((o,p)=>{let f=p.split(this._delimiter);C(f,r)&&n.push(o);});}else this.retainedMessages.has(t)&&n.push(this.retainedMessages.get(t));e&&n.forEach(r=>{this._executeListeners([e],r,{},o=>o[0]===e.__listeners[s][0]);});}_traverseToPath(t,e,s,n=0,r){if(n>=e.length){s(t);return}let o=e[n];if(r===true){this._traverseToPath(t,e,s,n+1,true);return}"*"in t&&this._traverseToPath(t["*"],e,s,n+1),"**"in t&&this._traverseToPath(t["**"],e,s,n+1,true),o in t&&this._traverseToPath(t[o],e,s,n+1);}_traverseListeners(t,e,s){let n=t;e&&e.length>0&&this._traverseToPath(t,e,o=>{n=o;});let r=a((o,p,f)=>{p(f,o);for(let[u,l]of Object.entries(o))u.startsWith("__")||l&&r(l,p,[...f,u]);},"traverseNodes");r(n,s,[]);}_onListenerError(t,e,s,n){if(n instanceof Error&&(n._emitter=`${t.name||"anonymous"}:${e.type}`),c(this._options.onListenerError))try{this._options.onListenerError.call(this,n,t,e,s);}catch{}if(this._options.ignoreErrors)return n;throw n}_setListenerFlags(t,e){return !t||t===0?e:t|e}_executeListener(t,e,s,n=false){try{if(s&&s.abortSignal&&s.abortSignal.aborted)return this._onListenerError(t,e,s,new x(t.name));let r=((s?.flags||0)&_.Transformed)>0,o=t.call(this.context,r?e.payload:e,s);return n&&o&&o instanceof Promise&&(o=Y(o,p=>this._onListenerError(t,e,s,p))),o}catch(r){return this._onListenerError(t,e,s,r)}}_getListenerExecutor(t){if(!t)return;let e=t.executor||this._options.executor;if(c(e))return e}_executeListeners(t,e,s,n){if(!t||t.length===0)return [];let r=t.reduce((p,f)=>p.concat(f.__listeners.filter(u=>c(n)?n(u,f):true).map((u,l)=>[u,l,f.__listeners])),[]);c(this._options.transform)&&(s||(s={}),s.rawEventType=e.type,e.payload=this._options.transform.call(this,e),s.flags=this._setListenerFlags(s.flags,_.Transformed)),this._decListenerExecCount(r);let o=this._getListenerExecutor(s);if(o){let p=o(r.map(f=>f[0]),e,s,this._executeListener.bind(this));return Array.isArray(p)?p:[p]}else return r.map(p=>this._executeListener(p[0][0],e,s,true))}_decListenerExecCount(t){for(let e=t.length-1;e>=0;e--){let s=t[e][0];s[2]++,s[1]>0&&s[1]<=s[2]&&t[e][2].splice(e,1);}}getListeners(t){let e=[],s=t.split(this._delimiter);return this._traverseToPath(this.listeners,s,n=>{e.push(n);}),e[0].__listeners}emit(){if(arguments.length===2&&typeof arguments[0]=="string"&&arguments[1]===d.clearRetain)return this.retainedMessages.delete(arguments[0]),[];let[t,e]=L(arguments,this.options.meta);c(e.parseArgs)&&e.parseArgs(t,e);let s=t.type.split(this._delimiter);e.retain&&this.retainedMessages.set(t.type,t);let n=[],r=[];if(this._traverseToPath(this.listeners,s,o=>{r.push(o);}),c(this._options.onBeforeExecuteListener)){let o=this._options.onBeforeExecuteListener.call(this,t,e);if(Array.isArray(o))return o;if(o===false)throw new x(t.type)}return c(this._options.transform)&&(t.payload=this._options.transform.call(this,t),e.rawEventType=t.type,e.flags=(e.flags||0)|_.Transformed),n.push(...this._executeListeners(r,t,e)),c(this._options.onAfterExecuteListener)&&this._options.onAfterExecuteListener.call(this,t,n,r),this._options.expandEmitResults&&J(n),n}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,n)=>{let r,o,p=a(f=>{clearTimeout(r),o&&o.off(),s(f);},"listener");e&&e>0&&(r=setTimeout(()=>{o&&o.off(),n(new Error("wait for event<"+t+"> is timeout"));},e)),o=this.on(t,p);})}scope(){let[t,e,s]=T(arguments,this.options.meta,this.options.context),n;return e?n=e:n=new S,n.bind(this,t,s),n}};a(N,"FastEvent");var g=N;var y="::",w="@",Z="data";function k(i){return i?typeof i=="object"&&"type"in i:false}a(k,"isFastEventMessage");function A(i,t="/"){let e=k(i[0]),s=Object.assign({payload:e?i[0].payload:i[1]},e?i[0]:{},{type:`${w}${t}${e?i[0].type:"data"}`}),n=e?i[1]:i[2];return [s,n]}a(A,"parseBroadcaseArgs");var D=class D extends g{constructor(e){super(e);h(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]=A(arguments,this.options.delimiter);return this.emit(e,s)}send(e,s){return e.type=`${e.to}${this.options.delimiter}${Z}`,this.emit(e,s)[0]}};a(D,"FastEventBus");var U=D;var W=class W extends g{constructor(e){super(e);h(this,"eventbus");h(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(y))return e.type=e.type.replace(y,this.eventbus.options.delimiter),e.from=this.id,this.eventbus.emit(e,s)}_onAddListener(e,s,n){if(e.includes(y)){let[r,o]=e.split(y);if(r===this.id)return;let p,f=this.eventbus.on(`$connect${this.options.delimiter}*`,u=>{if(u.payload===r){let l=this.eventbus.nodes.get(r);l&&(p=l.on(o,s,n)),f.off();}});return {off:a(()=>p?p.off():f.off(),"off"),listener:p?p.listener:f.listener}}}connect(e){this.eventbus=e,this.eventbus.add(this),this._subscribers.push(this.eventbus.on(`${w}${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,n)=>(s.type=s.type.substring(e.length),G(this.emit(s,n)))));}send(e,s,n){this._assertConnected();let r={type:"data",from:this.id,to:e,payload:s};return this.eventbus.send(r,n)}_assertConnected(){if(!this.eventbus)throw new Error("Node is not connected to any event bus")}broadcast(){this._assertConnected();let[e,s]=A(arguments,this.options.delimiter);return e.from=this.id,this.eventbus.broadcast(e,s)}onMessage(e,s){}};a(W,"FastEventBusNode");var Q=W;function Ee(i){return i&&typeof i=="string"}a(Ee,"isString");function Se(i){return typeof i=="function"&&(i.toString().startsWith("class ")||i.prototype?.constructor===i)}a(Se,"isClass");function Ae(i){return i?typeof i=="object"&&"__FastEvent__"in i:false}a(Ae,"isFastEvent");exports.AbortError=x;exports.BroadcastEvent=w;exports.CancelError=E;exports.FastEvent=g;exports.FastEventBus=U;exports.FastEventBusNode=Q;exports.FastEventDirectives=d;exports.FastEventError=m;exports.FastEventListenerFlags=_;exports.FastEventScope=S;exports.NamespaceDelimiter=y;exports.NodeDataEvent=Z;exports.QueueOverflowError=I;exports.TimeoutError=V;exports.UnboundError=v;exports.__FastEventScope__=it;exports.__FastEvent__=st;exports.__expandable__=z;exports.expandable=G;exports.isClass=Se;exports.isExpandable=H;exports.isFastEvent=Ae;exports.isFastEventMessage=k;exports.isFastEventScope=X;exports.isFunction=c;exports.isPathMatched=C;exports.isString=Ee;exports.isSubsctiber=K;//# sourceMappingURL=index.js.map
|
|
2
2
|
//# sourceMappingURL=index.js.map
|