-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodeaa.js
54 lines (43 loc) · 1.14 KB
/
nodeaa.js
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
class SimplePubSub {
constructor() {
this.events = {};
}
subscribe(event, listener) {
if (!this.events[event]) {
this.events[event] = [];
}
this.events[event].push(listener);
}
unsubscribe(event, listener) {
if (!this.events[event]) return;
this.events[event] = this.events[event].filter((l) => l !== listener);
}
publish(event, data) {
if (!this.events[event]) return;
this.events[event].forEach((listener) => listener(data));
}
}
// 创建 PubSub 实例
const pubSub = new SimplePubSub();
// 定义订阅者
const subscriber1 = (data) => {
console.log(`Subscriber 1 received: ${data}`);
};
const subscriber2 = (data) => {
console.log(`Subscriber 2 received: ${data}`);
};
// 订阅事件
pubSub.subscribe("event1", subscriber1);
pubSub.subscribe("event1", subscriber2);
// 发布事件
pubSub.publish("event1", "Hello, World!");
// 取消订阅
pubSub.unsubscribe("event1", subscriber1);
// 发布事件
pubSub.publish("event1", "Hello again!");
// 输出应该是如下的样式
/*
Subscriber 1 received: Hello, World!
Subscriber 2 received: Hello, World!
Subscriber 2 received: Hello again!
*/