4 * Class representing an event.
10 * Create a new `Event`.
12 * @param {String} type The name of the event
13 * @param {Object} target A reference to the target to which the event was
16 constructor(type, target) {
23 * Class representing a message event.
28 class MessageEvent extends Event {
30 * Create a new `MessageEvent`.
32 * @param {(String|Buffer|ArrayBuffer|Buffer[])} data The received data
33 * @param {WebSocket} target A reference to the target to which the event was
36 constructor(data, target) {
37 super('message', target);
44 * Class representing a close event.
49 class CloseEvent extends Event {
51 * Create a new `CloseEvent`.
53 * @param {Number} code The status code explaining why the connection is being
55 * @param {String} reason A human-readable string explaining why the
56 * connection is closing
57 * @param {WebSocket} target A reference to the target to which the event was
60 constructor(code, reason, target) {
61 super('close', target);
63 this.wasClean = target._closeFrameReceived && target._closeFrameSent;
70 * Class representing an open event.
75 class OpenEvent extends Event {
77 * Create a new `OpenEvent`.
79 * @param {WebSocket} target A reference to the target to which the event was
83 super('open', target);
88 * Class representing an error event.
93 class ErrorEvent extends Event {
95 * Create a new `ErrorEvent`.
97 * @param {Object} error The error that generated this event
98 * @param {WebSocket} target A reference to the target to which the event was
101 constructor(error, target) {
102 super('error', target);
104 this.message = error.message;
110 * This provides methods for emulating the `EventTarget` interface. It's not
111 * meant to be used directly.
115 const EventTarget = {
117 * Register an event listener.
119 * @param {String} type A string representing the event type to listen for
120 * @param {Function} listener The listener to add
121 * @param {Object} [options] An options object specifies characteristics about
123 * @param {Boolean} [options.once=false] A `Boolean`` indicating that the
124 * listener should be invoked at most once after being added. If `true`,
125 * the listener would be automatically removed when invoked.
128 addEventListener(type, listener, options) {
129 if (typeof listener !== 'function') return;
131 function onMessage(data) {
132 listener.call(this, new MessageEvent(data, this));
135 function onClose(code, message) {
136 listener.call(this, new CloseEvent(code, message, this));
139 function onError(error) {
140 listener.call(this, new ErrorEvent(error, this));
144 listener.call(this, new OpenEvent(this));
147 const method = options && options.once ? 'once' : 'on';
149 if (type === 'message') {
150 onMessage._listener = listener;
151 this[method](type, onMessage);
152 } else if (type === 'close') {
153 onClose._listener = listener;
154 this[method](type, onClose);
155 } else if (type === 'error') {
156 onError._listener = listener;
157 this[method](type, onError);
158 } else if (type === 'open') {
159 onOpen._listener = listener;
160 this[method](type, onOpen);
162 this[method](type, listener);
167 * Remove an event listener.
169 * @param {String} type A string representing the event type to remove
170 * @param {Function} listener The listener to remove
173 removeEventListener(type, listener) {
174 const listeners = this.listeners(type);
176 for (let i = 0; i < listeners.length; i++) {
177 if (listeners[i] === listener || listeners[i]._listener === listener) {
178 this.removeListener(type, listeners[i]);
184 module.exports = EventTarget;