Code coverage report for node/lib/event_emitter.js

Statements: 89.86% (62 / 69)      Branches: 80.56% (29 / 36)      Functions: 84.62% (11 / 13)      Lines: 89.86% (62 / 69)      Ignored: none     

All files » node/lib/ » event_emitter.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144                                            1   1     1   22839 6473   22839     1     4152 4152 1 4907       1   2597 2597 1947 650 650 650 1300 650     650 650   650       1           1   5     1 22839 22839 22839 22839     1   25847 10762 15085 2384 2384 5606   12701 1       1     20673 1672 19001 3539 3539   15462       1   157       157 157 157 449 292     157 132   157       1           1 1     1     1  
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
 
'use strict';
 
var assert = require('assert');
 
function EventEmitter() {
}
 
EventEmitter.prototype.defineEvent =
function defineEvent(type, defaultListener) {
    if (!defaultListener && type === 'error') {
        defaultListener = defaultErrorListener;
    }
    return new DefinedEvent(type, defaultListener);
};
 
EventEmitter.prototype.on =
EventEmitter.prototype.addListener =
function addListener(type, listener) {
    this[type + 'Event'].addListener(listenerCompatShim);
    listenerCompatShim.listener = listener;
    function listenerCompatShim(arg, self) {
        listener.call(self, arg);
    }
};
 
EventEmitter.prototype.removeListener =
function removeListener(type, listener) {
    var evt = this[type + 'Event'];
    if (evt.listener && evt.listener.listener === listener) {
        evt.listener = null;
    } else Eif (evt.listeners.length) {
        var listeners = [];
        for (var i = 0; i < evt.listeners.length; i++ ) {
            if (evt.listeners[i].listener !== listener) {
                listeners.push(evt.listeners[i]);
            }
        }
        Eif (listeners.length === 1) {
            evt.listener = listeners.pop();
        }
        evt.listeners = listeners;
    }
};
 
EventEmitter.prototype.removeAllListeners =
function removeAllListeners(type) {
    assert(type, 'not implemented: removing all listeners from all event types');
    this[type + 'Event'].removeAllListeners();
};
 
EventEmitter.prototype.emit =
function emit(type, arg) {
    this[type + 'Event'].emit(this, arg);
};
 
function DefinedEvent(type, defaultListener) {
    this.type = type;
    this.defaultListener = defaultListener;
    this.listener = null;
    this.listeners = [];
}
 
DefinedEvent.prototype.emit =
function emit(that, arg) {
    if (this.listener) {
        this.listener(arg, that);
    } else if (this.listeners.length) {
        var listeners = this.listeners;
        for (var i = 0; i < listeners.length; i++ ) {
            listeners[i](arg, that);
        }
    } else if (this.defaultListener) {
        this.defaultListener(arg, that);
    }
};
 
DefinedEvent.prototype.on =
DefinedEvent.prototype.addListener =
function addListener(listener) {
    if (this.listeners.length) {
        this.listeners.push(listener);
    } else if (this.listener) {
        this.listeners.push(this.listener, listener);
        this.listener = null;
    } else {
        this.listener = listener;
    }
};
 
DefinedEvent.prototype.removeListener =
function removeListener(listener) {
    Iif (this.listener) {
        if (this.listener === listener) {
            this.listener = null;
        }
    } else Eif (this.listeners.length) {
        var listeners = [];
        for (var i = 0; i < this.listeners.length; i++ ) {
            if (this.listeners[i] !== listener) {
                listeners.push(this.listeners[i]);
            }
        }
        if (listeners.length === 1) {
            this.listener = listeners.pop();
        }
        this.listeners = listeners;
    }
};
 
DefinedEvent.prototype.removeAllListeners =
function removeAllListeners() {
    this.listener = null;
    this.listeners = [];
};
 
function defaultErrorListener(err) {
    Iif (!err) {
        err = new TypeError('Uncaught, unspecified "error" event.');
    }
    throw err;
}
 
module.exports = EventEmitter;