EventBusSingleton.ts
// EventBusSingleton.ts - 事件总线单例导出
import { EventBus, EventHandler } from './EventBus'
// 单例实例
const bus = new EventBus()
// ========== 类型导出 ==========
export type { EventHandler }
export type SharedDataSubscriber<T = any> = EventHandler<T>
// ========== 最推荐的调用方式(on/off/emit) ==========
export const on = <T>(
event: string,
handler: EventHandler<T>,
immediate = true
): void => {
bus.on(event, handler, immediate)
}
export const off = <T>(event: string, handler: EventHandler<T>): void => {
bus.off(event, handler)
}
export const emit = <T>(event: string, data: T, cache = true): void => {
bus.emit(event, data, cache)
}
export const once = <T>(event: string, handler: EventHandler<T>): void => {
bus.once(event, handler)
}
export const get = <T>(event: string): T | undefined => {
return bus.get<T>(event)
}
export const clear = (event: string): void => {
bus.clear(event)
}
export const destroy = (): void => {
bus.destroy()
}
export const listenerCount = (event: string): number => {
return bus.listenerCount(event)
}
// ========== 共享数据(事件总线) ==========
/**
* 发布共享数据
*/
export const publishSharedData = <T>(key: string, data: T): void => {
emit(key, data)
}
/**
* 获取共享数据
*/
export const getSharedData = <T>(key: string): T | undefined => {
return get<T>(key)
}
/**
* 清除共享数据
*/
export const clearSharedData = (key: string): void => {
clear(key)
}
/**
* 订阅共享数据变化
*/
export const subscribeSharedData = <T>(
key: string,
callback: SharedDataSubscriber<T>
): void => {
on(key, callback)
}
/**
* 取消订阅共享数据变化
*/
export const unsubscribeSharedData = <T>(
key: string,
callback: SharedDataSubscriber<T>
): void => {
off(key, callback)
}
// ========== 事件总线扩展 API ==========
/**
* 一次性订阅
*/
export const onceSharedData = <T>(
key: string,
callback: SharedDataSubscriber<T>
): void => {
once(key, callback)
}
// 导出实例(供高级用法)
export { EventBus, bus }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
