primevue-mirror/components/utils/EventBus.js

33 lines
760 B
JavaScript
Raw Normal View History

2021-04-15 14:53:33 +00:00
export default function primebus() {
2022-09-14 11:26:01 +00:00
const allHandlers = new Map();
2021-03-02 08:54:37 +00:00
2022-09-14 11:26:01 +00:00
return {
on(type, handler) {
let handlers = allHandlers.get(type);
if (!handlers) handlers = [handler];
else handlers.push(handler);
2021-04-15 14:53:33 +00:00
allHandlers.set(type, handlers);
2022-09-14 11:26:01 +00:00
},
2021-04-15 14:53:33 +00:00
2022-09-14 11:26:01 +00:00
off(type, handler) {
let handlers = allHandlers.get(type);
2021-04-15 14:53:33 +00:00
2022-09-14 11:26:01 +00:00
if (handlers) {
handlers.splice(handlers.indexOf(handler) >>> 0, 1);
}
},
emit(type, evt) {
2021-04-15 14:53:33 +00:00
let handlers = allHandlers.get(type);
2022-09-14 11:26:01 +00:00
2021-04-15 14:53:33 +00:00
if (handlers) {
2022-09-14 11:26:01 +00:00
handlers.slice().map((handler) => {
handler(evt);
});
2021-04-06 09:49:34 +00:00
}
2022-09-14 11:26:01 +00:00
}
};
}