fastevent 2.2.4 → 2.2.6
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 +89 -6
- package/dist/eventbus/index.d.ts +89 -6
- package/dist/eventbus/index.js +1 -1
- package/dist/eventbus/index.js.map +1 -1
- package/dist/eventbus/index.mjs +1 -1
- package/dist/eventbus/index.mjs.map +1 -1
- package/dist/executors/index.d.mts +2 -1
- package/dist/executors/index.d.ts +2 -1
- package/dist/executors/index.js +1 -1
- package/dist/executors/index.mjs +1 -1
- package/dist/index.d.mts +89 -6
- package/dist/index.d.ts +89 -6
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/pipes/index.d.mts +2 -1
- package/dist/pipes/index.d.ts +2 -1
- package/package.json +1 -1
|
@@ -46,9 +46,10 @@ type FastEventListener<P = any, M extends Record<string, any> = Record<string, a
|
|
|
46
46
|
* 需要执行多少次, =0代表不限
|
|
47
47
|
* 实际执行的次数(用于负载均衡时记录)
|
|
48
48
|
* 标签 用于调试一般可以标识监听器类型或任意信息
|
|
49
|
+
* 标识
|
|
49
50
|
* ]
|
|
50
51
|
*/
|
|
51
|
-
type FastListenerMeta = [TypedFastEventListener<any, any>, number, number, string
|
|
52
|
+
type FastListenerMeta = [TypedFastEventListener<any, any>, number, number, string, number];
|
|
52
53
|
type FastListenerNode = {
|
|
53
54
|
__listeners: FastListenerMeta[];
|
|
54
55
|
} & {
|
|
@@ -116,16 +117,14 @@ type ScopeEvents<T extends Record<string, any>, Prefix extends string> = PickSco
|
|
|
116
117
|
type FastEventListenOptions<Events extends Record<string, any> = Record<string, any>, Meta = any> = {
|
|
117
118
|
count?: number;
|
|
118
119
|
prepend?: boolean;
|
|
120
|
+
flags?: number;
|
|
119
121
|
filter?: (message: TypedFastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => boolean;
|
|
120
122
|
off?: (message: TypedFastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => boolean;
|
|
121
123
|
pipes?: FastListenerPipe[];
|
|
122
124
|
/**
|
|
123
125
|
* 为监听器添加一个tag,在监听器注册表中记录,用于调试使用
|
|
124
|
-
*
|
|
125
126
|
* emitter.on(type,listener,{tag:"x"})
|
|
126
|
-
*
|
|
127
127
|
* emitter.getListeners(tag)
|
|
128
|
-
*
|
|
129
128
|
*/
|
|
130
129
|
tag?: string;
|
|
131
130
|
};
|
|
@@ -685,6 +684,7 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
685
684
|
* @param listeners
|
|
686
685
|
*/
|
|
687
686
|
_decListenerExecCount(listeners: [FastListenerMeta, number, FastListenerMeta[]][]): void;
|
|
687
|
+
getListeners(type: keyof AllEvents): FastListenerMeta[];
|
|
688
688
|
/**
|
|
689
689
|
* 触发事件并执行对应的监听器
|
|
690
690
|
*
|
|
@@ -757,7 +757,6 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
757
757
|
[K in T]: K extends Types ? AllEvents[K] : any;
|
|
758
758
|
}, Meta>, options?: FastEventListenerArgs<Meta>): R[];
|
|
759
759
|
emit<R = any>(message: FastEventEmitMessage<AllEvents, Meta>, options?: FastEventListenerArgs<Meta>): R[];
|
|
760
|
-
getListeners(type: keyof AllEvents): FastListenerMeta[];
|
|
761
760
|
/**
|
|
762
761
|
* 异步触发事件
|
|
763
762
|
* @param type - 事件类型,可以是字符串或预定义的事件类型
|
|
@@ -831,6 +830,12 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
831
830
|
* // 无限等待事件
|
|
832
831
|
* const event = await emitter.waitFor('server/ready');
|
|
833
832
|
* console.log('服务器就绪');
|
|
833
|
+
*
|
|
834
|
+
* // 等待指定的事件发生,并且如果该事件有其他订阅者,则同时等待该事件的所有订阅执行完成
|
|
835
|
+
* waitFor
|
|
836
|
+
*
|
|
837
|
+
*
|
|
838
|
+
*
|
|
834
839
|
* ```
|
|
835
840
|
*/
|
|
836
841
|
waitFor<T extends Types>(type: T, timeout?: number): Promise<TypedFastEventMessage<{
|
|
@@ -1062,4 +1067,82 @@ declare const FastEventDirectives: {
|
|
|
1062
1067
|
clearRetain: symbol;
|
|
1063
1068
|
};
|
|
1064
1069
|
|
|
1065
|
-
|
|
1070
|
+
declare function isFastEventScope(target: any): target is FastEventScope;
|
|
1071
|
+
|
|
1072
|
+
declare function isFastEventMessage(msg: any): msg is TypedFastEventMessage;
|
|
1073
|
+
|
|
1074
|
+
declare function isFunction(fn: any): fn is Function;
|
|
1075
|
+
|
|
1076
|
+
declare function isString(str: any): str is string;
|
|
1077
|
+
|
|
1078
|
+
/**
|
|
1079
|
+
*
|
|
1080
|
+
*
|
|
1081
|
+
* 用于包括输入值为可展开的对象
|
|
1082
|
+
*
|
|
1083
|
+
*
|
|
1084
|
+
* 用在emit事件
|
|
1085
|
+
*
|
|
1086
|
+
* emitter1 = new FastEvent()
|
|
1087
|
+
* emitter2 = new FastEvent()
|
|
1088
|
+
*
|
|
1089
|
+
* emitter1.on("xxx",(message)=>{
|
|
1090
|
+
* return emitter2.emit(message)
|
|
1091
|
+
* })
|
|
1092
|
+
*
|
|
1093
|
+
* emitter2.on("xxx",()=>1)
|
|
1094
|
+
* emitter2.on("xxx",()=>2)
|
|
1095
|
+
*
|
|
1096
|
+
* const results = emitter1.emit("xxx")
|
|
1097
|
+
* // results == [[1,2]] 而不 [1,2],因为emitter2.emit返回的是一个[]
|
|
1098
|
+
*
|
|
1099
|
+
* expandable的作用就是对emit结果进行包括,当isExpandable时,在emit内部进行展开
|
|
1100
|
+
*
|
|
1101
|
+
*
|
|
1102
|
+
*
|
|
1103
|
+
*
|
|
1104
|
+
* emitter1.on("xxx",(message)=>{
|
|
1105
|
+
* // 告诉emitter2.emit返回的是一个expandable对象
|
|
1106
|
+
* // 然后在内部就展开此对象
|
|
1107
|
+
* return emitter2.emit(message)
|
|
1108
|
+
* })
|
|
1109
|
+
*
|
|
1110
|
+
* const results = emitter1.emit("xxx")
|
|
1111
|
+
* // results == [1,2]
|
|
1112
|
+
*
|
|
1113
|
+
* 为了实现对结果数据的展开处理,在emit内部需要对监听器的执行结果进行依次检查
|
|
1114
|
+
* 这多了一个迭代操作,为了不影响性能,可以关闭此特性
|
|
1115
|
+
* options.expandEmitResults = false
|
|
1116
|
+
*
|
|
1117
|
+
*
|
|
1118
|
+
*/
|
|
1119
|
+
declare const __expandable__: unique symbol;
|
|
1120
|
+
declare function expandable(value: any): any;
|
|
1121
|
+
declare function isExpandable(value: any): any;
|
|
1122
|
+
|
|
1123
|
+
/**
|
|
1124
|
+
*
|
|
1125
|
+
* 判断path是否与pattern匹配
|
|
1126
|
+
*
|
|
1127
|
+
* isPathMatched("a.b.c","a.b.c") == true
|
|
1128
|
+
* isPathMatched("a.b.c","a.b.*") == true
|
|
1129
|
+
* isPathMatched("a.b.c","a.*.*") == true
|
|
1130
|
+
* isPathMatched("a.b.c","*.*.*") == true
|
|
1131
|
+
* isPathMatched("a.b.c",".b.*") == true
|
|
1132
|
+
* isPathMatched("a.b.c.d","a.**") == true
|
|
1133
|
+
*
|
|
1134
|
+
* - '**' 匹配后续的
|
|
1135
|
+
* - '*' 匹配任意数量的字符,包括零个字符
|
|
1136
|
+
*
|
|
1137
|
+
* @param path
|
|
1138
|
+
* @param pattern
|
|
1139
|
+
*/
|
|
1140
|
+
declare function isPathMatched(path: string[], pattern: string[]): boolean;
|
|
1141
|
+
|
|
1142
|
+
declare function isSubsctiber(val: any): val is FastEventSubscriber;
|
|
1143
|
+
|
|
1144
|
+
declare function isClass(target: unknown): target is new (...args: any[]) => any;
|
|
1145
|
+
|
|
1146
|
+
declare function isFastEvent(target: any): target is FastEvent;
|
|
1147
|
+
|
|
1148
|
+
export { AbortError, BroadcastEvent, CancelError, type ChangeFieldType, type DeepPartial, type Dict, type Expand, type Fallback, FastEvent, FastEventBus, type FastEventBusEventTypes, type FastEventBusEvents, type FastEventBusMessage, FastEventBusNode, type FastEventBusNodeIds, type FastEventBusNodeOptions, type FastEventBusNodes, type FastEventBusOptions, FastEventDirectives, type FastEventEmitMessage, FastEventError, type FastEventListenOptions, type FastEventListener, type FastEventListenerArgs, type FastEventListeners, type FastEventMessage, type FastEventMessageExtends, type FastEventMeta, type FastEventOptions, FastEventScope, type FastEventScopeMeta, type FastEventScopeOptions, type FastEventSubscriber, type FastEvents, type FastListenerMeta, type FastListenerNode, type FastListeners, type Merge, NamespaceDelimiter, NodeDataEvent, type ObjectKeys, type OptionalItems, type Overloads, type OverrideOptions, type PickScopeEvents, QueueOverflowError, type RequiredItems, type ScopeEvents, TimeoutError, type TypedFastEventAnyListener, type TypedFastEventListener, type TypedFastEventMessage, type TypedFastEventMessageOptional, UnboundError, type Unique, __FastEventScope__, __FastEvent__, __expandable__, expandable, isClass, isExpandable, isFastEvent, isFastEventMessage, isFastEventScope, isFunction, isPathMatched, isString, isSubsctiber };
|
package/dist/eventbus/index.d.ts
CHANGED
|
@@ -46,9 +46,10 @@ type FastEventListener<P = any, M extends Record<string, any> = Record<string, a
|
|
|
46
46
|
* 需要执行多少次, =0代表不限
|
|
47
47
|
* 实际执行的次数(用于负载均衡时记录)
|
|
48
48
|
* 标签 用于调试一般可以标识监听器类型或任意信息
|
|
49
|
+
* 标识
|
|
49
50
|
* ]
|
|
50
51
|
*/
|
|
51
|
-
type FastListenerMeta = [TypedFastEventListener<any, any>, number, number, string
|
|
52
|
+
type FastListenerMeta = [TypedFastEventListener<any, any>, number, number, string, number];
|
|
52
53
|
type FastListenerNode = {
|
|
53
54
|
__listeners: FastListenerMeta[];
|
|
54
55
|
} & {
|
|
@@ -116,16 +117,14 @@ type ScopeEvents<T extends Record<string, any>, Prefix extends string> = PickSco
|
|
|
116
117
|
type FastEventListenOptions<Events extends Record<string, any> = Record<string, any>, Meta = any> = {
|
|
117
118
|
count?: number;
|
|
118
119
|
prepend?: boolean;
|
|
120
|
+
flags?: number;
|
|
119
121
|
filter?: (message: TypedFastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => boolean;
|
|
120
122
|
off?: (message: TypedFastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => boolean;
|
|
121
123
|
pipes?: FastListenerPipe[];
|
|
122
124
|
/**
|
|
123
125
|
* 为监听器添加一个tag,在监听器注册表中记录,用于调试使用
|
|
124
|
-
*
|
|
125
126
|
* emitter.on(type,listener,{tag:"x"})
|
|
126
|
-
*
|
|
127
127
|
* emitter.getListeners(tag)
|
|
128
|
-
*
|
|
129
128
|
*/
|
|
130
129
|
tag?: string;
|
|
131
130
|
};
|
|
@@ -685,6 +684,7 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
685
684
|
* @param listeners
|
|
686
685
|
*/
|
|
687
686
|
_decListenerExecCount(listeners: [FastListenerMeta, number, FastListenerMeta[]][]): void;
|
|
687
|
+
getListeners(type: keyof AllEvents): FastListenerMeta[];
|
|
688
688
|
/**
|
|
689
689
|
* 触发事件并执行对应的监听器
|
|
690
690
|
*
|
|
@@ -757,7 +757,6 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
757
757
|
[K in T]: K extends Types ? AllEvents[K] : any;
|
|
758
758
|
}, Meta>, options?: FastEventListenerArgs<Meta>): R[];
|
|
759
759
|
emit<R = any>(message: FastEventEmitMessage<AllEvents, Meta>, options?: FastEventListenerArgs<Meta>): R[];
|
|
760
|
-
getListeners(type: keyof AllEvents): FastListenerMeta[];
|
|
761
760
|
/**
|
|
762
761
|
* 异步触发事件
|
|
763
762
|
* @param type - 事件类型,可以是字符串或预定义的事件类型
|
|
@@ -831,6 +830,12 @@ declare class FastEvent<Events extends Record<string, any> = Record<string, any>
|
|
|
831
830
|
* // 无限等待事件
|
|
832
831
|
* const event = await emitter.waitFor('server/ready');
|
|
833
832
|
* console.log('服务器就绪');
|
|
833
|
+
*
|
|
834
|
+
* // 等待指定的事件发生,并且如果该事件有其他订阅者,则同时等待该事件的所有订阅执行完成
|
|
835
|
+
* waitFor
|
|
836
|
+
*
|
|
837
|
+
*
|
|
838
|
+
*
|
|
834
839
|
* ```
|
|
835
840
|
*/
|
|
836
841
|
waitFor<T extends Types>(type: T, timeout?: number): Promise<TypedFastEventMessage<{
|
|
@@ -1062,4 +1067,82 @@ declare const FastEventDirectives: {
|
|
|
1062
1067
|
clearRetain: symbol;
|
|
1063
1068
|
};
|
|
1064
1069
|
|
|
1065
|
-
|
|
1070
|
+
declare function isFastEventScope(target: any): target is FastEventScope;
|
|
1071
|
+
|
|
1072
|
+
declare function isFastEventMessage(msg: any): msg is TypedFastEventMessage;
|
|
1073
|
+
|
|
1074
|
+
declare function isFunction(fn: any): fn is Function;
|
|
1075
|
+
|
|
1076
|
+
declare function isString(str: any): str is string;
|
|
1077
|
+
|
|
1078
|
+
/**
|
|
1079
|
+
*
|
|
1080
|
+
*
|
|
1081
|
+
* 用于包括输入值为可展开的对象
|
|
1082
|
+
*
|
|
1083
|
+
*
|
|
1084
|
+
* 用在emit事件
|
|
1085
|
+
*
|
|
1086
|
+
* emitter1 = new FastEvent()
|
|
1087
|
+
* emitter2 = new FastEvent()
|
|
1088
|
+
*
|
|
1089
|
+
* emitter1.on("xxx",(message)=>{
|
|
1090
|
+
* return emitter2.emit(message)
|
|
1091
|
+
* })
|
|
1092
|
+
*
|
|
1093
|
+
* emitter2.on("xxx",()=>1)
|
|
1094
|
+
* emitter2.on("xxx",()=>2)
|
|
1095
|
+
*
|
|
1096
|
+
* const results = emitter1.emit("xxx")
|
|
1097
|
+
* // results == [[1,2]] 而不 [1,2],因为emitter2.emit返回的是一个[]
|
|
1098
|
+
*
|
|
1099
|
+
* expandable的作用就是对emit结果进行包括,当isExpandable时,在emit内部进行展开
|
|
1100
|
+
*
|
|
1101
|
+
*
|
|
1102
|
+
*
|
|
1103
|
+
*
|
|
1104
|
+
* emitter1.on("xxx",(message)=>{
|
|
1105
|
+
* // 告诉emitter2.emit返回的是一个expandable对象
|
|
1106
|
+
* // 然后在内部就展开此对象
|
|
1107
|
+
* return emitter2.emit(message)
|
|
1108
|
+
* })
|
|
1109
|
+
*
|
|
1110
|
+
* const results = emitter1.emit("xxx")
|
|
1111
|
+
* // results == [1,2]
|
|
1112
|
+
*
|
|
1113
|
+
* 为了实现对结果数据的展开处理,在emit内部需要对监听器的执行结果进行依次检查
|
|
1114
|
+
* 这多了一个迭代操作,为了不影响性能,可以关闭此特性
|
|
1115
|
+
* options.expandEmitResults = false
|
|
1116
|
+
*
|
|
1117
|
+
*
|
|
1118
|
+
*/
|
|
1119
|
+
declare const __expandable__: unique symbol;
|
|
1120
|
+
declare function expandable(value: any): any;
|
|
1121
|
+
declare function isExpandable(value: any): any;
|
|
1122
|
+
|
|
1123
|
+
/**
|
|
1124
|
+
*
|
|
1125
|
+
* 判断path是否与pattern匹配
|
|
1126
|
+
*
|
|
1127
|
+
* isPathMatched("a.b.c","a.b.c") == true
|
|
1128
|
+
* isPathMatched("a.b.c","a.b.*") == true
|
|
1129
|
+
* isPathMatched("a.b.c","a.*.*") == true
|
|
1130
|
+
* isPathMatched("a.b.c","*.*.*") == true
|
|
1131
|
+
* isPathMatched("a.b.c",".b.*") == true
|
|
1132
|
+
* isPathMatched("a.b.c.d","a.**") == true
|
|
1133
|
+
*
|
|
1134
|
+
* - '**' 匹配后续的
|
|
1135
|
+
* - '*' 匹配任意数量的字符,包括零个字符
|
|
1136
|
+
*
|
|
1137
|
+
* @param path
|
|
1138
|
+
* @param pattern
|
|
1139
|
+
*/
|
|
1140
|
+
declare function isPathMatched(path: string[], pattern: string[]): boolean;
|
|
1141
|
+
|
|
1142
|
+
declare function isSubsctiber(val: any): val is FastEventSubscriber;
|
|
1143
|
+
|
|
1144
|
+
declare function isClass(target: unknown): target is new (...args: any[]) => any;
|
|
1145
|
+
|
|
1146
|
+
declare function isFastEvent(target: any): target is FastEvent;
|
|
1147
|
+
|
|
1148
|
+
export { AbortError, BroadcastEvent, CancelError, type ChangeFieldType, type DeepPartial, type Dict, type Expand, type Fallback, FastEvent, FastEventBus, type FastEventBusEventTypes, type FastEventBusEvents, type FastEventBusMessage, FastEventBusNode, type FastEventBusNodeIds, type FastEventBusNodeOptions, type FastEventBusNodes, type FastEventBusOptions, FastEventDirectives, type FastEventEmitMessage, FastEventError, type FastEventListenOptions, type FastEventListener, type FastEventListenerArgs, type FastEventListeners, type FastEventMessage, type FastEventMessageExtends, type FastEventMeta, type FastEventOptions, FastEventScope, type FastEventScopeMeta, type FastEventScopeOptions, type FastEventSubscriber, type FastEvents, type FastListenerMeta, type FastListenerNode, type FastListeners, type Merge, NamespaceDelimiter, NodeDataEvent, type ObjectKeys, type OptionalItems, type Overloads, type OverrideOptions, type PickScopeEvents, QueueOverflowError, type RequiredItems, type ScopeEvents, TimeoutError, type TypedFastEventAnyListener, type TypedFastEventListener, type TypedFastEventMessage, type TypedFastEventMessageOptional, UnboundError, type Unique, __FastEventScope__, __FastEvent__, __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 Q=(n,e,t)=>e in n?B(n,e,{enumerable:true,configurable:true,writable:true,value:t}):n[e]=t;var a=(n,e)=>B(n,"name",{value:e,configurable:true});var u=(n,e,t)=>Q(n,typeof e!="symbol"?e+"":e,t);var te=Symbol.for("__FastEvent__"),se=Symbol.for("__FastEventScope__"),$=class $ extends Error{constructor(e){super(e);}};a($,"FastEventError");var m=$,T=class T extends m{};a(T,"TimeoutError");var W=T,O=class O extends m{};a(O,"UnboundError");var y=O,j=class j extends m{};a(j,"AbortError");var g=j,F=class F extends m{};a(F,"CancelError");var v=F,R=class R extends m{};a(R,"QueueOverflowError");var V=R,d={clearRetain:Symbol.for("ClearRetain")};function E(n,e,t,s){let i,r={},o={};return typeof n[0]=="object"?(Object.assign(o,n[0]),r=typeof n[1]=="boolean"?{retain:n[1]}:n[1]||{},i=n[0].meta):(o.type=n[0],o.payload=n[1],r=typeof n[2]=="boolean"?{retain:n[2]}:n[2]||{}),i=Object.assign({},e,t,r.meta,i),Object.keys(i).length===0&&(i=void 0),o.meta=i,r.executor===void 0&&(r.executor=s),[o,r]}a(E,"parseEmitArgs");function I(n){return n?typeof n=="object"&&"__FastEventScope__"in n:false}a(I,"isFastEventScope");function L(n,e,t){let s=n[0],i=I(n[1])?n[1]:void 0,r=(i?n[2]:n[1])||{};return r.meta=Object.assign({},e,r?.meta),r.context=r.context!==void 0?r.context:t,[s,i,r]}a(L,"parseScopeArgs");function x(n,e){return Object.defineProperty(n,"name",{value:e||"anonymous",configurable:true}),n}a(x,"renameFn");var M=class M{constructor(e){u(this,"__FastEventScope__",true);u(this,"_options",{});u(this,"types",{events:void 0,meta:void 0,context:void 0,message:void 0,listeners:void 0,anyListener:void 0});u(this,"prefix","");u(this,"emitter");this._options=Object.assign({},this._initOptions(e));}get context(){return this.options.context||this}get options(){return this._options}bind(e,t,s){this.emitter=e,this._options=Object.assign(this._options,{scope:t},s),t.length>0&&!t.endsWith(e.options.delimiter)&&(this.prefix=t+e.options.delimiter);}_initOptions(e){return e}_getScopeListener(e){let t=this.prefix;if(t.length===0)return e;e||(e=(this._options.onMessage||this.onMessage).bind(this));let s=this;return x(function(r,o){if(r.type.startsWith(t))return e.call(s.context,Object.assign({},r,{type:r.type.substring(t.length)}),o)},e.name)}_getScopeType(e){return e===void 0?void 0:this.prefix+e}_fixScopeType(e){return e.startsWith(this.prefix)?e.substring(this.prefix.length):e}on(){if(!this.emitter)throw new y;let e=[...arguments];return e[0]=this._getScopeType(e[0]),e[1]=this._getScopeListener(e[1]),this.emitter.on(...e)}once(){return this.on(arguments[0],arguments[1],Object.assign({},arguments[2],{count:1}))}onAny(){return this.on("**",...arguments)}off(){let e=arguments;typeof e[0]=="string"&&(e[0]=this._getScopeType(e[0])),this.emitter.off(...e);}offAll(){this.emitter.offAll(this.prefix.substring(0,this.prefix.length-1));}clear(){this.emitter.clear(this.prefix.substring(0,this.prefix.length-1));}emit(){if(arguments.length===2&&typeof arguments[0]=="string"&&arguments[1]===d.clearRetain)return this.emitter.emit(this._getScopeType(arguments[0]));let[e,t]=E(arguments,this.emitter.options.meta,this.options.meta,this.options.executor);return e.type=this._getScopeType(e.type),this.emitter.emit(e,t)}async emitAsync(){return (await Promise.allSettled(this.emit.apply(this,arguments))).map(t=>t.status==="fulfilled"?t.value:t.reason)}async waitFor(){let e=arguments[0],t=arguments[1],s=await this.emitter.waitFor(this._getScopeType(e),t);return Object.assign({},s,{type:this._fixScopeType(s.type)})}scope(){let[e,t,s]=L(arguments,this.options.meta,this.options.context),i;return t?i=t:i=new M,i.bind(this.emitter,this.prefix+e,s),i}onMessage(e,t){}};a(M,"FastEventScope");var S=M;function P(n,e){if(n.length!==e.length&&n.length>0&&e[e.length-1]!=="**")return false;let t=[...e];e.length>0&&e[e.length-1]==="**"&&t.splice(e.length-1,1,...Array.from({length:n.length-e.length+1}).fill("*"));for(let s=0;s<n.length;s++)if(t[s]!=="*"&&t[s]!==n[s])return false;return true}a(P,"isPathMatched");function X(n,e){let t=[];for(;;){let s=n.findIndex(i=>e(i));if(s===-1){t.push(s);break}n.splice(s,1);}return t}a(X,"removeItem");function p(n){return n&&typeof n=="function"}a(p,"isFunction");var q=Symbol.for("__expandable__");function z(n){return n[q]=true,n}a(z,"expandable");function G(n){return n&&n[q]}a(G,"isExpandable");function H(n){for(let e=0;e<n.length;e++){let t=n[e];Array.isArray(t)&&G(t)&&(n.splice(e,1,...t),e+=t.length-1);}return n}a(H,"expandEmitResults");function J(n){return n&&typeof n=="object"&&"off"in n&&"listener"in n}a(J,"isSubsctiber");function K(n,e){return n.catch(t=>(e&&e(t),Promise.resolve(t)))}a(K,"tryReturnError");var N=class N{constructor(e){u(this,"__FastEvent__",true);u(this,"listeners",{__listeners:[]});u(this,"_options");u(this,"_delimiter","/");u(this,"_context");u(this,"retainedMessages",new Map);u(this,"listenerCount",0);u(this,"types",{events:void 0,meta:void 0,context:void 0,message:void 0,listeners:void 0,anyListener:void 0});this._options=Object.assign({debug:false,id:Math.random().toString(36).substring(2),delimiter:"/",context:null,ignoreErrors:true,meta:void 0,expandEmitResults:true},this._initOptions(e)),this._delimiter=this._options.delimiter,this._context=this._options.context,this._enableDevTools();}get options(){return this._options}get context(){return this.options.context||this}get meta(){return this.options.meta}get id(){return this._options.id}_initOptions(e){return e}_addListener(e,t,s){let{count:i,prepend:r}=s,o=0;return [this._forEachNodes(e,c=>{let f=[t,i,0];s.tag&&f.push(s.tag),r?(c.__listeners.splice(0,0,f),o=0):(c.__listeners.push(f),o=c.__listeners.length-1),this.listenerCount++;}),o]}_enableDevTools(){this.options.debug&&globalThis.__FLEXEVENT_DEVTOOLS__&&globalThis.__FLEXEVENT_DEVTOOLS__.add(this);}_forEachNodes(e,t){if(e.length===0)return;let s=this.listeners;for(let i=0;i<e.length;i++){let r=e[i];if(r in s||(s[r]={__listeners:[]}),i===e.length-1){let o=s[r];return t(o,s),o}else s=s[r];}}_removeListener(e,t,s){s&&X(e.__listeners,i=>{i=Array.isArray(i)?i[0]:i;let r=i===s;return r&&(this.listenerCount--,p(this._options.onRemoveListener)&&this._options.onRemoveListener(t.join(this._delimiter),s)),r});}_pipeListener(e,t){return t.forEach(s=>{e=x(s(e),e.name);}),e}on(){let e=arguments[0],t=p(arguments[1])?arguments[1]:(this._options.onMessage||this.onMessage).bind(this),s=Object.assign({count:0,prepend:false},p(arguments[1])?arguments[2]:arguments[1]);if(e.length===0)throw new Error("event type cannot be empty");if(p(this._options.onAddListener)){let c=this._options.onAddListener(e,t,s);if(c===false)throw new v;if(J(c))return c}let i=e.split(this._delimiter);if(s.pipes&&s.pipes.length>0&&(t=this._pipeListener(t,s.pipes)),p(s.filter)||p(s.off)){let c=t;t=x(function(f,l){if(p(s.off)&&s.off.call(this,f,l)){h();return}if(p(s.filter)){if(s.filter.call(this,f,l))return c.call(this,f,l)}else return c.call(this,f,l)},t.name);}let[r,o]=this._addListener(i,t,s),h=a(()=>r&&this._removeListener(r,i,t),"off");return this._emitRetainMessage(e,r,o),{off:h,listener:t}}once(){return p(arguments[1])?this.on(arguments[0],arguments[1],Object.assign({},arguments[2],{count:1})):this.on(arguments[0],Object.assign({},arguments[2],{count:1}))}onAny(){return this.on("**",arguments[0],arguments[1])}onMessage(e,t){}off(){let e=arguments,t=p(e[0])?void 0:e[0],s=p(e[0])?e[0]:e[1],i=t?t.split(this._delimiter):[],r=t?t.includes("*"):false;if(t&&!r)this._traverseToPath(this.listeners,i,o=>{s?this._removeListener(o,i,s):t&&(o.__listeners=[]);});else {let o=r?[]:i;this._traverseListeners(this.listeners,o,(h,c)=>{(s!==void 0||r&&P(h,i))&&(s?this._removeListener(c,i,s):c.__listeners=[]);});}}offAll(e){if(e){let t=e.split(this._delimiter),s=0;this._traverseListeners(this.listeners,t,(i,r)=>{s+=r.__listeners.length,r.__listeners=[];}),this.listenerCount-=s,this._removeRetainedEvents(e);}else {let t=0;this._traverseListeners(this.listeners,[],(s,i)=>{t+=i.__listeners.length;}),this.listenerCount-=t,this.retainedMessages.clear(),this.listeners={__listeners:[]};}p(this._options.onClearListeners)&&this._options.onClearListeners.call(this);}_removeRetainedEvents(e){e||this.retainedMessages.clear(),e?.endsWith(this._delimiter)&&(e+=this._delimiter),this.retainedMessages.delete(e);for(let t of this.retainedMessages.keys())t.startsWith(e)&&this.retainedMessages.delete(t);}clear(e){this.offAll(e),this._removeRetainedEvents(e);}_emitRetainMessage(e,t,s){let i=[];if(e.includes("*")){let r=e.split(this._delimiter);this.retainedMessages.forEach((o,h)=>{let c=h.split(this._delimiter);P(c,r)&&i.push(o);});}else this.retainedMessages.has(e)&&i.push(this.retainedMessages.get(e));t&&i.forEach(r=>{this._executeListeners([t],r,{},o=>o[0]===t.__listeners[s][0]);});}_traverseToPath(e,t,s,i=0,r){if(i>=t.length){s(e);return}let o=t[i];if(r===true){this._traverseToPath(e,t,s,i+1,true);return}"*"in e&&this._traverseToPath(e["*"],t,s,i+1),"**"in e&&this._traverseToPath(e["**"],t,s,i+1,true),o in e&&this._traverseToPath(e[o],t,s,i+1);}_traverseListeners(e,t,s){let i=e;t&&t.length>0&&this._traverseToPath(e,t,o=>{i=o;});let r=a((o,h,c)=>{h(c,o);for(let[f,l]of Object.entries(o))f.startsWith("__")||l&&r(l,h,[...c,f]);},"traverseNodes");r(i,s,[]);}_onListenerError(e,t,s,i){if(i instanceof Error&&(i._emitter=`${e.name||"anonymous"}:${t.type}`),p(this._options.onListenerError))try{this._options.onListenerError.call(this,i,e,t,s);}catch{}if(this._options.ignoreErrors)return i;throw i}_executeListener(e,t,s,i=false){try{if(s&&s.abortSignal&&s.abortSignal.aborted)return this._onListenerError(e,t,s,new g(e.name));let r=e.call(this.context,t,s);return i&&r&&r instanceof Promise&&(r=K(r,o=>this._onListenerError(e,t,s,o))),r}catch(r){return this._onListenerError(e,t,s,r)}}_getListenerExecutor(e){if(!e)return;let t=e.executor||this._options.executor;if(p(t))return t}_executeListeners(e,t,s,i){if(!e||e.length===0)return [];let r=e.reduce((h,c)=>h.concat(c.__listeners.filter(f=>p(i)?i(f,c):true).map((f,l)=>[f,l,c.__listeners])),[]);this._decListenerExecCount(r);let o=this._getListenerExecutor(s);if(o){let h=o(r.map(c=>c[0]),t,s,this._executeListener.bind(this));return Array.isArray(h)?h:[h]}else return r.map(h=>this._executeListener(h[0][0],t,s,true))}_decListenerExecCount(e){for(let t=e.length-1;t>=0;t--){let s=e[t][0];s[2]++,s[1]>0&&s[1]<=s[2]&&e[t][2].splice(t,1);}}emit(){if(arguments.length===2&&typeof arguments[0]=="string"&&arguments[1]===d.clearRetain)return this.retainedMessages.delete(arguments[0]),[];let[e,t]=E(arguments,this.options.meta);p(t.parseArgs)&&t.parseArgs(e,t);let s=e.type.split(this._delimiter);t.retain&&this.retainedMessages.set(e.type,e);let i=[],r=[];if(this._traverseToPath(this.listeners,s,o=>{r.push(o);}),p(this._options.onBeforeExecuteListener)){let o=this._options.onBeforeExecuteListener.call(this,e,t);if(Array.isArray(o))return o;if(o===false)throw new g(e.type)}return i.push(...this._executeListeners(r,e,t)),p(this._options.onAfterExecuteListener)&&this._options.onAfterExecuteListener.call(this,e,i,r),this._options.expandEmitResults&&H(i),i}getListeners(e){let t=[],s=e.split(this._delimiter);return this._traverseToPath(this.listeners,s,i=>{t.push(i);}),t[0].__listeners}async emitAsync(){return (await Promise.allSettled(this.emit.apply(this,arguments))).map(t=>t.status==="fulfilled"?t.value:t.reason)}waitFor(){let e=arguments[0],t=arguments[1];return new Promise((s,i)=>{let r,o,h=a(c=>{clearTimeout(r),o&&o.off(),s(c);},"listener");t&&t>0&&(r=setTimeout(()=>{o&&o.off(),i(new Error("wait for event<"+e+"> is timeout"));},t)),o=this.on(e,h);})}scope(){let[e,t,s]=L(arguments,this.options.meta,this.options.context),i;return t?i=t:i=new S,i.bind(this,e,s),i}};a(N,"FastEvent");var _=N;var b="::",A="@",Y="data";function Z(n){return n?typeof n=="object"&&"type"in n:false}a(Z,"isFastEventMessage");function w(n,e="/"){let t=Z(n[0]),s=Object.assign({payload:t?n[0].payload:n[1]},t?n[0]:{},{type:`${A}${e}${t?n[0].type:"data"}`}),i=t?n[1]:n[2];return [s,i]}a(w,"parseBroadcaseArgs");var C=class C extends _{constructor(t){super(t);u(this,"nodes");this.nodes=new Map;}add(...t){t.forEach(s=>{if(this.nodes.has(s.id))throw new Error(`Node with id ${s.id} already exists`);s.options.delimiter=this.options.delimiter,s.options.debug=this.options.debug,s.options.ignoreErrors=this.options.ignoreErrors,this.nodes.set(s.id,s),this.emit(`$disconnect${this.options.delimiter}${s.id}`,d.clearRetain),this.emit(`$connect${this.options.delimiter}${s.id}`,s.id,true);});}remove(t){let s=this.nodes.get(t);s&&(s.eventbus=void 0,this.nodes.delete(t),this.emit(`$connect${this.options.delimiter}${s.id}`,d.clearRetain),this.emit(`$disconnect${this.options.delimiter}${s.id}`,s.id,true));}broadcast(){let[t,s]=w(arguments,this.options.delimiter);return this.emit(t,s)}send(t,s){return t.type=`${t.to}${this.options.delimiter}${Y}`,this.emit(t,s)[0]}};a(C,"FastEventBus");var k=C;var D=class D extends _{constructor(t){super(t);u(this,"eventbus");u(this,"_subscribers",[]);this.options.onBeforeExecuteListener=this._onBeforeExecuteListener.bind(this),this.options.onAddListener=this._onAddListener.bind(this),this._subscribers.push(this.on("data"));}_onBeforeExecuteListener(t,s){if(t.type.includes(b))return t.type=t.type.replace(b,this.eventbus.options.delimiter),t.from=this.id,this.eventbus.emit(t,s)}_onAddListener(t,s,i){if(t.includes(b)){let[r,o]=t.split(b);if(r===this.id)return;let h,c=this.eventbus.on(`$connect${this.options.delimiter}*`,f=>{if(f.payload===r){let l=this.eventbus.nodes.get(r);l&&(h=l.on(o,s,i)),c.off();}});return {off:a(()=>h?h.off():c.off(),"off"),listener:h?h.listener:c.listener}}}connect(t){this.eventbus=t,this.eventbus.add(this),this._subscribers.push(this.eventbus.on(`${A}${this.eventbus.options.delimiter}**`,this.onMessage.bind(this))),this._onSubscribeForNode();}disconnect(){this.eventbus?.remove(this.id),this._subscribers.forEach(t=>t.off());}_onSubscribeForNode(){let t=`${this.id}${this.eventbus.options.delimiter}`;this._subscribers.push(this.eventbus.on(`${t}**`,(s,i)=>(s.type=s.type.substring(t.length),z(this.emit(s,i)))));}send(t,s,i){this._assertConnected();let r={type:"data",from:this.id,to:t,payload:s};return this.eventbus.send(r,i)}_assertConnected(){if(!this.eventbus)throw new Error("Node is not connected to any event bus")}broadcast(){this._assertConnected();let[t,s]=w(arguments,this.options.delimiter);return t.from=this.id,this.eventbus.broadcast(t,s)}onMessage(t,s){}};a(D,"FastEventBusNode");var U=D;exports.AbortError=g;exports.BroadcastEvent=A;exports.CancelError=v;exports.FastEvent=_;exports.FastEventBus=k;exports.FastEventBusNode=U;exports.FastEventDirectives=d;exports.FastEventError=m;exports.FastEventScope=S;exports.NamespaceDelimiter=b;exports.NodeDataEvent=Y;exports.QueueOverflowError=V;exports.TimeoutError=W;exports.UnboundError=y;exports.__FastEventScope__=se;exports.__FastEvent__=te;//# sourceMappingURL=index.js.map
|
|
1
|
+
'use strict';var W=Object.defineProperty;var Q=(n,e,t)=>e in n?W(n,e,{enumerable:true,configurable:true,writable:true,value:t}):n[e]=t;var a=(n,e)=>W(n,"name",{value:e,configurable:true});var u=(n,e,t)=>Q(n,typeof e!="symbol"?e+"":e,t);var te=Symbol.for("__FastEvent__"),se=Symbol.for("__FastEventScope__"),$=class $ extends Error{constructor(e){super(e);}};a($,"FastEventError");var m=$,T=class T extends m{};a(T,"TimeoutError");var B=T,j=class j extends m{};a(j,"UnboundError");var y=j,O=class O extends m{};a(O,"AbortError");var g=O,F=class F extends m{};a(F,"CancelError");var v=F,R=class R extends m{};a(R,"QueueOverflowError");var V=R,d={clearRetain:Symbol.for("ClearRetain")};function E(n,e,t,s){let i,r={},o={};return typeof n[0]=="object"?(Object.assign(o,n[0]),r=typeof n[1]=="boolean"?{retain:n[1]}:n[1]||{},i=n[0].meta):(o.type=n[0],o.payload=n[1],r=typeof n[2]=="boolean"?{retain:n[2]}:n[2]||{}),i=Object.assign({},e,t,r.meta,i),Object.keys(i).length===0&&(i=void 0),o.meta=i,r.executor===void 0&&(r.executor=s),[o,r]}a(E,"parseEmitArgs");function I(n){return n?typeof n=="object"&&"__FastEventScope__"in n:false}a(I,"isFastEventScope");function L(n,e,t){let s=n[0],i=I(n[1])?n[1]:void 0,r=(i?n[2]:n[1])||{};return r.meta=Object.assign({},e,r?.meta),r.context=r.context!==void 0?r.context:t,[s,i,r]}a(L,"parseScopeArgs");function x(n,e){return Object.defineProperty(n,"name",{value:e||"anonymous",configurable:true}),n}a(x,"renameFn");var M=class M{constructor(e){u(this,"__FastEventScope__",true);u(this,"_options",{});u(this,"types",{events:void 0,meta:void 0,context:void 0,message:void 0,listeners:void 0,anyListener:void 0});u(this,"prefix","");u(this,"emitter");this._options=Object.assign({},this._initOptions(e));}get context(){return this.options.context||this}get options(){return this._options}bind(e,t,s){this.emitter=e,this._options=Object.assign(this._options,{scope:t},s),t.length>0&&!t.endsWith(e.options.delimiter)&&(this.prefix=t+e.options.delimiter);}_initOptions(e){return e}_getScopeListener(e){let t=this.prefix;if(t.length===0)return e;e||(e=(this._options.onMessage||this.onMessage).bind(this));let s=this;return x(function(r,o){if(r.type.startsWith(t))return e.call(s.context,Object.assign({},r,{type:r.type.substring(t.length)}),o)},e.name)}_getScopeType(e){return e===void 0?void 0:this.prefix+e}_fixScopeType(e){return e.startsWith(this.prefix)?e.substring(this.prefix.length):e}on(){if(!this.emitter)throw new y;let e=[...arguments];return e[0]=this._getScopeType(e[0]),e[1]=this._getScopeListener(e[1]),this.emitter.on(...e)}once(){return this.on(arguments[0],arguments[1],Object.assign({},arguments[2],{count:1}))}onAny(){return this.on("**",...arguments)}off(){let e=arguments;typeof e[0]=="string"&&(e[0]=this._getScopeType(e[0])),this.emitter.off(...e);}offAll(){this.emitter.offAll(this.prefix.substring(0,this.prefix.length-1));}clear(){this.emitter.clear(this.prefix.substring(0,this.prefix.length-1));}emit(){if(arguments.length===2&&typeof arguments[0]=="string"&&arguments[1]===d.clearRetain)return this.emitter.emit(this._getScopeType(arguments[0]));let[e,t]=E(arguments,this.emitter.options.meta,this.options.meta,this.options.executor);return e.type=this._getScopeType(e.type),this.emitter.emit(e,t)}async emitAsync(){return (await Promise.allSettled(this.emit.apply(this,arguments))).map(t=>t.status==="fulfilled"?t.value:t.reason)}async waitFor(){let e=arguments[0],t=arguments[1],s=await this.emitter.waitFor(this._getScopeType(e),t);return Object.assign({},s,{type:this._fixScopeType(s.type)})}scope(){let[e,t,s]=L(arguments,this.options.meta,this.options.context),i;return t?i=t:i=new M,i.bind(this.emitter,this.prefix+e,s),i}onMessage(e,t){}};a(M,"FastEventScope");var S=M;function P(n,e){if(n.length!==e.length&&n.length>0&&e[e.length-1]!=="**")return false;let t=[...e];e.length>0&&e[e.length-1]==="**"&&t.splice(e.length-1,1,...Array.from({length:n.length-e.length+1}).fill("*"));for(let s=0;s<n.length;s++)if(t[s]!=="*"&&t[s]!==n[s])return false;return true}a(P,"isPathMatched");function X(n,e){let t=[];for(;;){let s=n.findIndex(i=>e(i));if(s===-1){t.push(s);break}n.splice(s,1);}return t}a(X,"removeItem");function p(n){return n&&typeof n=="function"}a(p,"isFunction");var q=Symbol.for("__expandable__");function z(n){return n[q]=true,n}a(z,"expandable");function G(n){return n&&n[q]}a(G,"isExpandable");function H(n){for(let e=0;e<n.length;e++){let t=n[e];Array.isArray(t)&&G(t)&&(n.splice(e,1,...t),e+=t.length-1);}return n}a(H,"expandEmitResults");function J(n){return n&&typeof n=="object"&&"off"in n&&"listener"in n}a(J,"isSubsctiber");function K(n,e){return n.catch(t=>(e&&e(t),Promise.resolve(t)))}a(K,"tryReturnError");var C=class C{constructor(e){u(this,"__FastEvent__",true);u(this,"listeners",{__listeners:[]});u(this,"_options");u(this,"_delimiter","/");u(this,"_context");u(this,"retainedMessages",new Map);u(this,"listenerCount",0);u(this,"types",{events:void 0,meta:void 0,context:void 0,message:void 0,listeners:void 0,anyListener:void 0});this._options=Object.assign({debug:false,id:Math.random().toString(36).substring(2),delimiter:"/",context:null,ignoreErrors:true,meta:void 0,expandEmitResults:true},this._initOptions(e)),this._delimiter=this._options.delimiter,this._context=this._options.context,this._enableDevTools();}get options(){return this._options}get context(){return this.options.context||this}get meta(){return this.options.meta}get id(){return this._options.id}_initOptions(e){return e}_addListener(e,t,s){let{count:i,prepend:r}=s,o=0;return [this._forEachNodes(e,c=>{let f=[t,i,0,s.tag,s.flags];r?(c.__listeners.splice(0,0,f),o=0):(c.__listeners.push(f),o=c.__listeners.length-1),this.listenerCount++;}),o]}_enableDevTools(){this.options.debug&&globalThis.__FLEXEVENT_DEVTOOLS__&&globalThis.__FLEXEVENT_DEVTOOLS__.add(this);}_forEachNodes(e,t){if(e.length===0)return;let s=this.listeners;for(let i=0;i<e.length;i++){let r=e[i];if(r in s||(s[r]={__listeners:[]}),i===e.length-1){let o=s[r];return t(o,s),o}else s=s[r];}}_removeListener(e,t,s){s&&X(e.__listeners,i=>{i=Array.isArray(i)?i[0]:i;let r=i===s;return r&&(this.listenerCount--,p(this._options.onRemoveListener)&&this._options.onRemoveListener(t.join(this._delimiter),s)),r});}_pipeListener(e,t){return t.forEach(s=>{e=x(s(e),e.name);}),e}on(){let e=arguments[0],t=p(arguments[1])?arguments[1]:(this._options.onMessage||this.onMessage).bind(this),s=Object.assign({count:0,flags:0,prepend:false},p(arguments[1])?arguments[2]:arguments[1]);if(e.length===0)throw new Error("event type cannot be empty");if(p(this._options.onAddListener)){let c=this._options.onAddListener(e,t,s);if(c===false)throw new v;if(J(c))return c}let i=e.split(this._delimiter);if(s.pipes&&s.pipes.length>0&&(t=this._pipeListener(t,s.pipes)),p(s.filter)||p(s.off)){let c=t;t=x(function(f,l){if(p(s.off)&&s.off.call(this,f,l)){h();return}if(p(s.filter)){if(s.filter.call(this,f,l))return c.call(this,f,l)}else return c.call(this,f,l)},t.name);}let[r,o]=this._addListener(i,t,s),h=a(()=>r&&this._removeListener(r,i,t),"off");return this._emitRetainMessage(e,r,o),{off:h,listener:t}}once(){return p(arguments[1])?this.on(arguments[0],arguments[1],Object.assign({},arguments[2],{count:1})):this.on(arguments[0],Object.assign({},arguments[2],{count:1}))}onAny(){return this.on("**",arguments[0],arguments[1])}onMessage(e,t){}off(){let e=arguments,t=p(e[0])?void 0:e[0],s=p(e[0])?e[0]:e[1],i=t?t.split(this._delimiter):[],r=t?t.includes("*"):false;if(t&&!r)this._traverseToPath(this.listeners,i,o=>{s?this._removeListener(o,i,s):t&&(o.__listeners=[]);});else {let o=r?[]:i;this._traverseListeners(this.listeners,o,(h,c)=>{(s!==void 0||r&&P(h,i))&&(s?this._removeListener(c,i,s):c.__listeners=[]);});}}offAll(e){if(e){let t=e.split(this._delimiter),s=0;this._traverseListeners(this.listeners,t,(i,r)=>{s+=r.__listeners.length,r.__listeners=[];}),this.listenerCount-=s,this._removeRetainedEvents(e);}else {let t=0;this._traverseListeners(this.listeners,[],(s,i)=>{t+=i.__listeners.length;}),this.listenerCount-=t,this.retainedMessages.clear(),this.listeners={__listeners:[]};}p(this._options.onClearListeners)&&this._options.onClearListeners.call(this);}_removeRetainedEvents(e){e||this.retainedMessages.clear(),e?.endsWith(this._delimiter)&&(e+=this._delimiter),this.retainedMessages.delete(e);for(let t of this.retainedMessages.keys())t.startsWith(e)&&this.retainedMessages.delete(t);}clear(e){this.offAll(e),this._removeRetainedEvents(e);}_emitRetainMessage(e,t,s){let i=[];if(e.includes("*")){let r=e.split(this._delimiter);this.retainedMessages.forEach((o,h)=>{let c=h.split(this._delimiter);P(c,r)&&i.push(o);});}else this.retainedMessages.has(e)&&i.push(this.retainedMessages.get(e));t&&i.forEach(r=>{this._executeListeners([t],r,{},o=>o[0]===t.__listeners[s][0]);});}_traverseToPath(e,t,s,i=0,r){if(i>=t.length){s(e);return}let o=t[i];if(r===true){this._traverseToPath(e,t,s,i+1,true);return}"*"in e&&this._traverseToPath(e["*"],t,s,i+1),"**"in e&&this._traverseToPath(e["**"],t,s,i+1,true),o in e&&this._traverseToPath(e[o],t,s,i+1);}_traverseListeners(e,t,s){let i=e;t&&t.length>0&&this._traverseToPath(e,t,o=>{i=o;});let r=a((o,h,c)=>{h(c,o);for(let[f,l]of Object.entries(o))f.startsWith("__")||l&&r(l,h,[...c,f]);},"traverseNodes");r(i,s,[]);}_onListenerError(e,t,s,i){if(i instanceof Error&&(i._emitter=`${e.name||"anonymous"}:${t.type}`),p(this._options.onListenerError))try{this._options.onListenerError.call(this,i,e,t,s);}catch{}if(this._options.ignoreErrors)return i;throw i}_executeListener(e,t,s,i=false){try{if(s&&s.abortSignal&&s.abortSignal.aborted)return this._onListenerError(e,t,s,new g(e.name));let r=e.call(this.context,t,s);return i&&r&&r instanceof Promise&&(r=K(r,o=>this._onListenerError(e,t,s,o))),r}catch(r){return this._onListenerError(e,t,s,r)}}_getListenerExecutor(e){if(!e)return;let t=e.executor||this._options.executor;if(p(t))return t}_executeListeners(e,t,s,i){if(!e||e.length===0)return [];let r=e.reduce((h,c)=>h.concat(c.__listeners.filter(f=>p(i)?i(f,c):true).map((f,l)=>[f,l,c.__listeners])),[]);this._decListenerExecCount(r);let o=this._getListenerExecutor(s);if(o){let h=o(r.map(c=>c[0]),t,s,this._executeListener.bind(this));return Array.isArray(h)?h:[h]}else return r.map(h=>this._executeListener(h[0][0],t,s,true))}_decListenerExecCount(e){for(let t=e.length-1;t>=0;t--){let s=e[t][0];s[2]++,s[1]>0&&s[1]<=s[2]&&e[t][2].splice(t,1);}}getListeners(e){let t=[],s=e.split(this._delimiter);return this._traverseToPath(this.listeners,s,i=>{t.push(i);}),t[0].__listeners}emit(){if(arguments.length===2&&typeof arguments[0]=="string"&&arguments[1]===d.clearRetain)return this.retainedMessages.delete(arguments[0]),[];let[e,t]=E(arguments,this.options.meta);p(t.parseArgs)&&t.parseArgs(e,t);let s=e.type.split(this._delimiter);t.retain&&this.retainedMessages.set(e.type,e);let i=[],r=[];if(this._traverseToPath(this.listeners,s,o=>{r.push(o);}),p(this._options.onBeforeExecuteListener)){let o=this._options.onBeforeExecuteListener.call(this,e,t);if(Array.isArray(o))return o;if(o===false)throw new g(e.type)}return i.push(...this._executeListeners(r,e,t)),p(this._options.onAfterExecuteListener)&&this._options.onAfterExecuteListener.call(this,e,i,r),this._options.expandEmitResults&&H(i),i}async emitAsync(){return (await Promise.allSettled(this.emit.apply(this,arguments))).map(t=>t.status==="fulfilled"?t.value:t.reason)}waitFor(){let e=arguments[0],t=arguments[1];return new Promise((s,i)=>{let r,o,h=a(c=>{clearTimeout(r),o&&o.off(),s(c);},"listener");t&&t>0&&(r=setTimeout(()=>{o&&o.off(),i(new Error("wait for event<"+e+"> is timeout"));},t)),o=this.on(e,h);})}scope(){let[e,t,s]=L(arguments,this.options.meta,this.options.context),i;return t?i=t:i=new S,i.bind(this,e,s),i}};a(C,"FastEvent");var _=C;var b="::",A="@",Y="data";function Z(n){return n?typeof n=="object"&&"type"in n:false}a(Z,"isFastEventMessage");function w(n,e="/"){let t=Z(n[0]),s=Object.assign({payload:t?n[0].payload:n[1]},t?n[0]:{},{type:`${A}${e}${t?n[0].type:"data"}`}),i=t?n[1]:n[2];return [s,i]}a(w,"parseBroadcaseArgs");var N=class N extends _{constructor(t){super(t);u(this,"nodes");this.nodes=new Map;}add(...t){t.forEach(s=>{if(this.nodes.has(s.id))throw new Error(`Node with id ${s.id} already exists`);s.options.delimiter=this.options.delimiter,s.options.debug=this.options.debug,s.options.ignoreErrors=this.options.ignoreErrors,this.nodes.set(s.id,s),this.emit(`$disconnect${this.options.delimiter}${s.id}`,d.clearRetain),this.emit(`$connect${this.options.delimiter}${s.id}`,s.id,true);});}remove(t){let s=this.nodes.get(t);s&&(s.eventbus=void 0,this.nodes.delete(t),this.emit(`$connect${this.options.delimiter}${s.id}`,d.clearRetain),this.emit(`$disconnect${this.options.delimiter}${s.id}`,s.id,true));}broadcast(){let[t,s]=w(arguments,this.options.delimiter);return this.emit(t,s)}send(t,s){return t.type=`${t.to}${this.options.delimiter}${Y}`,this.emit(t,s)[0]}};a(N,"FastEventBus");var k=N;var D=class D extends _{constructor(t){super(t);u(this,"eventbus");u(this,"_subscribers",[]);this.options.onBeforeExecuteListener=this._onBeforeExecuteListener.bind(this),this.options.onAddListener=this._onAddListener.bind(this),this._subscribers.push(this.on("data"));}_onBeforeExecuteListener(t,s){if(t.type.includes(b))return t.type=t.type.replace(b,this.eventbus.options.delimiter),t.from=this.id,this.eventbus.emit(t,s)}_onAddListener(t,s,i){if(t.includes(b)){let[r,o]=t.split(b);if(r===this.id)return;let h,c=this.eventbus.on(`$connect${this.options.delimiter}*`,f=>{if(f.payload===r){let l=this.eventbus.nodes.get(r);l&&(h=l.on(o,s,i)),c.off();}});return {off:a(()=>h?h.off():c.off(),"off"),listener:h?h.listener:c.listener}}}connect(t){this.eventbus=t,this.eventbus.add(this),this._subscribers.push(this.eventbus.on(`${A}${this.eventbus.options.delimiter}**`,this.onMessage.bind(this))),this._onSubscribeForNode();}disconnect(){this.eventbus?.remove(this.id),this._subscribers.forEach(t=>t.off());}_onSubscribeForNode(){let t=`${this.id}${this.eventbus.options.delimiter}`;this._subscribers.push(this.eventbus.on(`${t}**`,(s,i)=>(s.type=s.type.substring(t.length),z(this.emit(s,i)))));}send(t,s,i){this._assertConnected();let r={type:"data",from:this.id,to:t,payload:s};return this.eventbus.send(r,i)}_assertConnected(){if(!this.eventbus)throw new Error("Node is not connected to any event bus")}broadcast(){this._assertConnected();let[t,s]=w(arguments,this.options.delimiter);return t.from=this.id,this.eventbus.broadcast(t,s)}onMessage(t,s){}};a(D,"FastEventBusNode");var U=D;function lt(n){return n&&typeof n=="string"}a(lt,"isString");function _t(n){return typeof n=="function"&&(n.toString().startsWith("class ")||n.prototype?.constructor===n)}a(_t,"isClass");function bt(n){return n?typeof n=="object"&&"__FastEvent__"in n:false}a(bt,"isFastEvent");exports.AbortError=g;exports.BroadcastEvent=A;exports.CancelError=v;exports.FastEvent=_;exports.FastEventBus=k;exports.FastEventBusNode=U;exports.FastEventDirectives=d;exports.FastEventError=m;exports.FastEventScope=S;exports.NamespaceDelimiter=b;exports.NodeDataEvent=Y;exports.QueueOverflowError=V;exports.TimeoutError=B;exports.UnboundError=y;exports.__FastEventScope__=se;exports.__FastEvent__=te;exports.__expandable__=q;exports.expandable=z;exports.isClass=_t;exports.isExpandable=G;exports.isFastEvent=bt;exports.isFastEventMessage=Z;exports.isFastEventScope=I;exports.isFunction=p;exports.isPathMatched=P;exports.isString=lt;exports.isSubsctiber=J;//# sourceMappingURL=index.js.map
|
|
2
2
|
//# sourceMappingURL=index.js.map
|