删除 imp.js
This commit is contained in:
parent
cc02373fa3
commit
8348059dac
42
imp.js
42
imp.js
@ -1,42 +0,0 @@
|
||||
class SimplePubSub {
|
||||
events = [];
|
||||
subscribe(event, listener) {
|
||||
this.events.push({ event, listener });
|
||||
}
|
||||
|
||||
unsubscribe(event, listener) {
|
||||
this.events = this.events.filter(item => item.event !== event || item.listener !== listener); ;
|
||||
}
|
||||
publish(event, data) {
|
||||
this.events.forEach(item => {
|
||||
if (item.event === event) {
|
||||
item.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!');
|
Loading…
Reference in New Issue
Block a user