mirror of
https://github.com/hanzoai/tractor-beam.git
synced 2026-10-30 18:19:16 +00:00
637 lines
18 KiB
JavaScript
637 lines
18 KiB
JavaScript
var TractorBeam = (function () {
|
|
'use strict';
|
|
|
|
// src/event-emitter.coffee
|
|
var EventEmitter;
|
|
var slice = [].slice;
|
|
|
|
var EventEmitter$1 = EventEmitter = (function() {
|
|
function EventEmitter(opts) {
|
|
var ref;
|
|
if (opts == null) {
|
|
opts = {};
|
|
}
|
|
this.debug = (ref = opts.debug) != null ? ref : false;
|
|
this._listeners = {};
|
|
this._allListeners = [];
|
|
}
|
|
|
|
EventEmitter.prototype.on = function(event, callback) {
|
|
var base;
|
|
if (event) {
|
|
if ((base = this._listeners)[event] == null) {
|
|
base[event] = [];
|
|
}
|
|
this._listeners[event].push(callback);
|
|
return this._listeners[event].length - 1;
|
|
} else {
|
|
this._allListeners.push(callback);
|
|
return this._allListeners.length - 1;
|
|
}
|
|
};
|
|
|
|
EventEmitter.prototype.off = function(event, index) {
|
|
if (!event) {
|
|
return this._listeners = {};
|
|
}
|
|
if (index != null) {
|
|
this._listeners[event][index] = null;
|
|
} else {
|
|
this._listeners[event] = {};
|
|
}
|
|
};
|
|
|
|
EventEmitter.prototype.emit = function() {
|
|
var args, event, i, j, len, len1, listener, listeners, ref;
|
|
event = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
|
|
listeners = this._listeners[event] || [];
|
|
for (i = 0, len = listeners.length; i < len; i++) {
|
|
listener = listeners[i];
|
|
if (listener != null) {
|
|
listener.apply(this, args);
|
|
}
|
|
}
|
|
args.unshift(event);
|
|
ref = this._allListeners;
|
|
for (j = 0, len1 = ref.length; j < len1; j++) {
|
|
listener = ref[j];
|
|
listener.apply(this, args);
|
|
}
|
|
if (this.debug) {
|
|
return console.log.apply(console, args);
|
|
}
|
|
};
|
|
|
|
return EventEmitter;
|
|
|
|
})();
|
|
|
|
// |