Last change
on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var inherits = require('inherits')
|
---|
| 4 | , EventTarget = require('./eventtarget')
|
---|
| 5 | ;
|
---|
| 6 |
|
---|
| 7 | function EventEmitter() {
|
---|
| 8 | EventTarget.call(this);
|
---|
| 9 | }
|
---|
| 10 |
|
---|
| 11 | inherits(EventEmitter, EventTarget);
|
---|
| 12 |
|
---|
| 13 | EventEmitter.prototype.removeAllListeners = function(type) {
|
---|
| 14 | if (type) {
|
---|
| 15 | delete this._listeners[type];
|
---|
| 16 | } else {
|
---|
| 17 | this._listeners = {};
|
---|
| 18 | }
|
---|
| 19 | };
|
---|
| 20 |
|
---|
| 21 | EventEmitter.prototype.once = function(type, listener) {
|
---|
| 22 | var self = this
|
---|
| 23 | , fired = false;
|
---|
| 24 |
|
---|
| 25 | function g() {
|
---|
| 26 | self.removeListener(type, g);
|
---|
| 27 |
|
---|
| 28 | if (!fired) {
|
---|
| 29 | fired = true;
|
---|
| 30 | listener.apply(this, arguments);
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | this.on(type, g);
|
---|
| 35 | };
|
---|
| 36 |
|
---|
| 37 | EventEmitter.prototype.emit = function() {
|
---|
| 38 | var type = arguments[0];
|
---|
| 39 | var listeners = this._listeners[type];
|
---|
| 40 | if (!listeners) {
|
---|
| 41 | return;
|
---|
| 42 | }
|
---|
| 43 | // equivalent of Array.prototype.slice.call(arguments, 1);
|
---|
| 44 | var l = arguments.length;
|
---|
| 45 | var args = new Array(l - 1);
|
---|
| 46 | for (var ai = 1; ai < l; ai++) {
|
---|
| 47 | args[ai - 1] = arguments[ai];
|
---|
| 48 | }
|
---|
| 49 | for (var i = 0; i < listeners.length; i++) {
|
---|
| 50 | listeners[i].apply(this, args);
|
---|
| 51 | }
|
---|
| 52 | };
|
---|
| 53 |
|
---|
| 54 | EventEmitter.prototype.on = EventEmitter.prototype.addListener = EventTarget.prototype.addEventListener;
|
---|
| 55 | EventEmitter.prototype.removeListener = EventTarget.prototype.removeEventListener;
|
---|
| 56 |
|
---|
| 57 | module.exports.EventEmitter = EventEmitter;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.