UNPKG

787 BJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3function eventBus() {
4 const eventHandlerMap = new Map();
5 function publish(channel, event) {
6 const eventHandlers = eventHandlerMap.get(channel);
7 if (!eventHandlers) {
8 return;
9 }
10 for (const handle of eventHandlers) {
11 handle(event);
12 }
13 }
14 function subscribe(channel, handler) {
15 let eventHandlers = eventHandlerMap.get(channel);
16 if (!eventHandlers) {
17 eventHandlers = new Set();
18 eventHandlerMap.set(channel, eventHandlers);
19 }
20 eventHandlers.add(handler);
21 }
22 const bus = {
23 publish,
24 subscribe,
25 };
26 return Object.freeze(bus);
27}
28exports.default = eventBus;