Skip to content

Commit

Permalink
Refactor #1832 - Added EventBus
Browse files Browse the repository at this point in the history
  • Loading branch information
mertsincan committed Mar 1, 2021
1 parent c09f037 commit 699d069
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/components/utils/PrimeEventBus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function handler() {
let eventTarget = document.body.appendChild(document.createComment(''));
let eventListener, eventKey;

return {
on(type, listener, key) {
eventKey = key;
eventListener = function(e) { listener(e.detail); };
eventTarget.addEventListener(type, eventListener);
},
once(type, listener, key) {
eventKey = key;
eventListener = function(e) { listener(e.detail); };
eventTarget.addEventListener(type, eventListener, { once: true });
},
off(type, key) {
if (eventListener && eventKey === key) {
eventTarget.removeEventListener(type, eventListener);
}
},
emit(type, detail) {
eventTarget.dispatchEvent(new CustomEvent(type, { detail }));
}
}
}

export const PrimeEventBus = handler();

0 comments on commit 699d069

Please sign in to comment.