Home Reference Source

cables_dev/cables/src/core/eventtarget.js

  1. import { Logger } from "cables-shared-client";
  2.  
  3. const EventTarget = function ()
  4. {
  5. this._log = new Logger("eventtarget");
  6. this._eventCallbacks = {};
  7. this._logName = "";
  8. this._logEvents = false;
  9. this._listeners = {};
  10.  
  11. this.addEventListener = this.on = function (which, cb, idPrefix)
  12. {
  13. const event =
  14. {
  15. "id": (idPrefix || "") + CABLES.simpleId(),
  16. "name": which,
  17. "cb": cb,
  18. };
  19. if (!this._eventCallbacks[which]) this._eventCallbacks[which] = [event];
  20. else this._eventCallbacks[which].push(event);
  21.  
  22. this._listeners[event.id] = event;
  23.  
  24. return event.id;
  25. };
  26.  
  27. this.hasEventListener = function (which, cb)
  28. {
  29. if (which && !cb)
  30. {
  31. // check by id
  32. if (this._listeners[which]) return true;
  33. else return false;
  34. }
  35. else
  36. {
  37. this._log.warn("old eventtarget function haseventlistener!");
  38. if (which && cb)
  39. {
  40. if (this._eventCallbacks[which])
  41. {
  42. const idx = this._eventCallbacks[which].indexOf(cb);
  43. if (idx == -1) return false;
  44. return true;
  45. }
  46. }
  47. }
  48. };
  49.  
  50. this.hasListenerForEventName = function (eventName)
  51. {
  52. return this._eventCallbacks[eventName] && this._eventCallbacks[eventName].length > 0;
  53. };
  54.  
  55. this.removeEventListener = this.off = function (which, cb)
  56. {
  57. if (which === null || which === undefined) return;
  58.  
  59. if (!cb) // new style, remove by id, not by name/callback
  60. {
  61. const event = this._listeners[which];
  62. if (!event)
  63. {
  64. this._log.log("removeEvent: could not find event...", which, this);
  65. return;
  66. }
  67.  
  68. let found = true;
  69. while (found)
  70. {
  71. found = false;
  72. let index = -1;
  73. for (let i = 0; i < this._eventCallbacks[event.name].length; i++)
  74. {
  75. if (this._eventCallbacks[event.name][i].id.startsWith(which)) // this._eventCallbacks[event.name][i].id == which ||
  76. {
  77. found = true;
  78. index = i;
  79. }
  80. }
  81.  
  82. if (index !== -1)
  83. {
  84. this._eventCallbacks[event.name].splice(index, 1);
  85. delete this._listeners[which];
  86. }
  87. }
  88.  
  89. return;
  90. }
  91.  
  92. this._log.info("[eventtaget] ", "old function signature: removeEventListener! use listener id");
  93. this._log.log((new Error()).stack);
  94.  
  95. let index = null;
  96. for (let i = 0; i < this._eventCallbacks[which].length; i++)
  97. if (this._eventCallbacks[which][i].cb == cb)
  98. index = i;
  99.  
  100. if (index !== null)
  101. {
  102. delete this._eventCallbacks[index];
  103. }
  104. else this._log.warn("removeEventListener not found " + which);
  105. };
  106.  
  107. this.logEvents = function (enabled, name)
  108. {
  109. this._logEvents = enabled;
  110. this._logName = name;
  111. };
  112.  
  113. this.emitEvent = function (which, param1, param2, param3, param4, param5, param6)
  114. {
  115. if (this._logEvents) this._log.log("[event] ", this._logName, which, this._eventCallbacks);
  116.  
  117. if (this._eventCallbacks[which])
  118. {
  119. for (let i = 0; i < this._eventCallbacks[which].length; i++)
  120. {
  121. if (this._eventCallbacks[which][i])
  122. {
  123. this._eventCallbacks[which][i].cb(param1, param2, param3, param4, param5, param6);
  124. }
  125. }
  126. }
  127. else
  128. {
  129. if (this._logEvents) this._log.log("[event] has no event callback", which, this._eventCallbacks);
  130. }
  131. };
  132. };
  133.  
  134. export { EventTarget };