Projekt

Obecné

Profil

Stáhnout (13 KB) Statistiky
| Větev: | Revize:
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21

    
22
'use strict';
23

    
24
var R = typeof Reflect === 'object' ? Reflect : null
25
var ReflectApply = R && typeof R.apply === 'function'
26
  ? R.apply
27
  : function ReflectApply(target, receiver, args) {
28
    return Function.prototype.apply.call(target, receiver, args);
29
  }
30

    
31
var ReflectOwnKeys
32
if (R && typeof R.ownKeys === 'function') {
33
  ReflectOwnKeys = R.ownKeys
34
} else if (Object.getOwnPropertySymbols) {
35
  ReflectOwnKeys = function ReflectOwnKeys(target) {
36
    return Object.getOwnPropertyNames(target)
37
      .concat(Object.getOwnPropertySymbols(target));
38
  };
39
} else {
40
  ReflectOwnKeys = function ReflectOwnKeys(target) {
41
    return Object.getOwnPropertyNames(target);
42
  };
43
}
44

    
45
function ProcessEmitWarning(warning) {
46
  if (console && console.warn) console.warn(warning);
47
}
48

    
49
var NumberIsNaN = Number.isNaN || function NumberIsNaN(value) {
50
  return value !== value;
51
}
52

    
53
function EventEmitter() {
54
  EventEmitter.init.call(this);
55
}
56
module.exports = EventEmitter;
57

    
58
// Backwards-compat with node 0.10.x
59
EventEmitter.EventEmitter = EventEmitter;
60

    
61
EventEmitter.prototype._events = undefined;
62
EventEmitter.prototype._eventsCount = 0;
63
EventEmitter.prototype._maxListeners = undefined;
64

    
65
// By default EventEmitters will print a warning if more than 10 listeners are
66
// added to it. This is a useful default which helps finding memory leaks.
67
var defaultMaxListeners = 10;
68

    
69
function checkListener(listener) {
70
  if (typeof listener !== 'function') {
71
    throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener);
72
  }
73
}
74

    
75
Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
76
  enumerable: true,
77
  get: function() {
78
    return defaultMaxListeners;
79
  },
80
  set: function(arg) {
81
    if (typeof arg !== 'number' || arg < 0 || NumberIsNaN(arg)) {
82
      throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received ' + arg + '.');
83
    }
84
    defaultMaxListeners = arg;
85
  }
86
});
87

    
88
EventEmitter.init = function() {
89

    
90
  if (this._events === undefined ||
91
      this._events === Object.getPrototypeOf(this)._events) {
92
    this._events = Object.create(null);
93
    this._eventsCount = 0;
94
  }
95

    
96
  this._maxListeners = this._maxListeners || undefined;
97
};
98

    
99
// Obviously not all Emitters should be limited to 10. This function allows
100
// that to be increased. Set to zero for unlimited.
101
EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
102
  if (typeof n !== 'number' || n < 0 || NumberIsNaN(n)) {
103
    throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received ' + n + '.');
104
  }
105
  this._maxListeners = n;
106
  return this;
107
};
108

    
109
function _getMaxListeners(that) {
110
  if (that._maxListeners === undefined)
111
    return EventEmitter.defaultMaxListeners;
112
  return that._maxListeners;
113
}
114

    
115
EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
116
  return _getMaxListeners(this);
