fastevent 2.3.3 → 2.3.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 +19 -3
- package/dist/eventbus/index.d.ts +19 -3
- 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 +20 -4
- package/dist/index.d.ts +20 -4
- 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
|
@@ -361,6 +361,8 @@ type TransformedEvents<Events extends Record<string, any>> = {
|
|
|
361
361
|
[K in keyof Events]: NotPayload<Events[K]>;
|
|
362
362
|
};
|
|
363
363
|
|
|
364
|
+
type Class = (new (...args: any[]) => any) | (abstract new (...args: any[]) => any);
|
|
365
|
+
|
|
364
366
|
type FastEventBusMessage<Events extends Record<string, any> = Record<string, any>, M = any> = FastEventEmitMessage<Events, M> & {
|
|
365
367
|
from?: string;
|
|
366
368
|
to?: string;
|
|
@@ -403,6 +405,7 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
403
405
|
meta: FinalMeta;
|
|
404
406
|
context: Fallback<Context, typeof this>;
|
|
405
407
|
message: TypedFastEventMessageOptional<Events, FinalMeta>;
|
|
408
|
+
listeners: FastEventListeners<Events, FinalMeta>;
|
|
406
409
|
anyListener: TypedFastEventAnyListener<Events, FinalMeta, Fallback<Context, typeof this>>;
|
|
407
410
|
};
|
|
408
411
|
prefix: string;
|
|
@@ -410,6 +413,11 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
410
413
|
constructor(options?: DeepPartial<FastEventScopeOptions<FinalMeta, Context>>);
|
|
411
414
|
get context(): Fallback<Context, typeof this>;
|
|
412
415
|
get options(): FastEventScopeOptions<FinalMeta, Context>;
|
|
416
|
+
/**
|
|
417
|
+
* 获取监听器
|
|
418
|
+
* @returns 监听器
|
|
419
|
+
*/
|
|
420
|
+
get listeners(): FastListenerMeta[];
|
|
413
421
|
bind(emitter: FastEvent<any>, prefix: string, options?: DeepPartial<FastEventScopeOptions<FinalMeta, Context>>): void;
|
|
414
422
|
/**
|
|
415
423
|
* 初始化选项
|
|
@@ -440,8 +448,8 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
440
448
|
once<T extends Types = Types>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
441
449
|
once<T extends Exclude<string, Types>>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
442
450
|
once<T extends keyof OmitTransformedEvents<Events>>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
443
|
-
once<T extends keyof PickTransformedEvents<Events>>(type: T, listener: (message: PickPayload<RecordValues<
|
|
444
|
-
once<T extends Exclude<string, Types>>(type: T, listener: TypedFastEventAnyListener<
|
|
451
|
+
once<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;
|
|
452
|
+
once<T extends Exclude<string, Types>>(type: T, listener: TypedFastEventAnyListener<ClosestWildcardEvents<Events, T>, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
445
453
|
onAny(options?: Pick<FastEventListenOptions, 'prepend'>): FastEventSubscriber;
|
|
446
454
|
onAny<P = any>(listener: TypedFastEventAnyListener<{
|
|
447
455
|
[K: string]: P;
|
|
@@ -512,6 +520,7 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
512
520
|
* ```
|
|
513
521
|
*/
|
|
514
522
|
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>;
|
|
523
|
+
scope<E extends Record<string, any> = Record<string, any>, P extends string = string, C = Context, ScopeObject extends InstanceType<Class> = InstanceType<Class>>(prefix: P, scopeObj: ScopeObject, options?: DeepPartial<FastEventScopeOptions<Meta>>): FastEventScopeExtend<Events, P, ScopeObject>;
|
|
515
524
|
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>;
|
|
516
525
|
/**
|
|
517
526
|
* 当on/once/onAny未指定监听器时,此为默认监听器
|
|
@@ -519,6 +528,7 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
519
528
|
*/
|
|
520
529
|
onMessage(message: TypedFastEventMessage<Events, FinalMeta>, args: FastEventListenerArgs<FinalMeta>): void;
|
|
521
530
|
}
|
|
531
|
+
type FastEventScopeExtend<Events extends Record<string, any>, Prefix extends string, T extends InstanceType<Class> = never> = FastEventScope<ScopeEvents<Events, Prefix>> & T;
|
|
522
532
|
|
|
523
533
|
/**
|
|
524
534
|
* FastEvent 事件发射器类
|
|
@@ -790,6 +800,11 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
790
800
|
* @param listeners
|
|
791
801
|
*/
|
|
792
802
|
_decListenerExecCount(listeners: [FastListenerMeta, number, FastListenerMeta[]][]): void;
|
|
803
|
+
/**
|
|
804
|
+
* 获取指定类型的所有事件监听器
|
|
805
|
+
* @param type - 事件类型,必须是 AllEvents 的键
|
|
806
|
+
* @returns 包含指定类型的所有监听器元数据的数组
|
|
807
|
+
*/
|
|
793
808
|
getListeners(type: keyof AllEvents): FastListenerMeta[];
|
|
794
809
|
/**
|
|
795
810
|
* 触发事件并执行对应的监听器
|
|
@@ -993,6 +1008,7 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
993
1008
|
*/
|
|
994
1009
|
scope<P extends string>(prefix: P, options?: DeepPartial<FastEventScopeOptions<Meta, Context>>): FastEventScope<ScopeEvents<AllEvents, P>, Meta, Context>;
|
|
995
1010
|
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>;
|
|
1011
|
+
scope<E extends Record<string, any> = Record<string, any>, P extends string = string, C = Context, ScopeObject extends InstanceType<Class> = InstanceType<Class>>(prefix: P, scopeObj: ScopeObject, options?: DeepPartial<FastEventScopeOptions<Meta>>): FastEventScopeExtend<AllEvents, P, ScopeObject>;
|
|
996
1012
|
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>;
|
|
997
1013
|
}
|
|
998
1014
|
|
|
@@ -1252,4 +1268,4 @@ declare function isClass(target: unknown): target is new (...args: any[]) => any
|
|
|
1252
1268
|
|
|
1253
1269
|
declare function isFastEvent(target: any): target is FastEvent;
|
|
1254
1270
|
|
|
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 };
|
|
1271
|
+
export { AbortError, type AssertFastMessage, type AtPayloads, BroadcastEvent, CancelError, type ChangeFieldType, type Class, 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 FastEventScopeExtend, 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
|
@@ -361,6 +361,8 @@ type TransformedEvents<Events extends Record<string, any>> = {
|
|
|
361
361
|
[K in keyof Events]: NotPayload<Events[K]>;
|
|
362
362
|
};
|
|
363
363
|
|
|
364
|
+
type Class = (new (...args: any[]) => any) | (abstract new (...args: any[]) => any);
|
|
365
|
+
|
|
364
366
|
type FastEventBusMessage<Events extends Record<string, any> = Record<string, any>, M = any> = FastEventEmitMessage<Events, M> & {
|
|
365
367
|
from?: string;
|
|
366
368
|
to?: string;
|
|
@@ -403,6 +405,7 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
403
405
|
meta: FinalMeta;
|
|
404
406
|
context: Fallback<Context, typeof this>;
|
|
405
407
|
message: TypedFastEventMessageOptional<Events, FinalMeta>;
|
|
408
|
+
listeners: FastEventListeners<Events, FinalMeta>;
|
|
406
409
|
anyListener: TypedFastEventAnyListener<Events, FinalMeta, Fallback<Context, typeof this>>;
|
|
407
410
|
};
|
|
408
411
|
prefix: string;
|
|
@@ -410,6 +413,11 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
410
413
|
constructor(options?: DeepPartial<FastEventScopeOptions<FinalMeta, Context>>);
|
|
411
414
|
get context(): Fallback<Context, typeof this>;
|
|
412
415
|
get options(): FastEventScopeOptions<FinalMeta, Context>;
|
|
416
|
+
/**
|
|
417
|
+
* 获取监听器
|
|
418
|
+
* @returns 监听器
|
|
419
|
+
*/
|
|
420
|
+
get listeners(): FastListenerMeta[];
|
|
413
421
|
bind(emitter: FastEvent<any>, prefix: string, options?: DeepPartial<FastEventScopeOptions<FinalMeta, Context>>): void;
|
|
414
422
|
/**
|
|
415
423
|
* 初始化选项
|
|
@@ -440,8 +448,8 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
440
448
|
once<T extends Types = Types>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
441
449
|
once<T extends Exclude<string, Types>>(type: T, options?: FastEventListenOptions<Events, FinalMeta>): FastEventSubscriber;
|
|
442
450
|
once<T extends keyof OmitTransformedEvents<Events>>(type: T, listener: TypedFastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
443
|
-
once<T extends keyof PickTransformedEvents<Events>>(type: T, listener: (message: PickPayload<RecordValues<
|
|
444
|
-
once<T extends Exclude<string, Types>>(type: T, listener: TypedFastEventAnyListener<
|
|
451
|
+
once<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;
|
|
452
|
+
once<T extends Exclude<string, Types>>(type: T, listener: TypedFastEventAnyListener<ClosestWildcardEvents<Events, T>, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
445
453
|
onAny(options?: Pick<FastEventListenOptions, 'prepend'>): FastEventSubscriber;
|
|
446
454
|
onAny<P = any>(listener: TypedFastEventAnyListener<{
|
|
447
455
|
[K: string]: P;
|
|
@@ -512,6 +520,7 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
512
520
|
* ```
|
|
513
521
|
*/
|
|
514
522
|
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>;
|
|
523
|
+
scope<E extends Record<string, any> = Record<string, any>, P extends string = string, C = Context, ScopeObject extends InstanceType<Class> = InstanceType<Class>>(prefix: P, scopeObj: ScopeObject, options?: DeepPartial<FastEventScopeOptions<Meta>>): FastEventScopeExtend<Events, P, ScopeObject>;
|
|
515
524
|
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>;
|
|
516
525
|
/**
|
|
517
526
|
* 当on/once/onAny未指定监听器时,此为默认监听器
|
|
@@ -519,6 +528,7 @@ declare class FastEventScope<Events extends Record<string, any> = Record<string,
|
|
|
519
528
|
*/
|
|
520
529
|
onMessage(message: TypedFastEventMessage<Events, FinalMeta>, args: FastEventListenerArgs<FinalMeta>): void;
|
|
521
530
|
}
|
|
531
|
+
type FastEventScopeExtend<Events extends Record<string, any>, Prefix extends string, T extends InstanceType<Class> = never> = FastEventScope<ScopeEvents<Events, Prefix>> & T;
|
|
522
532
|
|
|
523
533
|
/**
|
|
524
534
|
* FastEvent 事件发射器类
|
|
@@ -790,6 +800,11 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
790
800
|
* @param listeners
|
|
791
801
|
*/
|
|
792
802
|
_decListenerExecCount(listeners: [FastListenerMeta, number, FastListenerMeta[]][]): void;
|
|
803
|
+
/**
|
|
804
|
+
* 获取指定类型的所有事件监听器
|
|
805
|
+
* @param type - 事件类型,必须是 AllEvents 的键
|
|
806
|
+
* @returns 包含指定类型的所有监听器元数据的数组
|
|
807
|
+
*/
|
|
793
808
|
getListeners(type: keyof AllEvents): FastListenerMeta[];
|
|
794
809
|
/**
|
|
795
810
|
* 触发事件并执行对应的监听器
|
|
@@ -993,6 +1008,7 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
993
1008
|
*/
|
|
994
1009
|
scope<P extends string>(prefix: P, options?: DeepPartial<FastEventScopeOptions<Meta, Context>>): FastEventScope<ScopeEvents<AllEvents, P>, Meta, Context>;
|
|
995
1010
|
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>;
|
|
1011
|
+
scope<E extends Record<string, any> = Record<string, any>, P extends string = string, C = Context, ScopeObject extends InstanceType<Class> = InstanceType<Class>>(prefix: P, scopeObj: ScopeObject, options?: DeepPartial<FastEventScopeOptions<Meta>>): FastEventScopeExtend<AllEvents, P, ScopeObject>;
|
|
996
1012
|
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>;
|
|
997
1013
|
}
|
|
998
1014
|
|
|
@@ -1252,4 +1268,4 @@ declare function isClass(target: unknown): target is new (...args: any[]) => any
|
|
|
1252
1268
|
|
|
1253
1269
|
declare function isFastEvent(target: any): target is FastEvent;
|
|
1254
1270
|
|
|
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 };
|
|
1271
|
+
export { AbortError, type AssertFastMessage, type AtPayloads, BroadcastEvent, CancelError, type ChangeFieldType, type Class, 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 FastEventScopeExtend, 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,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
|
|
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}get listeners(){return this.emitter.getListeners(this.prefix)}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
|