Code coverage report for sockjs-client/lib/reventtarget.js

Statements: 100% (31 / 31)      Branches: 100% (20 / 20)      Functions: 100% (4 / 4)      Lines: 100% (31 / 31)      Ignored: none     

All files » sockjs-client/lib/ » reventtarget.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                          1   1   1 13   13 1   13 3   13 13   10   13     1 11     11 1   10 10 10 8   6   2         1 103                 103 96   103     4 4 9         1  
'use strict';
/*
 * ***** BEGIN LICENSE BLOCK *****
 * Copyright (c) 2011-2012 VMware, Inc.
 *
 * For the license see COPYING.
 * ***** END LICENSE BLOCK *****
 */
 
/* Simplified implementation of DOM2 EventTarget.
 *   http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget
 */
 
module.exports = REventTarget;
 
function REventTarget() {}
 
REventTarget.prototype.addEventListener = function (eventType, listener) {
    var arr;
 
    if (!this._listeners) {
         this._listeners = {};
    }
    if (!(eventType in this._listeners)) {
        this._listeners[eventType] = [];
    }
    arr = this._listeners[eventType];
    if (arr.indexOf(listener) === -1) {
        // Make a copy so as not to interfere with a current dispatchEvent.
        arr = arr.concat([listener]);
    }
    this._listeners[eventType] = arr;
};
 
REventTarget.prototype.removeEventListener = function (eventType, listener) {
    var arr,
        idx;
 
    if (!(this._listeners && (eventType in this._listeners))) {
        return;
    }
    arr = this._listeners[eventType];
    idx = arr.indexOf(listener);
    if (idx !== -1) {
        if (arr.length > 1) {
            // Make a copy so as not to interfer with a current dispatchEvent.
            this._listeners[eventType] = arr.slice(0, idx).concat( arr.slice(idx+1) );
        } else {
            delete this._listeners[eventType];
        }
    }
};
 
REventTarget.prototype.dispatchEvent = function (event) {
    var type = event.type,
        args = [].slice.call(arguments, 0),
        listeners,
        i;
 
    // TODO: This doesn't match the real behavior; per spec, onfoo get
    // their place in line from the /first/ time they're set from
    // non-null. Although WebKit bumps it to the end every time it's
    // set.
    if (this['on'+type]) {
        this['on'+type].apply(this, args);
    }
    if (this._listeners && type in this._listeners) {
        // Grab a reference to the listeners list. removeEventListener may
        // remove the list.
        listeners = this._listeners[type];
        for (i = 0; i < listeners.length; i++) {
            listeners[i].apply(this, args);
        }
    }
};
 
module.exports = REventTarget;