117
};
118

    
119
EventEmitter.prototype.emit = function emit(type) {
120
  var args = [];
121
  for (var i = 1; i < arguments.length; i++) args.push(arguments[i]);
122
  var doError = (type === 'error');
123

    
124
  var events = this._events;
125
  if (events !== undefined)
126
    doError = (doError && events.error === undefined);
127
  else if (!doError)
128
    return false;
129

    
130
  // If there is no 'error' event listener then throw.
131
  if (doError) {
132
    var er;
133
    if (args.length > 0)
134
      er = args[0];
135
    if (er instanceof Error) {
136
      // Note: The comments on the `throw` lines are intentional, they show
137
      // up in Node's output if this results in an unhandled exception.
138
      throw er; // Unhandled 'error' event
139
    }
140
    // At least give some kind of context to the user
141
    var err = new Error('Unhandled error.' + (er ? ' (' + er.message + ')' : ''));
142
    err.context = er;
143
    throw err; // Unhandled 'error' event
144
  }
145

    
146
  var handler = events[type];
147

    
148
  if (handler === undefined)
149
    return false;
150

    
151
  if (typeof handler === 'function') {
152
    ReflectApply(handler, this, args);
153
  } else {
154
    var len = handler.length;
155
    var listeners = arrayClone(handler, len);
156
    for (var i = 0; i < len; ++i)
157
      ReflectApply(listeners[i], this, args);
158
  }
159

    
160
  return true;
161
};
162

    
163
function _addListener(target, type, listener, prepend) {
164
  var m;
165
  var events;
166
  var existing;
167

    
168
  checkListener(listener);
169

    
170
  events = target._events;
171
  if (events === undefined) {
172
    events = target._events = Object.create(null);
173
    target._eventsCount = 0;
174
  } else {
175
    // To avoid recursion in the case that type === "newListener"! Before
176
    // adding it to the listeners, first emit "newListener".
177
    if (events.newListener !== undefined) {
178
      target.emit('newListener', type,
179
                  listener.listener ? listener.listener : listener);
180

    
181
      // Re-assign `events` because a newListener handler could have caused the
182
      // this._events to be assigned to a new object
183
      events = target._events;
184
    }
185
    existing = events[type];
186
  }
187

    
188
  if (existing === undefined) {
189
    // Optimize the case of one listener. Don't need the extra array object.
190
    existing = events[type] = listener;
191
    ++target._eventsCount;
192
  } else {
193
    if (typeof existing === 'function') {
194
      // Adding the second element, need to change to array.
195
      existing = events[type] =
196
        prepend ? [listener, existing] : [existing, listener];
197
      // If we've already got an array, just append.
198
    } else if (prepend) {
199
      existing.unshift(listener);
200
    } else {
201
      existing.push(listener);
202
    }
203

    
204
    // Check for listener leak
205
    m = _getMaxListeners(target);
206
    if (m > 0 && existing.length > m && !existing.warned) {
207
      existing.warned = true;
208
      // No error code for this since it is a Warning
209
      // eslint-disable-next-line no-restricted-syntax
210
      var w = new Error('Possible EventEmitter memory leak detected. ' +
211
                          existing.length + ' ' + String(type) + ' listeners ' +
212
                          'added. Use emitter.setMaxListeners() to ' +
213
                          'increase limit');
214
      w.name = 'MaxListenersExceededWarning';
215
      w.emitter = target;
216
      w.type = type;
217
      w.count = existing.length;
218
      ProcessEmitWarning(w);
219
    }
220
  }
221

    
222
  return target;
223
}
224

    
225
EventEmitter.prototype.addListener = function addListener(type, listener) {
226
  return _addListener(this, type, listener, false);
227
};
228

    
229
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
230

    
231
EventEmitter.prototype.prependListener =
232
    function prependListener(type, listener) {
233
      return _addListener(this, type, listener, true);
234
    };
235

    
236
function onceWrapper() {
237
  if (!this.fired) {
238
    this.target.removeListener(this.type, this.wrapFn);
239
    this.fired = true;
240
    if (arguments.length === 0)
241
      return this.listener.call(this.target);
242
    return this.listener.apply(this.target, arguments);
243
  }
244
}
245

    
246
function _onceWrap(target, type, listener) {
247
  var state = { fired: false, wrapFn: undefined, target: target, type: type, listener: listener };
248
  var wrapped = onceWrapper.bind(state);
249
  wrapped.listener = listener;
250
  state.wrapFn = wrapped;
251
  return wrapped;
252
}
253

    
254
EventEmitter.prototype.once = function once(type, listener) {
255
  checkListener(listener);
256
  this.on(type, _onceWrap(this, type, listener));
257
  return this;
258
};
259

    
260
EventEmitter.prototype.prependOnceListener =
261
    function prependOnceListener(type, listener) {
262
      checkListener(listener);
263
      this.prependListener(type, _onceWrap(this, type, listener));
264
      return this;
265
    };
266

    
267
// Emits a 'removeListener' event if and only if the listener was removed.
268
EventEmitter.prototype.removeListener =
269
    function removeListener(type, listener) {
270
      var list, events, position, i, originalListener;
271

    
272
      checkListener(listener);
273

    
274
      events = this._events;
275
      if (events === undefined)
276
        return this;
277

    
278
      list = events[type];
279
      if (list === undefined)
280
        return this;
281

    
282
      if (list === listener || list.listener === listener) {
283
        if (--this._eventsCount === 0)
284
          this._events = Object.create(null);
285
        else {
286
          delete events[type];
287
          if (events.removeListener)
288
            this.emit('removeListener', type, list.listener || listener);
289
        }
290
      } else if (typeof list !== 'function') {
291
        position = -1;
292

    
293
        for (i = list.length - 1; i >= 0; i--) {
294
          if (list[i] === listener || list[i].listener === listener) {
295
            originalListener = list[i].listener;
296
            position = i;
297
            break;
298
          }
299
        }
300

    
301
        if (position < 0)
302
          return this;
303

    
304
        if (position === 0)
305
          list.shift();
306
        else {
307
          spliceOne(list, position);
308
        }
309

    
310
        if (list.length === 1)
311
          events[type] = list[0];
312

    
313
        if (events.removeListener !== undefined)
314
          this.emit('removeListener', type, originalListener || listener);
315
      }
316

    
317
      return this;
318
    };
