Projekt

Obecné

Profil

Stáhnout (3.84 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2

    
3
/**
4
 * Class representing an event.
5
 *
6
 * @private
7
 */
8
class Event {
9
  /**
10
   * Create a new `Event`.
11
   *
12
   * @param {String} type The name of the event
13
   * @param {Object} target A reference to the target to which the event was dispatched
14
   */
15
  constructor(type, target) {
16
    this.target = target;
17
    this.type = type;
18
  }
19
}
20

    
21
/**
22
 * Class representing a message event.
23
 *
24
 * @extends Event
25
 * @private
26
 */
27
class MessageEvent extends Event {
28
  /**
29
   * Create a new `MessageEvent`.
30
   *
31
   * @param {(String|Buffer|ArrayBuffer|Buffer[])} data The received data
32
   * @param {WebSocket} target A reference to the target to which the event was dispatched
33
   */
34
  constructor(data, target) {
35
    super('message', target);
36

    
37
    this.data = data;
38
  }
39
}
40

    
41
/**
42
 * Class representing a close event.
43
 *
44
 * @extends Event
45
 * @private
46
 */
47
class CloseEvent extends Event {
48
  /**
49
   * Create a new `CloseEvent`.
50
   *
51
   * @param {Number} code The status code explaining why the connection is being closed
52
   * @param {String} reason A human-readable string explaining why the connection is closing
53
   * @param {WebSocket} target A reference to the target to which the event was dispatched
54
   */
55
  constructor(code, reason, target) {
56
    super('close', target);
57

    
58
    this.wasClean = target._closeFrameReceived && target._closeFrameSent;
59
    this.reason = reason;
60
    this.code = code;
61
  }
62
}
63

    
64
/**
65
 * Class representing an open event.
66
 *
67
 * @extends Event
68
 * @private
69
 */
70
class OpenEvent extends Event {
71
  /**
72
   * Create a new `OpenEvent`.
73
   *
74
   * @param {WebSocket} target A reference to the target to which the event was dispatched
75
   */
76
  constructor(target) {
77
    super('open', target);
78
  }
79
}
80

    
81
/**
82
 * Class representing an error event.
83
 *
84
 * @extends Event
85
 * @private
86
 */
87
class ErrorEvent extends Event {
88
  /**
89
   * Create a new `ErrorEvent`.
90
   *
91
   * @param {Object} error The error that generated this event
92
   * @param {WebSocket} target A reference to the target to which the event was dispatched
93
   */
94
  constructor(error, target) {
95
    super('error', target);
96

    
97
    this.message = error.message;
98
    this.error = error;
99
  }
100
}
101

    
102
/**
103
 * This provides methods for emulating the `EventTarget` interface. It's not
104
 * meant to be used directly.
105
 *
106
 * @mixin
107
 */
108
const EventTarget = {
109
  /**
110
   * Register an event listener.
111
   *
112
   * @param {String} method A string representing the event type to listen for
113
   * @param {Function} listener The listener to add
114
   * @public
115
   */
116
  addEventListener(method, listener) {
117
    if (typeof listener !== 'function') return;
118

    
119
    function onMessage(data) {
120
      listener.call(this, new MessageEvent(data, this));
121
    }
122

    
123
    function onClose(code, message) {
124
      listener.call(this, new CloseEvent(code, message, this));
125
    }
126

    
127
    function onError(error) {
128
      listener.call(this, new ErrorEvent(error, this));
129
    }
130

    
131
    function onOpen() {
132
      listener.call(this, new OpenEvent(this));
133
    }
134

    
135
    if (method === 'message') {
136
      onMessage._listener = listener;
137
      this.on(method, onMessage);
138
    } else if (method === 'close') {
139
      onClose._listener = listener;
140
      this.on(method, onClose);
141
    } else if (method === 'error') {
142
      onError._listener = listener;
143
      this.on(method, onError);
144
    } else if (method === 'open') {
145
      onOpen._listener = listener;
146
      this.on(method, onOpen);
147
    } else {
148
      this.on(method, listener);
149
    }
150
  },
151

    
152
  /**
153
   * Remove an event listener.
154
   *
155
   * @param {String} method A string representing the event type to remove
156
   * @param {Function} listener The listener to remove
157
   * @public
158
   */
159
  removeEventListener(method, listener) {
160
    const listeners = this.listeners(method);
161

    
162
    for (var i = 0; i < listeners.length; i++) {
163
      if (listeners[i] === listener || listeners[i]._listener === listener) {
164
        this.removeListener(method, listeners[i]);
165
      }
166
    }
167
  }
168
};
169

    
170
module.exports = EventTarget;
(3-3/10)