event-target.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. 'use strict';
  2. /**
  3. * Class representing an event.
  4. *
  5. * @private
  6. */
  7. class Event {
  8. /**
  9. * Create a new `Event`.
  10. *
  11. * @param {String} type The name of the event
  12. * @param {Object} target A reference to the target to which the event was
  13. * dispatched
  14. */
  15. constructor(type, target) {
  16. this.target = target;
  17. this.type = type;
  18. }
  19. }
  20. /**
  21. * Class representing a message event.
  22. *
  23. * @extends Event
  24. * @private
  25. */
  26. class MessageEvent extends Event {
  27. /**
  28. * Create a new `MessageEvent`.
  29. *
  30. * @param {(String|Buffer|ArrayBuffer|Buffer[])} data The received data
  31. * @param {WebSocket} target A reference to the target to which the event was
  32. * dispatched
  33. */
  34. constructor(data, target) {
  35. super('message', target);
  36. this.data = data;
  37. }
  38. }
  39. /**
  40. * Class representing a close event.
  41. *
  42. * @extends Event
  43. * @private
  44. */
  45. class CloseEvent extends Event {
  46. /**
  47. * Create a new `CloseEvent`.
  48. *
  49. * @param {Number} code The status code explaining why the connection is being
  50. * closed
  51. * @param {String} reason A human-readable string explaining why the
  52. * connection is closing
  53. * @param {WebSocket} target A reference to the target to which the event was
  54. * dispatched
  55. */
  56. constructor(code, reason, target) {
  57. super('close', target);
  58. this.wasClean = target._closeFrameReceived && target._closeFrameSent;
  59. this.reason = reason;
  60. this.code = code;
  61. }
  62. }
  63. /**
  64. * Class representing an open event.
  65. *
  66. * @extends Event
  67. * @private
  68. */
  69. class OpenEvent extends Event {
  70. /**
  71. * Create a new `OpenEvent`.
  72. *
  73. * @param {WebSocket} target A reference to the target to which the event was
  74. * dispatched
  75. */
  76. constructor(target) {
  77. super('open', target);
  78. }
  79. }
  80. /**
  81. * Class representing an error event.
  82. *
  83. * @extends Event
  84. * @private
  85. */
  86. class ErrorEvent extends Event {
  87. /**
  88. * Create a new `ErrorEvent`.
  89. *
  90. * @param {Object} error The error that generated this event
  91. * @param {WebSocket} target A reference to the target to which the event was
  92. * dispatched
  93. */
  94. constructor(error, target) {
  95. super('error', target);
  96. this.message = error.message;
  97. this.error = error;
  98. }
  99. }
  100. /**
  101. * This provides methods for emulating the `EventTarget` interface. It's not
  102. * meant to be used directly.
  103. *
  104. * @mixin
  105. */
  106. const EventTarget = {
  107. /**
  108. * Register an event listener.
  109. *
  110. * @param {String} type A string representing the event type to listen for
  111. * @param {Function} listener The listener to add
  112. * @param {Object} [options] An options object specifies characteristics about
  113. * the event listener
  114. * @param {Boolean} [options.once=false] A `Boolean`` indicating that the
  115. * listener should be invoked at most once after being added. If `true`,
  116. * the listener would be automatically removed when invoked.
  117. * @public
  118. */
  119. addEventListener(type, listener, options) {
  120. if (typeof listener !== 'function') return;
  121. function onMessage(data) {
  122. listener.call(this, new MessageEvent(data, this));
  123. }
  124. function onClose(code, message) {
  125. listener.call(this, new CloseEvent(code, message, this));
  126. }
  127. function onError(error) {
  128. listener.call(this, new ErrorEvent(error, this));
  129. }
  130. function onOpen() {
  131. listener.call(this, new OpenEvent(this));
  132. }
  133. const method = options && options.once ? 'once' : 'on';
  134. if (type === 'message') {
  135. onMessage._listener = listener;
  136. this[method](type, onMessage);
  137. } else if (type === 'close') {
  138. onClose._listener = listener;
  139. this[method](type, onClose);
  140. } else if (type === 'error') {
  141. onError._listener = listener;
  142. this[method](type, onError);
  143. } else if (type === 'open') {
  144. onOpen._listener = listener;
  145. this[method](type, onOpen);
  146. } else {
  147. this[method](type, listener);
  148. }
  149. },
  150. /**
  151. * Remove an event listener.
  152. *
  153. * @param {String} type A string representing the event type to remove
  154. * @param {Function} listener The listener to remove
  155. * @public
  156. */
  157. removeEventListener(type, listener) {
  158. const listeners = this.listeners(type);
  159. for (let i = 0; i < listeners.length; i++) {
  160. if (listeners[i] === listener || listeners[i]._listener === listener) {
  161. this.removeListener(type, listeners[i]);
  162. }
  163. }
  164. }
  165. };
  166. module.exports = EventTarget;