319

    
320
EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
321

    
322
EventEmitter.prototype.removeAllListeners =
323
    function removeAllListeners(type) {
324
      var listeners, events, i;
325

    
326
      events = this._events;
327
      if (events === undefined)
328
        return this;
329

    
330
      // not listening for removeListener, no need to emit
331
      if (events.removeListener === undefined) {
332
        if (arguments.length === 0) {
333
          this._events = Object.create(null);
334
          this._eventsCount = 0;
335
        } else if (events[type] !== undefined) {
336
          if (--this._eventsCount === 0)
337
            this._events = Object.create(null);
338
          else
339
            delete events[type];
340
        }
341
        return this;
342
      }
343

    
344
      // emit removeListener for all listeners on all events
345
      if (arguments.length === 0) {
346
        var keys = Object.keys(events);
347
        var key;
348
        for (i = 0; i < keys.length; ++i) {
349
          key = keys[i];
350
          if (key === 'removeListener') continue;
351
          this.removeAllListeners(key);
352
        }
353
        this.removeAllListeners('removeListener');
354
        this._events = Object.create(null);
355
        this._eventsCount = 0;
356
        return this;
357
      }
358

    
359
      listeners = events[type];
360

    
361
      if (typeof listeners === 'function') {
362
        this.removeListener(type, listeners);
363
      } else if (listeners !== undefined) {
364
        // LIFO order
365
        for (i = listeners.length - 1; i >= 0; i--) {
366
          this.removeListener(type, listeners[i]);
367
        }
368
      }
369

    
370
      return this;
371
    };
372

    
373
function _listeners(target, type, unwrap) {
374
  var events = target._events;
375

    
376
  if (events === undefined)
377
    return [];
378

    
379
  var evlistener = events[type];
380
  if (evlistener === undefined)
381
    return [];
382

    
383
  if (typeof evlistener === 'function')
384
    return unwrap ? [evlistener.listener || evlistener] : [evlistener];
385

    
386
  return unwrap ?
387
    unwrapListeners(evlistener) : arrayClone(evlistener, evlistener.length);
388
}
389

    
390
EventEmitter.prototype.listeners = function listeners(type) {
391
  return _listeners(this, type, true);
392
};
393

    
394
EventEmitter.prototype.rawListeners = function rawListeners(type) {
395
  return _listeners(this, type, false);
396
};
397

    
398
EventEmitter.listenerCount = function(emitter, type) {
399
  if (typeof emitter.listenerCount === 'function') {
400
    return emitter.listenerCount(type);
401
  } else {
402
    return listenerCount.call(emitter, type);
403
  }
404
};
405

    
406
EventEmitter.prototype.listenerCount = listenerCount;
407
function listenerCount(type) {
408
  var events = this._events;
409

    
410
  if (events !== undefined) {
411
    var evlistener = events[type];
412

    
413
    if (typeof evlistener === 'function') {
414
      return 1;
415
    } else if (evlistener !== undefined) {
416
      return evlistener.length;
417
    }
418
  }
419

    
420
  return 0;
421
}
422

    
423
EventEmitter.prototype.eventNames = function eventNames() {
424
  return this._eventsCount > 0 ? ReflectOwnKeys(this._events) : [];
425
};
426

    
427
function arrayClone(arr, n) {
428
  var copy = new Array(n);
429
  for (var i = 0; i < n; ++i)
430
    copy[i] = arr[i];
431
  return copy;
432
}
433

    
434
function spliceOne(list, index) {
435
  for (; index + 1 < list.length; index++)
436
    list[index] = list[index + 1];
437
  list.pop();
438
}
439

    
440
function unwrapListeners(arr) {
441
  var ret = new Array(arr.length);
442
  for (var i = 0; i < ret.length; ++i) {
443
    ret[i] = arr[i].listener || arr[i];
444
  }
445
  return ret;
446
}
(6-6/8)