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
|
/*
* ***** BEGIN LICENSE BLOCK *****
* Copyright (c) 2011-2012 VMware, Inc.
*
* For the license see COPYING.
* ***** END LICENSE BLOCK *****
*/
var EventEmitter = function(events) {
var that = this;
that._events = events || [];
that._listeners = {};
};
EventEmitter.prototype.emit = function(type) {
var that = this;
that._verifyType(type);
if (that._nuked) return;
var args = Array.prototype.slice.call(arguments, 1);
if (that['on'+type]) {
that['on'+type].apply(that, args);
}
if (type in that._listeners) {
for(var i = 0; i < that._listeners[type].length; i++) {
that._listeners[type][i].apply(that, args);
}
}
};
EventEmitter.prototype.on = function(type, callback) {
var that = this;
that._verifyType(type);
if (that._nuked) return;
if (!(type in that._listeners)) {
that._listeners[type] = [];
}
that._listeners[type].push(callback);
};
EventEmitter.prototype._verifyType = function(type) {
var that = this;
if (utils.arrIndexOf(that._events, type) === -1) {
utils.log('Event ' + JSON.stringify(type) +
' not listed ' + JSON.stringify(that._events) +
' in ' + that);
}
};
EventEmitter.prototype.nuke = function() {
var that = this;
that._nuked = true;
for(var i=0; i<that._events.length; i++) {
delete that[that._events[i]];
}
that._listeners = {};
};
|