fastevent 1.1.1 → 1.1.2
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 +6 -0
- package/dist/devTools.d.mts +267 -32
- package/dist/devTools.d.ts +267 -32
- package/dist/index.d.mts +267 -32
- package/dist/index.d.ts +267 -32
- 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 +4 -3
package/src/event.ts
CHANGED
|
@@ -13,17 +13,47 @@ import {
|
|
|
13
13
|
import { isPathMatched } from './utils/isPathMatched';
|
|
14
14
|
import { removeItem } from './utils/removeItem';
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* FastEvent 事件发射器类
|
|
18
|
+
*
|
|
19
|
+
* @template Events - 事件类型定义,继承自FastEvents接口
|
|
20
|
+
* @template Meta - 事件元数据类型,默认为任意键值对对象
|
|
21
|
+
* @template Types - 事件类型的键名类型,默认为Events的键名类型
|
|
22
|
+
*/
|
|
16
23
|
export class FastEvent<
|
|
17
24
|
Events extends FastEvents = FastEvents,
|
|
18
25
|
Meta extends Record<string, any> = Record<string, any>,
|
|
26
|
+
Context = any,
|
|
19
27
|
Types extends keyof Events = keyof Events
|
|
20
28
|
> {
|
|
29
|
+
/** 事件监听器树结构,存储所有注册的事件监听器 */
|
|
21
30
|
public listeners: FastListeners = { __listeners: [] } as unknown as FastListeners
|
|
31
|
+
|
|
32
|
+
/** 事件发射器的配置选项 */
|
|
22
33
|
private _options: FastEventOptions
|
|
34
|
+
|
|
35
|
+
/** 事件名称的分隔符,默认为'/' */
|
|
23
36
|
private _delimiter: string = '/'
|
|
24
|
-
|
|
37
|
+
|
|
38
|
+
/** 事件监听器执行时的上下文对象 */
|
|
39
|
+
private _context: Context
|
|
40
|
+
|
|
41
|
+
/** 保留的事件消息映射,用于新订阅者 */
|
|
25
42
|
retainedMessages: Map<string, any> = new Map<string, any>()
|
|
43
|
+
|
|
44
|
+
/** 当前注册的监听器总数 */
|
|
26
45
|
listenerCount: number = 0
|
|
46
|
+
/**
|
|
47
|
+
* 创建FastEvent实例
|
|
48
|
+
* @param options - 事件发射器的配置选项
|
|
49
|
+
*
|
|
50
|
+
* 默认配置:
|
|
51
|
+
* - debug: false - 是否启用调试模式
|
|
52
|
+
* - id: 随机字符串 - 实例唯一标识符
|
|
53
|
+
* - delimiter: '/' - 事件名称分隔符
|
|
54
|
+
* - context: null - 监听器执行上下文
|
|
55
|
+
* - ignoreErrors: true - 是否忽略监听器执行错误
|
|
56
|
+
*/
|
|
27
57
|
constructor(options?: FastEventOptions<Meta>) {
|
|
28
58
|
this._options = Object.assign({
|
|
29
59
|
debug: false,
|
|
@@ -36,7 +66,11 @@ export class FastEvent<
|
|
|
36
66
|
this._context = this._options.context || this
|
|
37
67
|
this._enableDevTools()
|
|
38
68
|
}
|
|
69
|
+
|
|
70
|
+
/** 获取事件发射器的配置选项 */
|
|
39
71
|
get options() { return this._options }
|
|
72
|
+
|
|
73
|
+
/** 获取事件发射器的唯一标识符 */
|
|
40
74
|
get id() { return this._options.id! }
|
|
41
75
|
private _addListener(parts: string[], listener: FastEventListener<any, any>, options: Required<FastEventListenOptions>): FastListenerNode | undefined {
|
|
42
76
|
const { count, prepend } = options
|
|
@@ -108,9 +142,33 @@ export class FastEvent<
|
|
|
108
142
|
return isRemove
|
|
109
143
|
})
|
|
110
144
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
145
|
+
/**
|
|
146
|
+
* 注册事件监听器
|
|
147
|
+
* @param type - 事件类型,支持以下格式:
|
|
148
|
+
* - 普通字符串:'user/login'
|
|
149
|
+
* - 通配符:'user/*'(匹配单层)或'user/**'(匹配多层)
|
|
150
|
+
* - 全局监听:'**'(监听所有事件)
|
|
151
|
+
* @param listener - 事件监听器函数
|
|
152
|
+
* @param options - 监听器配置选项:
|
|
153
|
+
* - count: 触发次数限制,0表示无限制
|
|
154
|
+
* - prepend: 是否将监听器添加到监听器队列开头
|
|
155
|
+
* @returns 返回订阅者对象,包含off方法用于取消监听
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```ts
|
|
159
|
+
* // 监听特定事件
|
|
160
|
+
* emitter.on('user/login', (data) => console.log(data));
|
|
161
|
+
*
|
|
162
|
+
* // 使用通配符
|
|
163
|
+
* emitter.on('user/*', (data) => console.log(data));
|
|
164
|
+
*
|
|
165
|
+
* // 限制触发次数
|
|
166
|
+
* emitter.on('event', handler, { count: 3 });
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
public on<T extends string>(type: T, listener: FastEventListener<T, Events[T], Meta, Context>, options?: FastEventListenOptions): FastEventSubscriber
|
|
170
|
+
public on<T extends Types = Types>(type: T, listener: FastEventListener<Types, Events[T], Meta, Context>, options?: FastEventListenOptions): FastEventSubscriber
|
|
171
|
+
public on<P = any>(type: '**', listener: FastEventListener<Types, P, Meta, Context>): FastEventSubscriber
|
|
114
172
|
public on(): FastEventSubscriber {
|
|
115
173
|
const type = arguments[0] as string
|
|
116
174
|
const listener = arguments[1] as FastEventListener
|
|
@@ -137,8 +195,29 @@ export class FastEvent<
|
|
|
137
195
|
}
|
|
138
196
|
|
|
139
197
|
|
|
140
|
-
|
|
141
|
-
|
|
198
|
+
/**
|
|
199
|
+
* 注册一次性事件监听器
|
|
200
|
+
* @param type - 事件类型,支持与on方法相同的格式:
|
|
201
|
+
* - 普通字符串:'user/login'
|
|
202
|
+
* - 通配符:'user/*'(匹配单层)或'user/**'(匹配多层)
|
|
203
|
+
* @param listener - 事件监听器函数
|
|
204
|
+
* @returns 返回订阅者对象,包含off方法用于取消监听
|
|
205
|
+
*
|
|
206
|
+
* @description
|
|
207
|
+
* 监听器只会在事件首次触发时被调用一次,之后会自动解除注册。
|
|
208
|
+
* 这是on方法的特例,相当于设置options.count = 1。
|
|
209
|
+
* 如果事件有保留消息,新注册的监听器会立即收到最近一次的保留消息并解除注册。
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```ts
|
|
213
|
+
* // 只监听一次登录事件
|
|
214
|
+
* emitter.once('user/login', (data) => {
|
|
215
|
+
* console.log('用户登录:', data);
|
|
216
|
+
* });
|
|
217
|
+
* ```
|
|
218
|
+
*/
|
|
219
|
+
public once<T extends Types = Types>(type: T, listener: FastEventListener<T, Events[T], Meta, Context>): FastEventSubscriber
|
|
220
|
+
public once<T extends string>(type: T, listener: FastEventListener<T, Events[T], Meta, Context>): FastEventSubscriber
|
|
142
221
|
public once(): FastEventSubscriber {
|
|
143
222
|
return this.on(arguments[0], arguments[1], { count: 1 })
|
|
144
223
|
}
|
|
@@ -157,7 +236,7 @@ export class FastEvent<
|
|
|
157
236
|
* subscriber.off();
|
|
158
237
|
* ```
|
|
159
238
|
*/
|
|
160
|
-
onAny<P = any>(listener: FastEventListener<string, P, Meta>, options?: Pick<FastEventListenOptions, 'prepend'>): FastEventSubscriber {
|
|
239
|
+
onAny<P = any>(listener: FastEventListener<string, P, Meta, Context>, options?: Pick<FastEventListenOptions, 'prepend'>): FastEventSubscriber {
|
|
161
240
|
const listeners = this.listeners.__listeners
|
|
162
241
|
if (options && options.prepend) {
|
|
163
242
|
listeners.splice(0, 0, listener)
|
|
@@ -339,6 +418,23 @@ export class FastEvent<
|
|
|
339
418
|
traverseNodes(entryNode, callback, []);
|
|
340
419
|
}
|
|
341
420
|
|
|
421
|
+
/**
|
|
422
|
+
* 执行单个监听器函数
|
|
423
|
+
* @param listener - 要执行的监听器函数或包装过的监听器对象
|
|
424
|
+
* @param message - 事件消息对象,包含type、payload和meta
|
|
425
|
+
* @returns 监听器的执行结果或错误对象(如果配置了ignoreErrors)
|
|
426
|
+
* @private
|
|
427
|
+
*
|
|
428
|
+
* @description
|
|
429
|
+
* 执行单个监听器函数,处理以下情况:
|
|
430
|
+
* - 如果监听器是包装过的(有__wrappedListener属性),调用包装的函数
|
|
431
|
+
* - 否则直接调用监听器函数
|
|
432
|
+
* - 使用配置的上下文(_context)作为this
|
|
433
|
+
* - 捕获并处理执行过程中的错误:
|
|
434
|
+
* - 如果有onListenerError回调,调用它
|
|
435
|
+
* - 如果配置了ignoreErrors,返回错误对象
|
|
436
|
+
* - 否则抛出错误
|
|
437
|
+
*/
|
|
342
438
|
private _executeListener(listener: any, message: FastEventMessage): any {
|
|
343
439
|
try {
|
|
344
440
|
if (typeof (listener.__wrappedListener) === 'function') {
|
|
@@ -410,6 +506,44 @@ export class FastEvent<
|
|
|
410
506
|
* // 方式2: 对象形式
|
|
411
507
|
* emit({ type: 'user.login', payload: { id: 1 } ,meta:{...}}}, true)
|
|
412
508
|
*/
|
|
509
|
+
/**
|
|
510
|
+
* 同步触发事件
|
|
511
|
+
* @param type - 事件类型,可以是字符串或预定义的事件类型
|
|
512
|
+
* @param payload - 事件数据负载
|
|
513
|
+
* @param retain - 是否保留该事件,用于后续新的订阅者
|
|
514
|
+
* @param meta - 事件元数据
|
|
515
|
+
* @returns 所有监听器的执行结果数组
|
|
516
|
+
*
|
|
517
|
+
* @description
|
|
518
|
+
* 同步触发指定类型的事件,支持两种调用方式:
|
|
519
|
+
* 1. 参数形式:emit(type, payload, retain, meta)
|
|
520
|
+
* 2. 对象形式:emit({ type, payload, meta }, retain)
|
|
521
|
+
*
|
|
522
|
+
* 特性:
|
|
523
|
+
* - 支持通配符匹配,一个事件可能触发多个监听器
|
|
524
|
+
* - 如果设置了retain为true,会保存最后一次的事件数据
|
|
525
|
+
* - 按照注册顺序同步调用所有匹配的监听器
|
|
526
|
+
* - 如果配置了ignoreErrors,监听器抛出的错误会被捕获并返回
|
|
527
|
+
*
|
|
528
|
+
* @example
|
|
529
|
+
* ```ts
|
|
530
|
+
* // 简单事件触发
|
|
531
|
+
* emitter.emit('user/login', { userId: 123 });
|
|
532
|
+
*
|
|
533
|
+
* // 带保留的事件触发
|
|
534
|
+
* emitter.emit('status/change', { online: true }, true);
|
|
535
|
+
*
|
|
536
|
+
* // 带元数据的事件触发
|
|
537
|
+
* emitter.emit('data/update', newData, false, { timestamp: Date.now() });
|
|
538
|
+
*
|
|
539
|
+
* // 使用对象形式触发
|
|
540
|
+
* emitter.emit({
|
|
541
|
+
* type: 'user/login',
|
|
542
|
+
* payload: { userId: 123 },
|
|
543
|
+
* meta: { time: Date.now() }
|
|
544
|
+
* }, true);
|
|
545
|
+
* ```
|
|
546
|
+
*/
|
|
413
547
|
public emit<R = any>(type: string, payload?: any, retain?: boolean, meta?: Meta): R[]
|
|
414
548
|
public emit<R = any>(type: Types, payload?: Events[Types], retain?: boolean, meta?: Meta): R[]
|
|
415
549
|
public emit<R = any>(message: FastEventMessage<Types, Events[Types], Meta>, retain?: boolean): R[]
|
|
@@ -453,6 +587,42 @@ export class FastEvent<
|
|
|
453
587
|
return results
|
|
454
588
|
}
|
|
455
589
|
|
|
590
|
+
/**
|
|
591
|
+
* 异步触发事件
|
|
592
|
+
* @param type - 事件类型,可以是字符串或预定义的事件类型
|
|
593
|
+
* @param payload - 事件数据负载
|
|
594
|
+
* @param retain - 是否保留该事件,用于后续新的订阅者
|
|
595
|
+
* @param meta - 事件元数据
|
|
596
|
+
* @returns Promise,解析为所有监听器的执行结果数组
|
|
597
|
+
*
|
|
598
|
+
* @description
|
|
599
|
+
* 异步触发指定类型的事件,与emit方法类似,但有以下区别:
|
|
600
|
+
* - 返回Promise,等待所有异步监听器执行完成
|
|
601
|
+
* - 使用Promise.allSettled处理监听器的执行结果
|
|
602
|
+
* - 即使某些监听器失败,也会等待所有监听器执行完成
|
|
603
|
+
* - 返回结果包含成功值或错误信息
|
|
604
|
+
*
|
|
605
|
+
* @example
|
|
606
|
+
* ```ts
|
|
607
|
+
* // 异步事件处理
|
|
608
|
+
* const results = await emitter.emitAsync('data/process', rawData);
|
|
609
|
+
*
|
|
610
|
+
* // 处理结果包含成功和失败的情况
|
|
611
|
+
* results.forEach(result => {
|
|
612
|
+
* if (result instanceof Error) {
|
|
613
|
+
* console.error('处理失败:', result);
|
|
614
|
+
* } else {
|
|
615
|
+
* console.log('处理成功:', result);
|
|
616
|
+
* }
|
|
617
|
+
* });
|
|
618
|
+
*
|
|
619
|
+
* // 带元数据的异步事件
|
|
620
|
+
* await emitter.emitAsync('batch/process', items, false, {
|
|
621
|
+
* batchId: 'batch-001',
|
|
622
|
+
* timestamp: Date.now()
|
|
623
|
+
* });
|
|
624
|
+
* ```
|
|
625
|
+
*/
|
|
456
626
|
public async emitAsync<R = any>(type: string, payload?: any, retain?: boolean, meta?: Meta): Promise<[R | Error][]>
|
|
457
627
|
public async emitAsync<R = any>(type: Types, payload?: Events[Types], retain?: boolean, meta?: Meta): Promise<[R | Error][]>
|
|
458
628
|
public async emitAsync<P = any>(): Promise<[P | Error][]> {
|
|
@@ -476,12 +646,31 @@ export class FastEvent<
|
|
|
476
646
|
}
|
|
477
647
|
|
|
478
648
|
/**
|
|
479
|
-
*
|
|
649
|
+
* 等待指定事件发生,返回一个Promise
|
|
650
|
+
* @param type - 要等待的事件类型
|
|
651
|
+
* @param timeout - 超时时间(毫秒),默认为0表示永不超时
|
|
652
|
+
* @returns Promise,解析为事件消息对象,包含type、payload和meta
|
|
480
653
|
*
|
|
481
|
-
*
|
|
654
|
+
* @description
|
|
655
|
+
* 创建一个Promise,在指定事件发生时解析。
|
|
656
|
+
* - 当事件触发时,Promise会解析为事件消息对象
|
|
657
|
+
* - 如果设置了timeout且超时,Promise会被拒绝
|
|
658
|
+
* - 一旦事件发生或超时,会自动取消事件监听
|
|
659
|
+
*
|
|
660
|
+
* @example
|
|
661
|
+
* ```ts
|
|
662
|
+
* try {
|
|
663
|
+
* // 等待登录事件,最多等待5秒
|
|
664
|
+
* const event = await emitter.waitFor('user/login', 5000);
|
|
665
|
+
* console.log('用户登录成功:', event.payload);
|
|
666
|
+
* } catch (error) {
|
|
667
|
+
* console.error('等待登录超时');
|
|
668
|
+
* }
|
|
482
669
|
*
|
|
483
|
-
*
|
|
484
|
-
*
|
|
670
|
+
* // 无限等待事件
|
|
671
|
+
* const event = await emitter.waitFor('server/ready');
|
|
672
|
+
* console.log('服务器就绪');
|
|
673
|
+
* ```
|
|
485
674
|
*/
|
|
486
675
|
public waitFor<T extends Types, P = Events[T], M = Meta>(type: T, timeout?: number): Promise<FastEventMessage<T, P, M>>
|
|
487
676
|
public waitFor<T extends string, P = Events[T], M = Meta>(type: string, timeout?: number): Promise<FastEventMessage<T, P, M>>
|
|
@@ -507,22 +696,44 @@ export class FastEvent<
|
|
|
507
696
|
}
|
|
508
697
|
|
|
509
698
|
/**
|
|
510
|
-
*
|
|
699
|
+
* 创建一个新的事件作用域
|
|
700
|
+
* @param prefix - 作用域前缀,将自动添加到该作用域下所有事件名称前
|
|
701
|
+
* @returns 新的FastEventScope实例
|
|
702
|
+
*
|
|
703
|
+
* @description
|
|
704
|
+
* 创建一个新的事件作用域,用于在特定命名空间下管理事件。
|
|
705
|
+
*
|
|
706
|
+
* 重要特性:
|
|
707
|
+
* - 作用域与父事件发射器共享同一个监听器表
|
|
708
|
+
* - 作用域中的事件会自动添加前缀
|
|
709
|
+
* - 作用域的所有操作都会映射到父事件发射器上
|
|
710
|
+
* - 作用域不是完全隔离的,只是提供了事件名称的命名空间
|
|
511
711
|
*
|
|
512
|
-
*
|
|
712
|
+
* @example
|
|
713
|
+
* ```ts
|
|
714
|
+
* const emitter = new FastEvent();
|
|
513
715
|
*
|
|
514
|
-
*
|
|
515
|
-
*
|
|
516
|
-
* 两者工不是完全隔离的,仅是事件侦听和触发时的事件类型不同而已
|
|
716
|
+
* // 创建用户相关事件的作用域
|
|
717
|
+
* const userEvents = emitter.scope('user');
|
|
517
718
|
*
|
|
518
|
-
*
|
|
719
|
+
* // 在作用域中监听事件
|
|
720
|
+
* userEvents.on('login', (data) => {
|
|
721
|
+
* // 实际监听的是 'user/login'
|
|
722
|
+
* console.log('用户登录:', data);
|
|
723
|
+
* });
|
|
519
724
|
*
|
|
520
|
-
*
|
|
725
|
+
* // 在作用域中触发事件
|
|
726
|
+
* userEvents.emit('login', { userId: 123 });
|
|
727
|
+
* // 等同于 emitter.emit('user/login', { userId: 123 })
|
|
521
728
|
*
|
|
522
|
-
*
|
|
523
|
-
*
|
|
524
|
-
*
|
|
729
|
+
* // 创建嵌套作用域
|
|
730
|
+
* const profileEvents = userEvents.scope('profile');
|
|
731
|
+
* profileEvents.emit('update', { name: 'John' });
|
|
732
|
+
* // 等同于 emitter.emit('user/profile/update', { name: 'John' })
|
|
525
733
|
*
|
|
734
|
+
* // 清理作用域
|
|
735
|
+
* userEvents.offAll(); // 清理 'user' 前缀下的所有事件
|
|
736
|
+
* ```
|
|
526
737
|
*/
|
|
527
738
|
scope<T extends string>(prefix: T) {
|
|
528
739
|
return new FastEventScope<ScopeEvents<Events, T>>(this as unknown as FastEvent<ScopeEvents<Events, T>>, prefix)
|
package/src/scope.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { FastEventListener, FastEventListenOptions, FastEventMessage, FastEvents
|
|
|
4
4
|
export class FastEventScope<
|
|
5
5
|
Events extends FastEvents = FastEvents,
|
|
6
6
|
Meta extends Record<string, any> = Record<string, any>,
|
|
7
|
+
Context = any,
|
|
7
8
|
Types extends keyof Events = keyof Events
|
|
8
9
|
> {
|
|
9
10
|
constructor(public emitter: FastEvent<Events, Meta, Types>, public prefix: string) {
|
|
@@ -33,9 +34,9 @@ export class FastEventScope<
|
|
|
33
34
|
return type.startsWith(this.prefix) ? type.substring(this.prefix.length) : type
|
|
34
35
|
}
|
|
35
36
|
|
|
36
|
-
public on<T extends Types = Types>(type: T, listener: FastEventListener<T, Events[T], Meta>, options?: FastEventListenOptions): FastEventSubscriber
|
|
37
|
-
public on<T extends string>(type: T, listener: FastEventListener<T, Events[T], Meta>, options?: FastEventListenOptions): FastEventSubscriber
|
|
38
|
-
public on(type: '**', listener: FastEventListener<any, any, Meta>): FastEventSubscriber
|
|
37
|
+
public on<T extends Types = Types>(type: T, listener: FastEventListener<T, Events[T], Meta, Context>, options?: FastEventListenOptions): FastEventSubscriber
|
|
38
|
+
public on<T extends string>(type: T, listener: FastEventListener<T, Events[T], Meta, Context>, options?: FastEventListenOptions): FastEventSubscriber
|
|
39
|
+
public on(type: '**', listener: FastEventListener<any, any, Meta, Context>): FastEventSubscriber
|
|
39
40
|
public on(): FastEventSubscriber {
|
|
40
41
|
const args = [...arguments] as [any, any, any]
|
|
41
42
|
args[0] = this._getScopeType(args[0])
|
|
@@ -43,13 +44,13 @@ export class FastEventScope<
|
|
|
43
44
|
return this.emitter.on(...args)
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
public once<T extends string>(type: T, listener: FastEventListener<T, Events[T], Meta>, options?: FastEventListenOptions): FastEventSubscriber
|
|
47
|
-
public once<T extends Types = Types>(type: T, listener: FastEventListener<Types, Events[T], Meta>, options?: FastEventListenOptions): FastEventSubscriber
|
|
47
|
+
public once<T extends string>(type: T, listener: FastEventListener<T, Events[T], Meta, Context>, options?: FastEventListenOptions): FastEventSubscriber
|
|
48
|
+
public once<T extends Types = Types>(type: T, listener: FastEventListener<Types, Events[T], Meta, Context>, options?: FastEventListenOptions): FastEventSubscriber
|
|
48
49
|
public once(): FastEventSubscriber {
|
|
49
50
|
return this.on(arguments[0], arguments[1], Object.assign({}, arguments[2], { count: 1 }))
|
|
50
51
|
}
|
|
51
52
|
|
|
52
|
-
onAny<P = any>(listener: FastEventListener<Types, P, Meta>, options?: FastEventListenOptions): FastEventSubscriber {
|
|
53
|
+
onAny<P = any>(listener: FastEventListener<Types, P, Meta, Context>, options?: FastEventListenOptions): FastEventSubscriber {
|
|
53
54
|
const type = this.prefix + '**'
|
|
54
55
|
return this.on(type as any, listener, options)
|
|
55
56
|
}
|
|
@@ -91,6 +92,38 @@ export class FastEventScope<
|
|
|
91
92
|
})
|
|
92
93
|
return scopeMessage as unknown as FastEventMessage<T, P, M>
|
|
93
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* 创建一个新的作用域实例
|
|
97
|
+
* @param prefix - 作用域前缀
|
|
98
|
+
* @returns 新的FastEventScope实例
|
|
99
|
+
*
|
|
100
|
+
* @description
|
|
101
|
+
* 基于当前作用域创建一个新的子作用域。新作用域会继承当前作用域的所有特性,
|
|
102
|
+
* 并在事件类型前添加额外的前缀。这允许创建层级化的事件命名空间。
|
|
103
|
+
*
|
|
104
|
+
* 作用域的特性:
|
|
105
|
+
* - 自动为所有事件类型添加前缀
|
|
106
|
+
* - 在触发事件时自动添加前缀
|
|
107
|
+
* - 在接收事件时自动移除前缀
|
|
108
|
+
* - 支持多层级的作用域嵌套
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```ts
|
|
112
|
+
* const emitter = new FastEvent();
|
|
113
|
+
* const userScope = emitter.scope('user');
|
|
114
|
+
* const profileScope = userScope.scope('profile');
|
|
115
|
+
*
|
|
116
|
+
* // 在profileScope中监听'update'事件
|
|
117
|
+
* // 实际监听的是'user/profile/update'
|
|
118
|
+
* profileScope.on('update', (data) => {
|
|
119
|
+
* console.log('Profile updated:', data);
|
|
120
|
+
* });
|
|
121
|
+
*
|
|
122
|
+
* // 在profileScope中触发'update'事件
|
|
123
|
+
* // 实际触发的是'user/profile/update'
|
|
124
|
+
* profileScope.emit('update', { name: 'John' });
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
94
127
|
public scope(prefix: string) {
|
|
95
128
|
return this.emitter.scope(this._getScopeType(prefix)!)
|
|
96
129
|
}
|
package/src/types.ts
CHANGED
|
@@ -11,8 +11,9 @@ export type FastEventMessage<T = string, P = any, M = unknown> = {
|
|
|
11
11
|
export type FastEventListener<
|
|
12
12
|
T = string,
|
|
13
13
|
P = any,
|
|
14
|
-
M = unknown
|
|
15
|
-
|
|
14
|
+
M = unknown,
|
|
15
|
+
C = any
|
|
16
|
+
> = (this: C, message: FastEventMessage<T, P, M>) => any | Promise<any>
|
|
16
17
|
|
|
17
18
|
export type FastListenerNode = {
|
|
18
19
|
__listeners: (FastEventListener<any, any, any> | [FastEventListener<any, any>, number])[];
|
|
@@ -30,7 +31,7 @@ export interface FastEventListenerMeta {
|
|
|
30
31
|
|
|
31
32
|
export type FastListeners = FastListenerNode
|
|
32
33
|
|
|
33
|
-
export type FastEventOptions<M =
|
|
34
|
+
export type FastEventOptions<M = Record<string, any>> = {
|
|
34
35
|
id?: string
|
|
35
36
|
debug?: boolean
|
|
36
37
|
// 事件分隔符
|