fastevent 1.1.2 → 2.0.0
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/devTools.js +2 -2
- package/dist/devTools.js.map +1 -1
- package/dist/devTools.mjs +2 -2
- package/dist/devTools.mjs.map +1 -1
- package/dist/index.d.mts +286 -57
- package/dist/index.d.ts +286 -57
- 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 +64 -50
- package/readme.md +517 -50
- package/.changeset/README.md +0 -8
- package/.changeset/config.json +0 -11
- package/.github/workflows/publish.yaml +0 -50
- package/.prettierrc.js +0 -20
- package/.vscode/launch.json +0 -20
- package/.vscode/settings.json +0 -18
- package/CHANGELOG.md +0 -54
- package/LICENSE +0 -21
- package/bench.png +0 -0
- package/dist/devTools.d.mts +0 -543
- package/dist/devTools.d.ts +0 -543
- package/example/README.md +0 -54
- package/example/eslint.config.js +0 -28
- package/example/index.html +0 -13
- package/example/package.json +0 -29
- package/example/pnpm-lock.yaml +0 -2047
- package/example/public/vite.svg +0 -1
- package/example/src/App.css +0 -42
- package/example/src/App.tsx +0 -60
- package/example/src/assets/react.svg +0 -1
- package/example/src/index.css +0 -68
- package/example/src/main.tsx +0 -10
- package/example/src/myEvent.ts +0 -32
- package/example/src/vite-env.d.ts +0 -1
- package/example/tsconfig.app.json +0 -26
- package/example/tsconfig.json +0 -7
- package/example/tsconfig.node.json +0 -24
- package/example/vite.config.ts +0 -7
- package/packages/native/index.ts +0 -1
- package/packages/turbo/.zig-cache/h/271c82d991949fd7788fd5451f0ca834.txt +0 -0
- package/packages/turbo/.zig-cache/h/timestamp +0 -0
- package/packages/turbo/.zig-cache/o/ebd7ddab8ffe003267120d598aecce68/dependencies.zig +0 -2
- package/packages/turbo/.zig-cache/z/c8114b040daa461a9e2eabd0357554a4 +0 -0
- package/packages/turbo/build.zig +0 -60
- package/packages/turbo/examples/basic.zig +0 -107
- package/packages/turbo/src/event.zig +0 -251
- package/packages/turbo/src/index.zig +0 -70
- package/packages/turbo/src/scope.zig +0 -104
- package/packages/turbo/src/types.zig +0 -88
- package/packages/turbo/src/utils.zig +0 -171
- package/readme_cn.md +0 -491
- package/src/__benchmarks__/index.ts +0 -3
- package/src/__benchmarks__/multi-level.ts +0 -40
- package/src/__benchmarks__/sample.ts +0 -40
- package/src/__benchmarks__/wildcard.ts +0 -41
- package/src/__tests__/emit.test.ts +0 -106
- package/src/__tests__/emitAsync.test.ts +0 -64
- package/src/__tests__/isPathMatched.test.ts +0 -205
- package/src/__tests__/many.test.ts +0 -22
- package/src/__tests__/meta.test.ts +0 -28
- package/src/__tests__/off.test.ts +0 -214
- package/src/__tests__/onany.test.ts +0 -212
- package/src/__tests__/once.test.ts +0 -70
- package/src/__tests__/retain.test.ts +0 -66
- package/src/__tests__/scope.test.ts +0 -110
- package/src/__tests__/types.test.ts +0 -145
- package/src/__tests__/waitFor.test.ts +0 -116
- package/src/__tests__/wildcard.test.ts +0 -185
- package/src/devTools.ts +0 -166
- package/src/event.ts +0 -741
- package/src/index.ts +0 -3
- package/src/scope.ts +0 -130
- package/src/types.ts +0 -69
- package/src/utils/WeakObjectMap.ts +0 -64
- package/src/utils/isPathMatched.ts +0 -40
- package/src/utils/removeItem.ts +0 -16
- package/tsconfig.json +0 -104
- package/tsup.config.ts +0 -30
package/dist/index.d.mts
CHANGED
|
@@ -1,67 +1,188 @@
|
|
|
1
|
-
type
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
type FastListenerPipe = (listener: FastEventListener) => FastEventListener;
|
|
2
|
+
|
|
3
|
+
interface FaseEventMessageExtends {
|
|
4
|
+
}
|
|
5
|
+
interface FastEventMeta {
|
|
6
|
+
priority?: number;
|
|
7
|
+
}
|
|
8
|
+
type FastEventMessage<Events extends Record<string, any> = Record<string, any>, M = any> = ({
|
|
9
|
+
[K in keyof Events]: {
|
|
10
|
+
type: Exclude<K, number | symbol>;
|
|
11
|
+
payload: Events[K];
|
|
12
|
+
meta: FastEventMeta & M & Record<string, any>;
|
|
13
|
+
};
|
|
14
|
+
}[Exclude<keyof Events, number | symbol>]) & FaseEventMessageExtends;
|
|
15
|
+
type FastEventEmitMessage<Events extends Record<string, any> = Record<string, any>, M = any> = ({
|
|
16
|
+
[K in keyof Events]: {
|
|
17
|
+
type: Exclude<K, number | symbol>;
|
|
18
|
+
payload?: Events[K];
|
|
19
|
+
meta?: FastEventMeta & M & Record<string, any>;
|
|
20
|
+
};
|
|
21
|
+
}[Exclude<keyof Events, number | symbol>]) & FaseEventMessageExtends;
|
|
22
|
+
type FastEventListener<T extends string = string, P = any, M = any, C = any> = (this: C, message: FastEventMessage<{
|
|
23
|
+
[K in T]: P;
|
|
24
|
+
}, M>, args: FastEventListenerArgs<M>) => any | Promise<any>;
|
|
25
|
+
type FastEventAnyListener<Events extends Record<string, any> = Record<string, any>, Meta = never, Context = any> = (this: Context, message: FastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => any | Promise<any>;
|
|
26
|
+
/**
|
|
27
|
+
* [监听器函数引用,需要执行多少次,实际执行的次数(用于负载均衡时记录)]
|
|
28
|
+
*/
|
|
29
|
+
type FastListenerMeta = [FastEventListener<any, any>, number, number];
|
|
7
30
|
type FastListenerNode = {
|
|
8
|
-
__listeners:
|
|
31
|
+
__listeners: FastListenerMeta[];
|
|
9
32
|
} & {
|
|
10
33
|
[key: string]: FastListenerNode;
|
|
11
34
|
};
|
|
12
35
|
type FastEventSubscriber = {
|
|
13
36
|
off: () => void;
|
|
37
|
+
/**
|
|
38
|
+
* 为什么要有一个listener引用? 主要用于移除侦听器时使用
|
|
39
|
+
*
|
|
40
|
+
* - 正常情况下
|
|
41
|
+
* const subscriber = emitter.on('event', listener)
|
|
42
|
+
*
|
|
43
|
+
* subscriber.off()
|
|
44
|
+
* emitter.off('event', listener)
|
|
45
|
+
* emitter.off(listener)
|
|
46
|
+
*
|
|
47
|
+
* - 在使用scope时
|
|
48
|
+
* const scope = emitter.scope("xxx")
|
|
49
|
+
* const subscriber = scope.on('event', listener)
|
|
50
|
+
*
|
|
51
|
+
* subscriber.off() 可以正常生效
|
|
52
|
+
* scope.off('event', listener) // 无法生效
|
|
53
|
+
* scope.off(listener) // 无法生效
|
|
54
|
+
* 因为在scope中,为了让侦听器可以处理scope的逻辑,对listener进行了包装,
|
|
55
|
+
* 因此在事件注册表中登记的不是listener,而是经过包装的侦听器
|
|
56
|
+
* subscriber.off() 可以正常生效
|
|
57
|
+
* 如果要使用scope.off或emitter.off
|
|
58
|
+
* 需要使用subscriber.listener, subscriber.listener记录了原始的侦听器引用
|
|
59
|
+
* subscriber.listener===listener
|
|
60
|
+
*
|
|
61
|
+
* scope.off('event', subscriber.listener) // 生效
|
|
62
|
+
* scope.off(subscriber.listener) // 生效
|
|
63
|
+
*
|
|
64
|
+
*/
|
|
65
|
+
listener: FastEventListener<any, any, any>;
|
|
14
66
|
};
|
|
15
|
-
interface FastEventListenerMeta {
|
|
16
|
-
emitter?: string;
|
|
17
|
-
}
|
|
18
67
|
type FastListeners = FastListenerNode;
|
|
19
|
-
type FastEventOptions<
|
|
68
|
+
type FastEventOptions<Meta = Record<string, any>, Context = any> = {
|
|
20
69
|
id?: string;
|
|
21
70
|
debug?: boolean;
|
|
22
71
|
delimiter?: string;
|
|
23
|
-
context?:
|
|
72
|
+
context?: Context;
|
|
24
73
|
ignoreErrors?: boolean;
|
|
25
|
-
|
|
26
|
-
meta?: M;
|
|
74
|
+
meta?: Meta;
|
|
27
75
|
onAddListener?: (type: string[], listener: FastEventListener) => void;
|
|
28
76
|
onRemoveListener?: (type: string[], listener: FastEventListener) => void;
|
|
29
77
|
onClearListeners?: () => void;
|
|
30
|
-
|
|
78
|
+
onListenerError?: ((listener: FastEventListener, error: Error, message: FastEventMessage<any, Meta>, args: FastEventListenerArgs<Meta> | undefined) => void);
|
|
79
|
+
onBeforeExecuteListener?: (message: FastEventMessage<any, Meta>, args: FastEventListenerArgs<Meta>) => boolean | void;
|
|
80
|
+
onAfterExecuteListener?: (message: FastEventMessage<any, Meta>, returns: any[], listeners: FastListenerNode[]) => void;
|
|
81
|
+
/**
|
|
82
|
+
* 全局执行器
|
|
83
|
+
* allSettled: 使用Promise.allSettled()执行所有监听器
|
|
84
|
+
* race: 使用Promise.race()执行所有监听器,只有第一个执行完成就返回,其他监听器执行结果会被忽略
|
|
85
|
+
* balance: 尽可能平均执行各个侦听器
|
|
86
|
+
* sequence: 按照侦听器添加顺序依次执行
|
|
87
|
+
*/
|
|
88
|
+
executor?: FastListenerExecutorArgs;
|
|
31
89
|
};
|
|
32
|
-
|
|
33
|
-
|
|
90
|
+
interface FastEvents {
|
|
91
|
+
}
|
|
92
|
+
type PickScopeEvents<T extends Record<string, any>, Prefix extends string> = {
|
|
34
93
|
[K in keyof T as K extends `${Prefix}/${infer Rest}` ? Rest : never]: T[K];
|
|
35
94
|
};
|
|
36
|
-
type
|
|
95
|
+
type ScopeEvents<T extends Record<string, any>, Prefix extends string> = PickScopeEvents<T, Prefix>;
|
|
96
|
+
type FastEventListenOptions<Events extends Record<string, any> = Record<string, any>, Meta = any> = {
|
|
37
97
|
count?: number;
|
|
38
98
|
prepend?: boolean;
|
|
99
|
+
filter?: (message: FastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => boolean;
|
|
100
|
+
off?: (message: FastEventMessage<Events, Meta>, args: FastEventListenerArgs<Meta>) => boolean;
|
|
101
|
+
pipes?: FastListenerPipe[];
|
|
102
|
+
};
|
|
103
|
+
type FastListenerExecutorArgs = 'default' | 'allSettled' | 'race' | 'balance' | 'first' | 'last' | 'random' | IFastListenerExecutor;
|
|
104
|
+
type FastEventListenerArgs<M = Record<string, any>> = {
|
|
105
|
+
retain?: boolean;
|
|
106
|
+
meta?: Partial<M> & Record<string, any>;
|
|
107
|
+
abortSignal?: AbortSignal;
|
|
108
|
+
/**
|
|
109
|
+
*
|
|
110
|
+
* allSettled: 使用Promise.allSettled()执行所有监听器
|
|
111
|
+
* race: 使用Promise.race()执行所有监听器,只有第一个执行完成就返回,其他监听器执行结果会被忽略
|
|
112
|
+
* balance: 尽可能平均执行各个侦听器
|
|
113
|
+
* sequence: 按照侦听器添加顺序依次执行
|
|
114
|
+
*/
|
|
115
|
+
executor?: FastListenerExecutorArgs;
|
|
116
|
+
};
|
|
117
|
+
type Merge<T extends object, U extends object> = {
|
|
118
|
+
[K in keyof T | keyof U]: K extends keyof U ? U[K] : K extends keyof T ? T[K] : never;
|
|
39
119
|
};
|
|
120
|
+
type RequiredItems<T extends object, Items extends string[]> = Omit<T, Items[number]> & {
|
|
121
|
+
[K in Items[number] & keyof T]-?: Exclude<T[K], undefined>;
|
|
122
|
+
};
|
|
123
|
+
type Fallback<T, F> = [
|
|
124
|
+
T
|
|
125
|
+
] extends [never] ? F : T extends undefined ? F : T;
|
|
126
|
+
type IFastListenerExecutor = (listeners: FastListenerMeta[], message: FastEventMessage, args: FastEventListenerArgs | undefined, execute: (listener: FastEventListener, message: FastEventMessage, args?: FastEventListenerArgs) => Promise<any> | any) => Promise<any[]> | any[];
|
|
40
127
|
|
|
41
|
-
|
|
42
|
-
|
|
128
|
+
type FastEventScopeOptions<Meta, Context> = {
|
|
129
|
+
meta?: FastEventMeta & Meta;
|
|
130
|
+
context?: Context;
|
|
131
|
+
executor?: FastListenerExecutorArgs;
|
|
132
|
+
};
|
|
133
|
+
type FastEventScopeMeta = {
|
|
134
|
+
scope: string;
|
|
135
|
+
};
|
|
136
|
+
declare class FastEventScope<Events extends Record<string, any> = Record<string, any>, Meta extends Record<string, any> = Record<string, any>, Context = any, Types extends keyof Events = keyof Events, FinalMeta extends Record<string, any> = Meta & FastEventScopeMeta> {
|
|
137
|
+
emitter: FastEvent<Events>;
|
|
43
138
|
prefix: string;
|
|
44
|
-
|
|
139
|
+
options: Required<FastEventScopeOptions<FinalMeta, Context>>;
|
|
140
|
+
events: Events;
|
|
141
|
+
constructor(emitter: FastEvent<Events>, prefix: string, options?: FastEventScopeOptions<Meta, Context>);
|
|
142
|
+
get context(): Context;
|
|
143
|
+
/**
|
|
144
|
+
* 获取作用域监听器
|
|
145
|
+
* 当启用作用域时,对原始监听器进行包装,添加作用域前缀处理逻辑
|
|
146
|
+
* @param listener 原始事件监听器
|
|
147
|
+
* @returns 包装后的作用域监听器
|
|
148
|
+
* @private
|
|
149
|
+
*/
|
|
45
150
|
private _getScopeListener;
|
|
46
151
|
private _getScopeType;
|
|
47
152
|
private _fixScopeType;
|
|
48
|
-
on<T extends Types = Types>(type: T, listener: FastEventListener<T, Events[T],
|
|
49
|
-
on<T extends string>(type: T, listener: FastEventListener<
|
|
50
|
-
on(type: '**', listener:
|
|
51
|
-
once<T extends
|
|
52
|
-
once<T extends
|
|
53
|
-
onAny<P = any>(listener:
|
|
54
|
-
|
|
153
|
+
on<T extends Types = Types>(type: T, listener: FastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Context>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
154
|
+
on<T extends string>(type: T, listener: FastEventListener<string, any, FinalMeta, Context>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
155
|
+
on(type: '**', listener: FastEventAnyListener<Events, FinalMeta, Context>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
156
|
+
once<T extends Types = Types>(type: T, listener: FastEventListener<Exclude<T, number | symbol>, Events[T], FinalMeta, Context>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
157
|
+
once<T extends string>(type: T, listener: FastEventListener<string, any, FinalMeta, Context>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
158
|
+
onAny<P = any>(listener: FastEventAnyListener<{
|
|
159
|
+
[K: string]: P;
|
|
160
|
+
}, FinalMeta, Context>, options?: Pick<FastEventListenOptions, 'prepend'>): FastEventSubscriber;
|
|
55
161
|
off(listener: FastEventListener<any, any, any>): void;
|
|
56
162
|
off(type: string, listener: FastEventListener<any, any, any>): void;
|
|
57
163
|
off(type: Types, listener: FastEventListener<any, any, any>): void;
|
|
58
164
|
off(type: string): void;
|
|
59
165
|
off(type: Types): void;
|
|
166
|
+
offAll(): void;
|
|
60
167
|
clear(): void;
|
|
61
|
-
emit<R = any>(type: Types, payload?: Events[Types],
|
|
62
|
-
emit<R = any>(type:
|
|
63
|
-
|
|
64
|
-
|
|
168
|
+
emit<R = any>(type: Types, payload?: Events[Types], options?: FastEventListenerArgs<FinalMeta>): R[];
|
|
169
|
+
emit<R = any, T extends string = string>(type: T, payload?: T extends Types ? Events[Types] : any, options?: FastEventListenerArgs<FinalMeta>): R[];
|
|
170
|
+
emit<R = any>(message: FastEventEmitMessage<Events, FinalMeta>, options?: FastEventListenerArgs<FinalMeta>): R[];
|
|
171
|
+
emit<R = any, T extends string = string>(message: FastEventEmitMessage<{
|
|
172
|
+
[K in T]: K extends Types ? Events[K] : any;
|
|
173
|
+
}, FinalMeta>, options?: FastEventListenerArgs<FinalMeta>): R[];
|
|
174
|
+
emitAsync<R = any>(type: string, payload?: any, options?: FastEventListenerArgs<FinalMeta>): Promise<[R | Error][]>;
|
|
175
|
+
emitAsync<R = any>(type: Types, payload?: Events[Types], options?: FastEventListenerArgs<FinalMeta>): Promise<[R | Error][]>;
|
|
176
|
+
emitAsync<R = any>(message: FastEventMessage<Events, FinalMeta>, options?: FastEventListenerArgs<FinalMeta>): Promise<[R | Error][]>;
|
|
177
|
+
waitFor<T extends Types>(type: T, timeout?: number): Promise<FastEventMessage<{
|
|
178
|
+
[key in T]: Events[T];
|
|
179
|
+
}, FinalMeta>>;
|
|
180
|
+
waitFor(type: string, timeout?: number): Promise<FastEventMessage<{
|
|
181
|
+
[key: string]: any;
|
|
182
|
+
}, FinalMeta>>;
|
|
183
|
+
waitFor<P = any>(type: string, timeout?: number): Promise<FastEventMessage<{
|
|
184
|
+
[key: string]: P;
|
|
185
|
+
}, FinalMeta>>;
|
|
65
186
|
/**
|
|
66
187
|
* 创建一个新的作用域实例
|
|
67
188
|
* @param prefix - 作用域前缀
|
|
@@ -94,7 +215,7 @@ declare class FastEventScope<Events extends FastEvents = FastEvents, Meta extend
|
|
|
94
215
|
* profileScope.emit('update', { name: 'John' });
|
|
95
216
|
* ```
|
|
96
217
|
*/
|
|
97
|
-
scope(prefix:
|
|
218
|
+
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?: FastEventScopeOptions<Partial<FinalMeta> & M, C>): FastEventScope<PickScopeEvents<Events, P> & E, FinalMeta & M, C, keyof PickScopeEvents<Events, P> | keyof E, FinalMeta & M & FastEventScopeMeta>;
|
|
98
219
|
}
|
|
99
220
|
|
|
100
221
|
/**
|
|
@@ -104,7 +225,7 @@ declare class FastEventScope<Events extends FastEvents = FastEvents, Meta extend
|
|
|
104
225
|
* @template Meta - 事件元数据类型,默认为任意键值对对象
|
|
105
226
|
* @template Types - 事件类型的键名类型,默认为Events的键名类型
|
|
106
227
|
*/
|
|
107
|
-
declare class FastEvent<Events extends
|
|
228
|
+
declare class FastEvent<Events extends Record<string, any> = Record<string, any>, Meta extends Record<string, any> = Record<string, any>, Context = never, AllEvents extends Record<string, any> = Events & FastEvents, Types extends keyof AllEvents = Exclude<keyof (AllEvents), number | symbol>> {
|
|
108
229
|
/** 事件监听器树结构,存储所有注册的事件监听器 */
|
|
109
230
|
listeners: FastListeners;
|
|
110
231
|
/** 事件发射器的配置选项 */
|
|
@@ -117,6 +238,7 @@ declare class FastEvent<Events extends FastEvents = FastEvents, Meta extends Rec
|
|
|
117
238
|
retainedMessages: Map<string, any>;
|
|
118
239
|
/** 当前注册的监听器总数 */
|
|
119
240
|
listenerCount: number;
|
|
241
|
+
events: Events;
|
|
120
242
|
/**
|
|
121
243
|
* 创建FastEvent实例
|
|
122
244
|
* @param options - 事件发射器的配置选项
|
|
@@ -128,9 +250,10 @@ declare class FastEvent<Events extends FastEvents = FastEvents, Meta extends Rec
|
|
|
128
250
|
* - context: null - 监听器执行上下文
|
|
129
251
|
* - ignoreErrors: true - 是否忽略监听器执行错误
|
|
130
252
|
*/
|
|
131
|
-
constructor(options?: FastEventOptions<Meta>);
|
|
253
|
+
constructor(options?: FastEventOptions<Meta, Context>);
|
|
132
254
|
/** 获取事件发射器的配置选项 */
|
|
133
|
-
get options(): FastEventOptions
|
|
255
|
+
get options(): RequiredItems<FastEventOptions<Meta, Context>, ["meta", "context"]>;
|
|
256
|
+
get context(): Context;
|
|
134
257
|
/** 获取事件发射器的唯一标识符 */
|
|
135
258
|
get id(): string;
|
|
136
259
|
private _addListener;
|
|
@@ -152,6 +275,7 @@ declare class FastEvent<Events extends FastEvents = FastEvents, Meta extends Rec
|
|
|
152
275
|
* @description 遍历节点的监听器列表,移除所有匹配的监听器。支持移除普通函数和数组形式的监听器
|
|
153
276
|
*/
|
|
154
277
|
private _removeListener;
|
|
278
|
+
private _pipeListener;
|
|
155
279
|
/**
|
|
156
280
|
* 注册事件监听器
|
|
157
281
|
* @param type - 事件类型,支持以下格式:
|
|
@@ -176,9 +300,12 @@ declare class FastEvent<Events extends FastEvents = FastEvents, Meta extends Rec
|
|
|
176
300
|
* emitter.on('event', handler, { count: 3 });
|
|
177
301
|
* ```
|
|
178
302
|
*/
|
|
179
|
-
on<T extends
|
|
180
|
-
on<T extends
|
|
181
|
-
on
|
|
303
|
+
on<T extends Types = Types>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
304
|
+
on<T extends string>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
305
|
+
on(type: '**', options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
306
|
+
on<T extends Types = Types>(type: T, listener: FastEventListener<Exclude<T, number | symbol>, AllEvents[T], Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
307
|
+
on<T extends string>(type: T, listener: FastEventAnyListener<AllEvents, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
308
|
+
on(type: '**', listener: FastEventAnyListener<Record<string, any>, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
182
309
|
/**
|
|
183
310
|
* 注册一次性事件监听器
|
|
184
311
|
* @param type - 事件类型,支持与on方法相同的格式:
|
|
@@ -200,8 +327,10 @@ declare class FastEvent<Events extends FastEvents = FastEvents, Meta extends Rec
|
|
|
200
327
|
* });
|
|
201
328
|
* ```
|
|
202
329
|
*/
|
|
203
|
-
once<T extends Types = Types>(type: T,
|
|
204
|
-
once<T extends string>(type: T,
|
|
330
|
+
once<T extends Types = Types>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
331
|
+
once<T extends string>(type: T, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
332
|
+
once<T extends Types = Types>(type: T, listener: FastEventListener<Exclude<T, number | symbol>, AllEvents[T], Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
333
|
+
once<T extends string>(type: T, listener: FastEventAnyListener<AllEvents, Meta, Fallback<Context, typeof this>>, options?: FastEventListenOptions<AllEvents, Meta>): FastEventSubscriber;
|
|
205
334
|
/**
|
|
206
335
|
* 注册一个监听器,用于监听所有事件
|
|
207
336
|
* @param listener 事件监听器函数,可以接收任意类型的事件数据
|
|
@@ -214,9 +343,18 @@ declare class FastEvent<Events extends FastEvents = FastEvents, Meta extends Rec
|
|
|
214
343
|
*
|
|
215
344
|
* // 取消监听
|
|
216
345
|
* subscriber.off();
|
|
217
|
-
* ```
|
|
346
|
+
* ```listener: FastEventAnyListener<AllEvents, Meta, Fallback<Context, typeof this>>): FastEventSubscriber
|
|
347
|
+
*/
|
|
348
|
+
onAny(options?: Omit<FastEventListenOptions<AllEvents, Meta>, 'count'>): FastEventSubscriber;
|
|
349
|
+
onAny<P = any>(listener: FastEventAnyListener<Record<string, P>, Meta, Fallback<Context, typeof this>>, options?: Omit<FastEventListenOptions<AllEvents, Meta>, 'count'>): FastEventSubscriber;
|
|
350
|
+
/**
|
|
351
|
+
*
|
|
352
|
+
* 当调用on/once/onAny时如果没有指定监听器,则调用此方法
|
|
353
|
+
*
|
|
354
|
+
* 此方法供子类继承
|
|
355
|
+
*
|
|
218
356
|
*/
|
|
219
|
-
|
|
357
|
+
onMessage(message: FastEventMessage): void;
|
|
220
358
|
off(listener: FastEventListener<any, any, any>): void;
|
|
221
359
|
off(type: string, listener: FastEventListener<any, any, any>): void;
|
|
222
360
|
off(type: Types, listener: FastEventListener<any, any, any>): void;
|
|
@@ -230,7 +368,7 @@ declare class FastEvent<Events extends FastEvents = FastEvents, Meta extends Rec
|
|
|
230
368
|
* - 如果没有提供prefix,则清除所有监听器
|
|
231
369
|
* - 同时会清空保留的事件(_retainedEvents)
|
|
232
370
|
* - 重置监听器对象为空
|
|
233
|
-
|
|
371
|
+
|
|
234
372
|
* @example
|
|
235
373
|
*
|
|
236
374
|
* ```ts
|
|
@@ -239,7 +377,6 @@ declare class FastEvent<Events extends FastEvents = FastEvents, Meta extends Rec
|
|
|
239
377
|
*
|
|
240
378
|
*/
|
|
241
379
|
offAll(entry?: string): void;
|
|
242
|
-
private _getListenerNode;
|
|
243
380
|
/**
|
|
244
381
|
* 移除保留的事件
|
|
245
382
|
* @param prefix - 事件前缀。如果不提供,将清除所有保留的事件。
|
|
@@ -248,8 +385,7 @@ declare class FastEvent<Events extends FastEvents = FastEvents, Meta extends Rec
|
|
|
248
385
|
* @private
|
|
249
386
|
*/
|
|
250
387
|
private _removeRetainedEvents;
|
|
251
|
-
clear(): void;
|
|
252
|
-
private _createMeta;
|
|
388
|
+
clear(prefix?: string): void;
|
|
253
389
|
private _emitForLastEvent;
|
|
254
390
|
/**
|
|
255
391
|
* 遍历监听器节点树
|
|
@@ -267,6 +403,7 @@ declare class FastEvent<Events extends FastEvents = FastEvents, Meta extends Rec
|
|
|
267
403
|
*/
|
|
268
404
|
private _traverseToPath;
|
|
269
405
|
private _traverseListeners;
|
|
406
|
+
private _onListenerError;
|
|
270
407
|
/**
|
|
271
408
|
* 执行单个监听器函数
|
|
272
409
|
* @param listener - 要执行的监听器函数或包装过的监听器对象
|
|
@@ -285,6 +422,13 @@ declare class FastEvent<Events extends FastEvents = FastEvents, Meta extends Rec
|
|
|
285
422
|
* - 否则抛出错误
|
|
286
423
|
*/
|
|
287
424
|
private _executeListener;
|
|
425
|
+
private _getListenerExecutor;
|
|
426
|
+
/**
|
|
427
|
+
* 触发事件并执行对应的监听器
|
|
428
|
+
* @param type - 事件类型字符串或包含事件信息的对象
|
|
429
|
+
* @param payload - 事件携带的数据负载
|
|
430
|
+
return
|
|
431
|
+
}
|
|
288
432
|
/**
|
|
289
433
|
* 执行监听器节点中的所有监听函数
|
|
290
434
|
* @param node - FastListenerNode类型的监听器节点
|
|
@@ -353,10 +497,18 @@ declare class FastEvent<Events extends FastEvents = FastEvents, Meta extends Rec
|
|
|
353
497
|
* }, true);
|
|
354
498
|
* ```
|
|
355
499
|
*/
|
|
356
|
-
emit<R = any>(type:
|
|
357
|
-
emit<R = any>(type:
|
|
358
|
-
emit<R = any
|
|
359
|
-
|
|
500
|
+
emit<R = any, T extends Types = Types>(type: T, payload?: AllEvents[T], retain?: boolean): R[];
|
|
501
|
+
emit<R = any, T extends string = string>(type: T, payload?: T extends Types ? AllEvents[Types] : any, retain?: boolean): R[];
|
|
502
|
+
emit<R = any, T extends string = string>(message: FastEventEmitMessage<{
|
|
503
|
+
[K in T]: K extends Types ? AllEvents[K] : any;
|
|
504
|
+
}, Meta>, retain?: boolean): R[];
|
|
505
|
+
emit<R = any>(message: FastEventEmitMessage<AllEvents, Meta>, retain?: boolean): R[];
|
|
506
|
+
emit<R = any, T extends Types = Types>(type: T, payload?: AllEvents[T], options?: FastEventListenerArgs<Meta>): R[];
|
|
507
|
+
emit<R = any, T extends string = string>(type: T, payload?: T extends Types ? AllEvents[Types] : any, options?: FastEventListenerArgs<Meta>): R[];
|
|
508
|
+
emit<R = any, T extends string = string>(message: FastEventEmitMessage<{
|
|
509
|
+
[K in T]: K extends Types ? AllEvents[K] : any;
|
|
510
|
+
}, Meta>, options?: FastEventListenerArgs<Meta>): R[];
|
|
511
|
+
emit<R = any>(message: FastEventEmitMessage<AllEvents, Meta>, options?: FastEventListenerArgs<Meta>): R[];
|
|
360
512
|
/**
|
|
361
513
|
* 异步触发事件
|
|
362
514
|
* @param type - 事件类型,可以是字符串或预定义的事件类型
|
|
@@ -393,8 +545,18 @@ declare class FastEvent<Events extends FastEvents = FastEvents, Meta extends Rec
|
|
|
393
545
|
* });
|
|
394
546
|
* ```
|
|
395
547
|
*/
|
|
396
|
-
emitAsync<R = any>(type: string, payload?: any, retain?: boolean
|
|
397
|
-
emitAsync<R = any>(type: Types, payload?:
|
|
548
|
+
emitAsync<R = any>(type: string, payload?: any, retain?: boolean): Promise<[R | Error][]>;
|
|
549
|
+
emitAsync<R = any>(type: Types, payload?: AllEvents[Types], retain?: boolean): Promise<[R | Error][]>;
|
|
550
|
+
emitAsync<R = any, T extends string = string>(message: FastEventEmitMessage<{
|
|
551
|
+
[K in T]: K extends Types ? AllEvents[K] : any;
|
|
552
|
+
}, Meta>, retain?: boolean): Promise<[R | Error][]>;
|
|
553
|
+
emitAsync<R = any>(message: FastEventEmitMessage<AllEvents, Meta>, retain?: boolean): Promise<[R | Error][]>;
|
|
554
|
+
emitAsync<R = any>(type: string, payload?: any, options?: FastEventListenerArgs<Meta>): Promise<[R | Error][]>;
|
|
555
|
+
emitAsync<R = any>(type: Types, payload?: AllEvents[Types], options?: FastEventListenerArgs<Meta>): Promise<[R | Error][]>;
|
|
556
|
+
emitAsync<R = any, T extends string = string>(message: FastEventEmitMessage<{
|
|
557
|
+
[K in T]: K extends Types ? AllEvents[K] : any;
|
|
558
|
+
}, Meta>, options?: FastEventListenerArgs<Meta>): Promise<[R | Error][]>;
|
|
559
|
+
emitAsync<R = any>(message: FastEventEmitMessage<AllEvents, Meta>, options?: FastEventListenerArgs<Meta>): Promise<[R | Error][]>;
|
|
398
560
|
/**
|
|
399
561
|
* 等待指定事件发生,返回一个Promise
|
|
400
562
|
* @param type - 要等待的事件类型
|
|
@@ -422,8 +584,13 @@ declare class FastEvent<Events extends FastEvents = FastEvents, Meta extends Rec
|
|
|
422
584
|
* console.log('服务器就绪');
|
|
423
585
|
* ```
|
|
424
586
|
*/
|
|
425
|
-
waitFor<T extends Types
|
|
426
|
-
|
|
587
|
+
waitFor<T extends Types>(type: T, timeout?: number): Promise<FastEventMessage<{
|
|
588
|
+
[key in T]: AllEvents[T];
|
|
589
|
+
}, Meta>>;
|
|
590
|
+
waitFor(type: string, timeout?: number): Promise<FastEventMessage<AllEvents, Meta>>;
|
|
591
|
+
waitFor<P = any>(type: string, timeout?: number): Promise<FastEventMessage<{
|
|
592
|
+
[key: string]: P;
|
|
593
|
+
}, Meta>>;
|
|
427
594
|
/**
|
|
428
595
|
* 创建一个新的事件作用域
|
|
429
596
|
* @param prefix - 作用域前缀,将自动添加到该作用域下所有事件名称前
|
|
@@ -464,7 +631,69 @@ declare class FastEvent<Events extends FastEvents = FastEvents, Meta extends Rec
|
|
|
464
631
|
* userEvents.offAll(); // 清理 'user' 前缀下的所有事件
|
|
465
632
|
* ```
|
|
466
633
|
*/
|
|
467
|
-
scope<
|
|
634
|
+
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?: FastEventScopeOptions<M, C>): FastEventScope<PickScopeEvents<AllEvents, P> & E, Meta & M, C, keyof PickScopeEvents<AllEvents, P> | keyof E, Meta & M & FastEventScopeMeta>;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
interface FastEventListenerDecorators {
|
|
638
|
+
queue: string;
|
|
639
|
+
}
|
|
640
|
+
type FastQueuePriority = 'none';
|
|
641
|
+
type FastQueueOverflows = "drop" | "expand" | 'slide' | 'throw';
|
|
642
|
+
type QueueListenerPipeOptions = {
|
|
643
|
+
size?: number;
|
|
644
|
+
maxExpandSize?: number;
|
|
645
|
+
expandOverflow?: Omit<FastQueueOverflows, 'expand'>;
|
|
646
|
+
overflow?: FastQueueOverflows;
|
|
647
|
+
onNew?: (newMessage: FastEventMessage, messages: FastEventMessage[]) => void;
|
|
648
|
+
};
|
|
649
|
+
declare const queue: (options?: QueueListenerPipeOptions) => FastListenerPipe;
|
|
650
|
+
declare const dropping: (size?: number) => FastListenerPipe;
|
|
651
|
+
declare const sliding: (size?: number) => FastListenerPipe;
|
|
652
|
+
declare const expanding: (options?: Omit<QueueListenerPipeOptions, "overflow">) => FastListenerPipe;
|
|
653
|
+
|
|
654
|
+
/**
|
|
655
|
+
* 创建一个超时装饰器,限制监听器函数的执行时间
|
|
656
|
+
* @param ms 超时时间(毫秒)
|
|
657
|
+
* @param defaultValue 可选的默认返回值,如果提供则超时时返回此值,否则抛出TimeoutError
|
|
658
|
+
* @returns 装饰器函数
|
|
659
|
+
*/
|
|
660
|
+
declare const timeout: <T = any>(ms: number, defaultValue?: T) => FastListenerPipe;
|
|
661
|
+
|
|
662
|
+
interface DebounceOptions {
|
|
663
|
+
/**
|
|
664
|
+
* 当消息被丢弃时的回调函数
|
|
665
|
+
*/
|
|
666
|
+
drop?: (message: FastEventMessage) => void;
|
|
667
|
+
}
|
|
668
|
+
/**
|
|
669
|
+
* 创建一个防抖动装饰器,限制监听器函数的执行频率
|
|
670
|
+
* @param ms 防抖动时间(毫秒)
|
|
671
|
+
* @param options 可选的配置项
|
|
672
|
+
* @returns 装饰器函数
|
|
673
|
+
*/
|
|
674
|
+
declare const debounce: (ms: number, options?: DebounceOptions) => FastListenerPipe;
|
|
675
|
+
|
|
676
|
+
interface ThrottleOptions {
|
|
677
|
+
/**
|
|
678
|
+
* 当消息被丢弃时的回调函数
|
|
679
|
+
*/
|
|
680
|
+
drop?: (message: FastEventMessage) => void;
|
|
681
|
+
}
|
|
682
|
+
/**
|
|
683
|
+
* 创建一个节流装饰器,限制监听器函数的执行频率
|
|
684
|
+
* @param interval 节流时间间隔(毫秒)
|
|
685
|
+
* @param options 可选的配置项
|
|
686
|
+
* @returns 装饰器函数
|
|
687
|
+
*/
|
|
688
|
+
declare const throttle: (interval: number, options?: ThrottleOptions) => FastListenerPipe;
|
|
689
|
+
|
|
690
|
+
declare class FastEventError extends Error {
|
|
691
|
+
}
|
|
692
|
+
declare class TimeoutError extends FastEventError {
|
|
693
|
+
}
|
|
694
|
+
declare class AbortError extends FastEventError {
|
|
695
|
+
}
|
|
696
|
+
declare class QueueOverflowError extends FastEventError {
|
|
468
697
|
}
|
|
469
698
|
|
|
470
|
-
export { FastEvent, type FastEventListenOptions, type FastEventListener, type
|
|
699
|
+
export { AbortError, type DebounceOptions, type Fallback, type FaseEventMessageExtends, FastEvent, type FastEventAnyListener, type FastEventEmitMessage, FastEventError, type FastEventListenOptions, type FastEventListener, type FastEventListenerArgs, type FastEventListenerDecorators, type FastEventMessage, type FastEventMeta, type FastEventOptions, FastEventScope, type FastEventScopeMeta, type FastEventScopeOptions, type FastEventSubscriber, type FastEvents, type FastListenerExecutorArgs, type FastListenerMeta, type FastListenerNode, type FastListenerPipe, type FastListeners, type FastQueueOverflows, type FastQueuePriority, type IFastListenerExecutor, type Merge, type PickScopeEvents, type QueueListenerPipeOptions, QueueOverflowError, type RequiredItems, type ScopeEvents, type ThrottleOptions, TimeoutError, debounce, dropping, expanding, queue, sliding, throttle, timeout };
|