31 lines
724 B
JavaScript
31 lines
724 B
JavaScript
|
export class EventBus {
|
||
|
constructor() {
|
||
|
this.events = new Map();
|
||
|
}
|
||
|
emit(id, data) {
|
||
|
let ev = this.events.get(id);
|
||
|
if (!ev) {
|
||
|
let ev = new EventItem(id);
|
||
|
this.events.set(id, ev);
|
||
|
return;
|
||
|
}
|
||
|
ev.subscribers.forEach((subscriber) => {
|
||
|
subscriber(data);
|
||
|
});
|
||
|
}
|
||
|
subscribe(id, subscriber) {
|
||
|
let ev = this.events.get(id);
|
||
|
if (!ev) {
|
||
|
ev = new EventItem(id);
|
||
|
this.events.set(id, ev);
|
||
|
}
|
||
|
ev.subscribers.push(subscriber);
|
||
|
}
|
||
|
}
|
||
|
export class EventItem {
|
||
|
constructor(id) {
|
||
|
this.id = id;
|
||
|
this.subscribers = [];
|
||
|
}
|
||
|
}
|