primevue-mirror/components/utils/EventBus.js

29 lines
658 B
JavaScript
Raw Normal View History

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