ListenerHandler.class.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. export default class ListenerHandler extends EventTarget {
  2. listeners: Record<
  3. string,
  4. {
  5. cb: (event: any) => void;
  6. options: { replaceable: boolean; modalUuid?: string };
  7. }[]
  8. >;
  9. constructor() {
  10. super();
  11. this.listeners = {};
  12. }
  13. addEventListener(type, cb, options) {
  14. // add the listener type to listeners object
  15. if (!(type in this.listeners)) this.listeners[type] = [];
  16. const stack = this.listeners[type];
  17. // push the callback
  18. stack.push({ cb, options });
  19. const replaceableIndexes = [];
  20. // check for any replaceable callbacks
  21. stack.forEach((element, index) => {
  22. if (element.options && element.options.replaceable)
  23. replaceableIndexes.push(index);
  24. });
  25. // should always be 1 replaceable callback remaining
  26. replaceableIndexes.pop();
  27. // delete the other replaceable callbacks
  28. replaceableIndexes.forEach(index => stack.splice(index, 1));
  29. }
  30. // eslint-disable-next-line consistent-return
  31. removeEventListener(type, cb) {
  32. if (!(type in this.listeners)) return true; // event type doesn't exist
  33. const stack = this.listeners[type];
  34. stack.forEach((element, index) => {
  35. if (element.cb === cb) stack.splice(index, 1);
  36. });
  37. }
  38. dispatchEvent(event) {
  39. if (!(event.type in this.listeners)) return true; // event type doesn't exist
  40. const stack = this.listeners[event.type].slice();
  41. stack.forEach(element => element.cb.call(this, event));
  42. return !event.defaultPrevented;
  43. }
  44. }