EventSystem.js 452 B

1234567891011121314151617181920212223
  1. export default class EventSystem {
  2. constructor() {
  3. this.listeners = { test: () => console.log('This is default listener!')};
  4. // helper renames
  5. this.addEventListener = this.on;
  6. this.removeEventListener = this.off;
  7. }
  8. on(eventId, fn) {
  9. this.listeners[eventId] = fn;
  10. return this;
  11. }
  12. off(eventId) {
  13. delete this.listeners[eventId];
  14. return this;
  15. }
  16. emit(eventId, data) {
  17. return this.listeners[eventId](data); // return result
  18. }
  19. }