fastevent 1.1.1 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/devTools.d.mts +269 -34
- package/dist/devTools.d.ts +269 -34
- package/dist/index.d.mts +270 -38
- package/dist/index.d.ts +270 -38
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/example/src/myEvent.ts +32 -0
- package/package.json +1 -1
- package/packages/native/index.ts +1 -0
- 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 +2 -0
- package/packages/turbo/.zig-cache/z/c8114b040daa461a9e2eabd0357554a4 +0 -0
- package/packages/turbo/build.zig +60 -0
- package/packages/turbo/examples/basic.zig +107 -0
- package/packages/turbo/src/event.zig +251 -0
- package/packages/turbo/src/index.zig +70 -0
- package/packages/turbo/src/scope.zig +104 -0
- package/packages/turbo/src/types.zig +88 -0
- package/packages/turbo/src/utils.zig +171 -0
- package/src/event.ts +232 -21
- package/src/scope.ts +39 -6
- package/src/types.ts +5 -7
package/CHANGELOG.md
CHANGED
package/dist/devTools.d.mts
CHANGED
|
@@ -41,12 +41,12 @@ declare class WeakObjectMap<T extends object> {
|
|
|
41
41
|
has(key: string): boolean;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
interface FastEventMessage<T = string, P = any, M = unknown> {
|
|
45
45
|
type: T;
|
|
46
46
|
payload: P;
|
|
47
47
|
meta: M;
|
|
48
|
-
}
|
|
49
|
-
type FastEventListener<T = string, P = any, M = unknown> = (message: FastEventMessage<T, P, M>) => any | Promise<any>;
|
|
48
|
+
}
|
|
49
|
+
type FastEventListener<T = string, P = any, M = unknown, C = any> = (this: C, message: FastEventMessage<T, P, M>) => any | Promise<any>;
|
|
50
50
|
type FastListenerNode = {
|
|
51
51
|
__listeners: (FastEventListener<any, any, any> | [FastEventListener<any, any>, number])[];
|
|
52
52
|
} & {
|
|
@@ -56,7 +56,7 @@ type FastEventSubscriber = {
|
|
|
56
56
|
off: () => void;
|
|
57
57
|
};
|
|
58
58
|
type FastListeners = FastListenerNode;
|
|
59
|
-
type FastEventOptions<M =
|
|
59
|
+
type FastEventOptions<M = Record<string, any>> = {
|
|
60
60
|
id?: string;
|
|
61
61
|
debug?: boolean;
|
|
62
62
|
delimiter?: string;
|
|
@@ -78,19 +78,19 @@ type FastEventListenOptions = {
|
|
|
78
78
|
prepend?: boolean;
|
|
79
79
|
};
|
|
80
80
|
|
|
81
|
-
declare class FastEventScope<Events extends FastEvents = FastEvents, Meta extends Record<string, any> = Record<string, any>, Types extends keyof Events = keyof Events> {
|
|
81
|
+
declare class FastEventScope<Events extends FastEvents = FastEvents, Meta extends Record<string, any> = Record<string, any>, Context = any, Types extends keyof Events = keyof Events> {
|
|
82
82
|
emitter: FastEvent<Events, Meta, Types>;
|
|
83
83
|
prefix: string;
|
|
84
84
|
constructor(emitter: FastEvent<Events, Meta, Types>, prefix: string);
|
|
85
85
|
private _getScopeListener;
|
|
86
86
|
private _getScopeType;
|
|
87
87
|
private _fixScopeType;
|
|
88
|
-
on<T extends Types = Types>(type: T, listener: FastEventListener<T, Events[T], Meta>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
89
|
-
on<T extends string>(type: T, listener: FastEventListener<T, Events[T], Meta>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
90
|
-
on(type: '**', listener: FastEventListener<any, any, Meta>): FastEventSubscriber;
|
|
91
|
-
once<T extends string>(type: T, listener: FastEventListener<T, Events[T], Meta>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
92
|
-
once<T extends Types = Types>(type: T, listener: FastEventListener<Types, Events[T], Meta>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
93
|
-
onAny<P = any>(listener: FastEventListener<Types, P, Meta>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
88
|
+
on<T extends Types = Types>(type: T, listener: FastEventListener<T, Events[T], Meta, Context>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
89
|
+
on<T extends string>(type: T, listener: FastEventListener<T, Events[T], Meta, Context>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
90
|
+
on(type: '**', listener: FastEventListener<any, any, Meta, Context>): FastEventSubscriber;
|
|
91
|
+
once<T extends string>(type: T, listener: FastEventListener<T, Events[T], Meta, Context>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
92
|
+
once<T extends Types = Types>(type: T, listener: FastEventListener<Types, Events[T], Meta, Context>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
93
|
+
onAny<P = any>(listener: FastEventListener<Types, P, Meta, Context>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
94
94
|
offAll(): void;
|
|
95
95
|
off(listener: FastEventListener<any, any, any>): void;
|
|
96
96
|
off(type: string, listener: FastEventListener<any, any, any>): void;
|
|
@@ -102,18 +102,76 @@ declare class FastEventScope<Events extends FastEvents = FastEvents, Meta extend
|
|
|
102
102
|
emit<R = any>(type: string, payload?: any, retain?: boolean): R[];
|
|
103
103
|
waitFor<T extends Types, P = Events[T], M = Meta>(type: T, timeout?: number): Promise<FastEventMessage<T, P, M>>;
|
|
104
104
|
waitFor<T extends string, P = Events[T], M = Meta>(type: string, timeout?: number): Promise<FastEventMessage<T, P, M>>;
|
|
105
|
-
|
|
105
|
+
/**
|
|
106
|
+
* 创建一个新的作用域实例
|
|
107
|
+
* @param prefix - 作用域前缀
|
|
108
|
+
* @returns 新的FastEventScope实例
|
|
109
|
+
*
|
|
110
|
+
* @description
|
|
111
|
+
* 基于当前作用域创建一个新的子作用域。新作用域会继承当前作用域的所有特性,
|
|
112
|
+
* 并在事件类型前添加额外的前缀。这允许创建层级化的事件命名空间。
|
|
113
|
+
*
|
|
114
|
+
* 作用域的特性:
|
|
115
|
+
* - 自动为所有事件类型添加前缀
|
|
116
|
+
* - 在触发事件时自动添加前缀
|
|
117
|
+
* - 在接收事件时自动移除前缀
|
|
118
|
+
* - 支持多层级的作用域嵌套
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```ts
|
|
122
|
+
* const emitter = new FastEvent();
|
|
123
|
+
* const userScope = emitter.scope('user');
|
|
124
|
+
* const profileScope = userScope.scope('profile');
|
|
125
|
+
*
|
|
126
|
+
* // 在profileScope中监听'update'事件
|
|
127
|
+
* // 实际监听的是'user/profile/update'
|
|
128
|
+
* profileScope.on('update', (data) => {
|
|
129
|
+
* console.log('Profile updated:', data);
|
|
130
|
+
* });
|
|
131
|
+
*
|
|
132
|
+
* // 在profileScope中触发'update'事件
|
|
133
|
+
* // 实际触发的是'user/profile/update'
|
|
134
|
+
* profileScope.emit('update', { name: 'John' });
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
scope(prefix: string): FastEventScope<ScopeEvents<Events, string>, Record<string, any>, any, keyof ScopeEvents<Events, string>>;
|
|
106
138
|
}
|
|
107
139
|
|
|
108
|
-
|
|
140
|
+
/**
|
|
141
|
+
* FastEvent 事件发射器类
|
|
142
|
+
*
|
|
143
|
+
* @template Events - 事件类型定义,继承自FastEvents接口
|
|
144
|
+
* @template Meta - 事件元数据类型,默认为任意键值对对象
|
|
145
|
+
* @template Types - 事件类型的键名类型,默认为Events的键名类型
|
|
146
|
+
*/
|
|
147
|
+
declare class FastEvent<Events extends FastEvents = FastEvents, Meta extends Record<string, any> = Record<string, any>, Context = any, Types extends keyof Events = keyof Events> {
|
|
148
|
+
/** 事件监听器树结构,存储所有注册的事件监听器 */
|
|
109
149
|
listeners: FastListeners;
|
|
150
|
+
/** 事件发射器的配置选项 */
|
|
110
151
|
private _options;
|
|
152
|
+
/** 事件名称的分隔符,默认为'/' */
|
|
111
153
|
private _delimiter;
|
|
154
|
+
/** 事件监听器执行时的上下文对象 */
|
|
112
155
|
private _context;
|
|
156
|
+
/** 保留的事件消息映射,用于新订阅者 */
|
|
113
157
|
retainedMessages: Map<string, any>;
|
|
158
|
+
/** 当前注册的监听器总数 */
|
|
114
159
|
listenerCount: number;
|
|
160
|
+
/**
|
|
161
|
+
* 创建FastEvent实例
|
|
162
|
+
* @param options - 事件发射器的配置选项
|
|
163
|
+
*
|
|
164
|
+
* 默认配置:
|
|
165
|
+
* - debug: false - 是否启用调试模式
|
|
166
|
+
* - id: 随机字符串 - 实例唯一标识符
|
|
167
|
+
* - delimiter: '/' - 事件名称分隔符
|
|
168
|
+
* - context: null - 监听器执行上下文
|
|
169
|
+
* - ignoreErrors: true - 是否忽略监听器执行错误
|
|
170
|
+
*/
|
|
115
171
|
constructor(options?: FastEventOptions<Meta>);
|
|
172
|
+
/** 获取事件发射器的配置选项 */
|
|
116
173
|
get options(): FastEventOptions;
|
|
174
|
+
/** 获取事件发射器的唯一标识符 */
|
|
117
175
|
get id(): string;
|
|
118
176
|
private _addListener;
|
|
119
177
|
private _enableDevTools;
|
|
@@ -134,11 +192,56 @@ declare class FastEvent<Events extends FastEvents = FastEvents, Meta extends Rec
|
|
|
134
192
|
* @description 遍历节点的监听器列表,移除所有匹配的监听器。支持移除普通函数和数组形式的监听器
|
|
135
193
|
*/
|
|
136
194
|
private _removeListener;
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
195
|
+
/**
|
|
196
|
+
* 注册事件监听器
|
|
197
|
+
* @param type - 事件类型,支持以下格式:
|
|
198
|
+
* - 普通字符串:'user/login'
|
|
199
|
+
* - 通配符:'user/*'(匹配单层)或'user/**'(匹配多层)
|
|
200
|
+
* - 全局监听:'**'(监听所有事件)
|
|
201
|
+
* @param listener - 事件监听器函数
|
|
202
|
+
* @param options - 监听器配置选项:
|
|
203
|
+
* - count: 触发次数限制,0表示无限制
|
|
204
|
+
* - prepend: 是否将监听器添加到监听器队列开头
|
|
205
|
+
* @returns 返回订阅者对象,包含off方法用于取消监听
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* ```ts
|
|
209
|
+
* // 监听特定事件
|
|
210
|
+
* emitter.on('user/login', (data) => console.log(data));
|
|
211
|
+
*
|
|
212
|
+
* // 使用通配符
|
|
213
|
+
* emitter.on('user/*', (data) => console.log(data));
|
|
214
|
+
*
|
|
215
|
+
* // 限制触发次数
|
|
216
|
+
* emitter.on('event', handler, { count: 3 });
|
|
217
|
+
* ```
|
|
218
|
+
*/
|
|
219
|
+
on<T extends string>(type: T, listener: FastEventListener<T, Events[T], Meta, Context>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
220
|
+
on<T extends Types = Types>(type: T, listener: FastEventListener<Types, Events[T], Meta, Context>, options?: FastEventListenOptions): FastEventSubscriber;
|
|
221
|
+
on<P = any>(type: '**', listener: FastEventListener<Types, P, Meta, Context>): FastEventSubscriber;
|
|
222
|
+
/**
|
|
223
|
+
* 注册一次性事件监听器
|
|
224
|
+
* @param type - 事件类型,支持与on方法相同的格式:
|
|
225
|
+
* - 普通字符串:'user/login'
|
|
226
|
+
* - 通配符:'user/*'(匹配单层)或'user/**'(匹配多层)
|
|
227
|
+
* @param listener - 事件监听器函数
|
|
228
|
+
* @returns 返回订阅者对象,包含off方法用于取消监听
|
|
229
|
+
*
|
|
230
|
+
* @description
|
|
231
|
+
* 监听器只会在事件首次触发时被调用一次,之后会自动解除注册。
|
|
232
|
+
* 这是on方法的特例,相当于设置options.count = 1。
|
|
233
|
+
* 如果事件有保留消息,新注册的监听器会立即收到最近一次的保留消息并解除注册。
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* ```ts
|
|
237
|
+
* // 只监听一次登录事件
|
|
238
|
+
* emitter.once('user/login', (data) => {
|
|
239
|
+
* console.log('用户登录:', data);
|
|
240
|
+
* });
|
|
241
|
+
* ```
|
|
242
|
+
*/
|
|
243
|
+
once<T extends Types = Types>(type: T, listener: FastEventListener<T, Events[T], Meta, Context>): FastEventSubscriber;
|
|
244
|
+
once<T extends string>(type: T, listener: FastEventListener<T, Events[T], Meta, Context>): FastEventSubscriber;
|
|
142
245
|
/**
|
|
143
246
|
* 注册一个监听器,用于监听所有事件
|
|
144
247
|
* @param listener 事件监听器函数,可以接收任意类型的事件数据
|
|
@@ -153,7 +256,7 @@ declare class FastEvent<Events extends FastEvents = FastEvents, Meta extends Rec
|
|
|
153
256
|
* subscriber.off();
|
|
154
257
|
* ```
|
|
155
258
|
*/
|
|
156
|
-
onAny<P = any>(listener: FastEventListener<string, P, Meta>, options?: Pick<FastEventListenOptions, 'prepend'>): FastEventSubscriber;
|
|
259
|
+
onAny<P = any>(listener: FastEventListener<string, P, Meta, Context>, options?: Pick<FastEventListenOptions, 'prepend'>): FastEventSubscriber;
|
|
157
260
|
off(listener: FastEventListener<any, any, any>): void;
|
|
158
261
|
off(type: string, listener: FastEventListener<any, any, any>): void;
|
|
159
262
|
off(type: Types, listener: FastEventListener<any, any, any>): void;
|
|
@@ -204,6 +307,23 @@ declare class FastEvent<Events extends FastEvents = FastEvents, Meta extends Rec
|
|
|
204
307
|
*/
|
|
205
308
|
private _traverseToPath;
|
|
206
309
|
private _traverseListeners;
|
|
310
|
+
/**
|
|
311
|
+
* 执行单个监听器函数
|
|
312
|
+
* @param listener - 要执行的监听器函数或包装过的监听器对象
|
|
313
|
+
* @param message - 事件消息对象,包含type、payload和meta
|
|
314
|
+
* @returns 监听器的执行结果或错误对象(如果配置了ignoreErrors)
|
|
315
|
+
* @private
|
|
316
|
+
*
|
|
317
|
+
* @description
|
|
318
|
+
* 执行单个监听器函数,处理以下情况:
|
|
319
|
+
* - 如果监听器是包装过的(有__wrappedListener属性),调用包装的函数
|
|
320
|
+
* - 否则直接调用监听器函数
|
|
321
|
+
* - 使用配置的上下文(_context)作为this
|
|
322
|
+
* - 捕获并处理执行过程中的错误:
|
|
323
|
+
* - 如果有onListenerError回调,调用它
|
|
324
|
+
* - 如果配置了ignoreErrors,返回错误对象
|
|
325
|
+
* - 否则抛出错误
|
|
326
|
+
*/
|
|
207
327
|
private _executeListener;
|
|
208
328
|
/**
|
|
209
329
|
* 执行监听器节点中的所有监听函数
|
|
@@ -235,41 +355,156 @@ declare class FastEvent<Events extends FastEvents = FastEvents, Meta extends Rec
|
|
|
235
355
|
* // 方式2: 对象形式
|
|
236
356
|
* emit({ type: 'user.login', payload: { id: 1 } ,meta:{...}}}, true)
|
|
237
357
|
*/
|
|
358
|
+
/**
|
|
359
|
+
* 同步触发事件
|
|
360
|
+
* @param type - 事件类型,可以是字符串或预定义的事件类型
|
|
361
|
+
* @param payload - 事件数据负载
|
|
362
|
+
* @param retain - 是否保留该事件,用于后续新的订阅者
|
|
363
|
+
* @param meta - 事件元数据
|
|
364
|
+
* @returns 所有监听器的执行结果数组
|
|
365
|
+
*
|
|
366
|
+
* @description
|
|
367
|
+
* 同步触发指定类型的事件,支持两种调用方式:
|
|
368
|
+
* 1. 参数形式:emit(type, payload, retain, meta)
|
|
369
|
+
* 2. 对象形式:emit({ type, payload, meta }, retain)
|
|
370
|
+
*
|
|
371
|
+
* 特性:
|
|
372
|
+
* - 支持通配符匹配,一个事件可能触发多个监听器
|
|
373
|
+
* - 如果设置了retain为true,会保存最后一次的事件数据
|
|
374
|
+
* - 按照注册顺序同步调用所有匹配的监听器
|
|
375
|
+
* - 如果配置了ignoreErrors,监听器抛出的错误会被捕获并返回
|
|
376
|
+
*
|
|
377
|
+
* @example
|
|
378
|
+
* ```ts
|
|
379
|
+
* // 简单事件触发
|
|
380
|
+
* emitter.emit('user/login', { userId: 123 });
|
|
381
|
+
*
|
|
382
|
+
* // 带保留的事件触发
|
|
383
|
+
* emitter.emit('status/change', { online: true }, true);
|
|
384
|
+
*
|
|
385
|
+
* // 带元数据的事件触发
|
|
386
|
+
* emitter.emit('data/update', newData, false, { timestamp: Date.now() });
|
|
387
|
+
*
|
|
388
|
+
* // 使用对象形式触发
|
|
389
|
+
* emitter.emit({
|
|
390
|
+
* type: 'user/login',
|
|
391
|
+
* payload: { userId: 123 },
|
|
392
|
+
* meta: { time: Date.now() }
|
|
393
|
+
* }, true);
|
|
394
|
+
* ```
|
|
395
|
+
*/
|
|
238
396
|
emit<R = any>(type: string, payload?: any, retain?: boolean, meta?: Meta): R[];
|
|
239
397
|
emit<R = any>(type: Types, payload?: Events[Types], retain?: boolean, meta?: Meta): R[];
|
|
240
398
|
emit<R = any>(message: FastEventMessage<Types, Events[Types], Meta>, retain?: boolean): R[];
|
|
241
399
|
emit<R = any>(message: FastEventMessage<string, any, Meta>, retain?: boolean): R[];
|
|
400
|
+
/**
|
|
401
|
+
* 异步触发事件
|
|
402
|
+
* @param type - 事件类型,可以是字符串或预定义的事件类型
|
|
403
|
+
* @param payload - 事件数据负载
|
|
404
|
+
* @param retain - 是否保留该事件,用于后续新的订阅者
|
|
405
|
+
* @param meta - 事件元数据
|
|
406
|
+
* @returns Promise,解析为所有监听器的执行结果数组
|
|
407
|
+
*
|
|
408
|
+
* @description
|
|
409
|
+
* 异步触发指定类型的事件,与emit方法类似,但有以下区别:
|
|
410
|
+
* - 返回Promise,等待所有异步监听器执行完成
|
|
411
|
+
* - 使用Promise.allSettled处理监听器的执行结果
|
|
412
|
+
* - 即使某些监听器失败,也会等待所有监听器执行完成
|
|
413
|
+
* - 返回结果包含成功值或错误信息
|
|
414
|
+
*
|
|
415
|
+
* @example
|
|
416
|
+
* ```ts
|
|
417
|
+
* // 异步事件处理
|
|
418
|
+
* const results = await emitter.emitAsync('data/process', rawData);
|
|
419
|
+
*
|
|
420
|
+
* // 处理结果包含成功和失败的情况
|
|
421
|
+
* results.forEach(result => {
|
|
422
|
+
* if (result instanceof Error) {
|
|
423
|
+
* console.error('处理失败:', result);
|
|
424
|
+
* } else {
|
|
425
|
+
* console.log('处理成功:', result);
|
|
426
|
+
* }
|
|
427
|
+
* });
|
|
428
|
+
*
|
|
429
|
+
* // 带元数据的异步事件
|
|
430
|
+
* await emitter.emitAsync('batch/process', items, false, {
|
|
431
|
+
* batchId: 'batch-001',
|
|
432
|
+
* timestamp: Date.now()
|
|
433
|
+
* });
|
|
434
|
+
* ```
|
|
435
|
+
*/
|
|
242
436
|
emitAsync<R = any>(type: string, payload?: any, retain?: boolean, meta?: Meta): Promise<[R | Error][]>;
|
|
243
437
|
emitAsync<R = any>(type: Types, payload?: Events[Types], retain?: boolean, meta?: Meta): Promise<[R | Error][]>;
|
|
244
438
|
/**
|
|
245
|
-
*
|
|
439
|
+
* 等待指定事件发生,返回一个Promise
|
|
440
|
+
* @param type - 要等待的事件类型
|
|
441
|
+
* @param timeout - 超时时间(毫秒),默认为0表示永不超时
|
|
442
|
+
* @returns Promise,解析为事件消息对象,包含type、payload和meta
|
|
443
|
+
*
|
|
444
|
+
* @description
|
|
445
|
+
* 创建一个Promise,在指定事件发生时解析。
|
|
446
|
+
* - 当事件触发时,Promise会解析为事件消息对象
|
|
447
|
+
* - 如果设置了timeout且超时,Promise会被拒绝
|
|
448
|
+
* - 一旦事件发生或超时,会自动取消事件监听
|
|
246
449
|
*
|
|
247
|
-
*
|
|
450
|
+
* @example
|
|
451
|
+
* ```ts
|
|
452
|
+
* try {
|
|
453
|
+
* // 等待登录事件,最多等待5秒
|
|
454
|
+
* const event = await emitter.waitFor('user/login', 5000);
|
|
455
|
+
* console.log('用户登录成功:', event.payload);
|
|
456
|
+
* } catch (error) {
|
|
457
|
+
* console.error('等待登录超时');
|
|
458
|
+
* }
|
|
248
459
|
*
|
|
249
|
-
*
|
|
250
|
-
*
|
|
460
|
+
* // 无限等待事件
|
|
461
|
+
* const event = await emitter.waitFor('server/ready');
|
|
462
|
+
* console.log('服务器就绪');
|
|
463
|
+
* ```
|
|
251
464
|
*/
|
|
252
465
|
waitFor<T extends Types, P = Events[T], M = Meta>(type: T, timeout?: number): Promise<FastEventMessage<T, P, M>>;
|
|
253
466
|
waitFor<T extends string, P = Events[T], M = Meta>(type: string, timeout?: number): Promise<FastEventMessage<T, P, M>>;
|
|
254
467
|
/**
|
|
255
|
-
*
|
|
468
|
+
* 创建一个新的事件作用域
|
|
469
|
+
* @param prefix - 作用域前缀,将自动添加到该作用域下所有事件名称前
|
|
470
|
+
* @returns 新的FastEventScope实例
|
|
471
|
+
*
|
|
472
|
+
* @description
|
|
473
|
+
* 创建一个新的事件作用域,用于在特定命名空间下管理事件。
|
|
256
474
|
*
|
|
257
|
-
*
|
|
475
|
+
* 重要特性:
|
|
476
|
+
* - 作用域与父事件发射器共享同一个监听器表
|
|
477
|
+
* - 作用域中的事件会自动添加前缀
|
|
478
|
+
* - 作用域的所有操作都会映射到父事件发射器上
|
|
479
|
+
* - 作用域不是完全隔离的,只是提供了事件名称的命名空间
|
|
258
480
|
*
|
|
259
|
-
*
|
|
260
|
-
*
|
|
261
|
-
*
|
|
481
|
+
* @example
|
|
482
|
+
* ```ts
|
|
483
|
+
* const emitter = new FastEvent();
|
|
262
484
|
*
|
|
263
|
-
*
|
|
485
|
+
* // 创建用户相关事件的作用域
|
|
486
|
+
* const userEvents = emitter.scope('user');
|
|
264
487
|
*
|
|
265
|
-
*
|
|
488
|
+
* // 在作用域中监听事件
|
|
489
|
+
* userEvents.on('login', (data) => {
|
|
490
|
+
* // 实际监听的是 'user/login'
|
|
491
|
+
* console.log('用户登录:', data);
|
|
492
|
+
* });
|
|
266
493
|
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
494
|
+
* // 在作用域中触发事件
|
|
495
|
+
* userEvents.emit('login', { userId: 123 });
|
|
496
|
+
* // 等同于 emitter.emit('user/login', { userId: 123 })
|
|
270
497
|
*
|
|
498
|
+
* // 创建嵌套作用域
|
|
499
|
+
* const profileEvents = userEvents.scope('profile');
|
|
500
|
+
* profileEvents.emit('update', { name: 'John' });
|
|
501
|
+
* // 等同于 emitter.emit('user/profile/update', { name: 'John' })
|
|
502
|
+
*
|
|
503
|
+
* // 清理作用域
|
|
504
|
+
* userEvents.offAll(); // 清理 'user' 前缀下的所有事件
|
|
505
|
+
* ```
|
|
271
506
|
*/
|
|
272
|
-
scope<T extends string>(prefix: T): FastEventScope<ScopeEvents<Events, T>, Record<string, any>, keyof ScopeEvents<Events, T>>;
|
|
507
|
+
scope<T extends string>(prefix: T): FastEventScope<ScopeEvents<Events, T>, Record<string, any>, any, keyof ScopeEvents<Events, T>>;
|
|
273
508
|
}
|
|
274
509
|
|
|
275
510
|
/**
|