2012-11-30 00:07:59 -08:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-09-11 04:25:52 -07:00
|
|
|
|
2012-11-30 00:07:59 -08:00
|
|
|
/**
|
|
|
|
* EventEmitter.
|
2012-12-13 23:05:00 -08:00
|
|
|
*/
|
2013-04-11 13:59:08 -07:00
|
|
|
|
2014-06-10 11:36:38 -07:00
|
|
|
(function (factory) { // Module boilerplate
|
|
|
|
if (this.module && module.id.indexOf("event-emitter") >= 0) { // require
|
|
|
|
factory.call(this, require, exports, module);
|
|
|
|
} else { // Cu.import
|
|
|
|
const Cu = Components.utils;
|
|
|
|
const { devtools } = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
|
2014-06-17 12:51:43 -07:00
|
|
|
this.isWorker = false;
|
2014-06-10 11:36:38 -07:00
|
|
|
this.promise = Cu.import("resource://gre/modules/Promise.jsm", {}).Promise;
|
|
|
|
factory.call(this, devtools.require, this, { exports: this });
|
|
|
|
this.EXPORTED_SYMBOLS = ["EventEmitter"];
|
|
|
|
}
|
|
|
|
}).call(this, function (require, exports, module) {
|
2012-12-13 23:05:00 -08:00
|
|
|
|
2014-06-10 11:36:38 -07:00
|
|
|
this.EventEmitter = function EventEmitter() {};
|
|
|
|
module.exports = EventEmitter;
|
2013-04-11 13:59:08 -07:00
|
|
|
|
2014-06-10 11:36:38 -07:00
|
|
|
const { Cu, components } = require("chrome");
|
|
|
|
const Services = require("Services");
|
2014-07-07 03:07:00 -07:00
|
|
|
const promise = require("promise");
|
2014-01-15 02:33:18 -08:00
|
|
|
|
2012-12-13 23:05:00 -08:00
|
|
|
/**
|
|
|
|
* Decorate an object with event emitter functionality.
|
2012-11-30 00:07:59 -08:00
|
|
|
*
|
2012-12-13 23:05:00 -08:00
|
|
|
* @param Object aObjectToDecorate
|
|
|
|
* Bind all public methods of EventEmitter to
|
|
|
|
* the aObjectToDecorate object.
|
2012-11-30 00:07:59 -08:00
|
|
|
*/
|
2012-12-13 23:05:00 -08:00
|
|
|
EventEmitter.decorate = function EventEmitter_decorate (aObjectToDecorate) {
|
|
|
|
let emitter = new EventEmitter();
|
|
|
|
aObjectToDecorate.on = emitter.on.bind(emitter);
|
|
|
|
aObjectToDecorate.off = emitter.off.bind(emitter);
|
|
|
|
aObjectToDecorate.once = emitter.once.bind(emitter);
|
|
|
|
aObjectToDecorate.emit = emitter.emit.bind(emitter);
|
|
|
|
};
|
2012-09-11 04:25:52 -07:00
|
|
|
|
|
|
|
EventEmitter.prototype = {
|
|
|
|
/**
|
|
|
|
* Connect a listener.
|
|
|
|
*
|
|
|
|
* @param string aEvent
|
|
|
|
* The event name to which we're connecting.
|
|
|
|
* @param function aListener
|
|
|
|
* Called when the event is fired.
|
|
|
|
*/
|
|
|
|
on: function EventEmitter_on(aEvent, aListener) {
|
|
|
|
if (!this._eventEmitterListeners)
|
|
|
|
this._eventEmitterListeners = new Map();
|
|
|
|
if (!this._eventEmitterListeners.has(aEvent)) {
|
|
|
|
this._eventEmitterListeners.set(aEvent, []);
|
|
|
|
}
|
|
|
|
this._eventEmitterListeners.get(aEvent).push(aListener);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen for the next time an event is fired.
|
|
|
|
*
|
|
|
|
* @param string aEvent
|
|
|
|
* The event name to which we're connecting.
|
|
|
|
* @param function aListener
|
2014-01-15 02:33:18 -08:00
|
|
|
* (Optional) Called when the event is fired. Will be called at most
|
|
|
|
* one time.
|
|
|
|
* @return promise
|
|
|
|
* A promise which is resolved when the event next happens. The
|
|
|
|
* resolution value of the promise is the first event argument. If
|
|
|
|
* you need access to second or subsequent event arguments (it's rare
|
|
|
|
* that this is needed) then use aListener
|
2012-09-11 04:25:52 -07:00
|
|
|
*/
|
|
|
|
once: function EventEmitter_once(aEvent, aListener) {
|
2014-01-15 02:33:18 -08:00
|
|
|
let deferred = promise.defer();
|
|
|
|
|
2014-06-14 03:49:00 -07:00
|
|
|
let handler = (aEvent, aFirstArg) => {
|
2012-09-11 04:25:52 -07:00
|
|
|
this.off(aEvent, handler);
|
2014-01-15 02:33:18 -08:00
|
|
|
if (aListener) {
|
|
|
|
aListener.apply(null, arguments);
|
|
|
|
}
|
|
|
|
deferred.resolve(aFirstArg);
|
2014-06-14 03:49:00 -07:00
|
|
|
};
|
2014-01-15 02:33:18 -08:00
|
|
|
|
2013-09-19 17:36:13 -07:00
|
|
|
handler._originalListener = aListener;
|
2012-09-11 04:25:52 -07:00
|
|
|
this.on(aEvent, handler);
|
2014-01-15 02:33:18 -08:00
|
|
|
|
|
|
|
return deferred.promise;
|
2012-09-11 04:25:52 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a previously-registered event listener. Works for events
|
|
|
|
* registered with either on or once.
|
|
|
|
*
|
|
|
|
* @param string aEvent
|
|
|
|
* The event name whose listener we're disconnecting.
|
|
|
|
* @param function aListener
|
|
|
|
* The listener to remove.
|
|
|
|
*/
|
|
|
|
off: function EventEmitter_off(aEvent, aListener) {
|
|
|
|
if (!this._eventEmitterListeners)
|
|
|
|
return;
|
|
|
|
let listeners = this._eventEmitterListeners.get(aEvent);
|
2012-11-30 00:07:59 -08:00
|
|
|
if (listeners) {
|
2013-09-19 17:36:13 -07:00
|
|
|
this._eventEmitterListeners.set(aEvent, listeners.filter(l => {
|
|
|
|
return l !== aListener && l._originalListener !== aListener;
|
|
|
|
}));
|
2012-11-30 00:07:59 -08:00
|
|
|
}
|
2012-09-11 04:25:52 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Emit an event. All arguments to this method will
|
2014-07-28 06:01:00 -07:00
|
|
|
* be sent to listener functions.
|
2012-09-11 04:25:52 -07:00
|
|
|
*/
|
|
|
|
emit: function EventEmitter_emit(aEvent) {
|
2014-03-06 01:37:59 -08:00
|
|
|
this.logEvent(aEvent, arguments);
|
|
|
|
|
|
|
|
if (!this._eventEmitterListeners || !this._eventEmitterListeners.has(aEvent)) {
|
2012-09-11 04:25:52 -07:00
|
|
|
return;
|
2014-03-06 01:37:59 -08:00
|
|
|
}
|
2012-09-11 04:25:52 -07:00
|
|
|
|
|
|
|
let originalListeners = this._eventEmitterListeners.get(aEvent);
|
|
|
|
for (let listener of this._eventEmitterListeners.get(aEvent)) {
|
|
|
|
// If the object was destroyed during event emission, stop
|
|
|
|
// emitting.
|
|
|
|
if (!this._eventEmitterListeners) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If listeners were removed during emission, make sure the
|
|
|
|
// event handler we're going to fire wasn't removed.
|
|
|
|
if (originalListeners === this._eventEmitterListeners.get(aEvent) ||
|
|
|
|
this._eventEmitterListeners.get(aEvent).some(function(l) l === listener)) {
|
2012-12-13 05:03:55 -08:00
|
|
|
try {
|
|
|
|
listener.apply(null, arguments);
|
|
|
|
}
|
|
|
|
catch (ex) {
|
|
|
|
// Prevent a bad listener from interfering with the others.
|
|
|
|
let msg = ex + ": " + ex.stack;
|
2013-04-11 13:59:08 -07:00
|
|
|
Cu.reportError(msg);
|
2012-12-13 05:03:55 -08:00
|
|
|
dump(msg + "\n");
|
|
|
|
}
|
2012-09-11 04:25:52 -07:00
|
|
|
}
|
|
|
|
}
|
2014-03-06 01:37:59 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
logEvent: function(aEvent, args) {
|
2014-06-17 12:51:43 -07:00
|
|
|
let logging = isWorker ? true : Services.prefs.getBoolPref("devtools.dump.emit");
|
2014-03-06 01:37:59 -08:00
|
|
|
|
|
|
|
if (logging) {
|
2014-06-17 12:51:43 -07:00
|
|
|
let caller, func, path;
|
|
|
|
if (!isWorker) {
|
|
|
|
caller = components.stack.caller.caller;
|
|
|
|
func = caller.name;
|
2014-07-04 02:47:45 -07:00
|
|
|
let file = caller.filename;
|
|
|
|
if (file.contains(" -> ")) {
|
|
|
|
file = caller.filename.split(/ -> /)[1];
|
|
|
|
}
|
|
|
|
path = file + ":" + caller.lineNumber;
|
2014-06-17 12:51:43 -07:00
|
|
|
}
|
2014-03-06 01:37:59 -08:00
|
|
|
|
|
|
|
let argOut = "(";
|
|
|
|
if (args.length === 1) {
|
|
|
|
argOut += aEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
let out = "EMITTING: ";
|
|
|
|
|
|
|
|
// We need this try / catch to prevent any dead object errors.
|
|
|
|
try {
|
|
|
|
for (let i = 1; i < args.length; i++) {
|
|
|
|
if (i === 1) {
|
|
|
|
argOut = "(" + aEvent + ", ";
|
|
|
|
} else {
|
|
|
|
argOut += ", ";
|
|
|
|
}
|
|
|
|
|
|
|
|
let arg = args[i];
|
|
|
|
argOut += arg;
|
|
|
|
|
|
|
|
if (arg && arg.nodeName) {
|
|
|
|
argOut += " (" + arg.nodeName;
|
|
|
|
if (arg.id) {
|
|
|
|
argOut += "#" + arg.id;
|
|
|
|
}
|
|
|
|
if (arg.className) {
|
|
|
|
argOut += "." + arg.className;
|
|
|
|
}
|
|
|
|
argOut += ")";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
// Object is dead so the toolbox is most likely shutting down,
|
|
|
|
// do nothing.
|
|
|
|
}
|
|
|
|
|
|
|
|
argOut += ")";
|
|
|
|
out += "emit" + argOut + " from " + func + "() -> " + path + "\n";
|
|
|
|
|
|
|
|
dump(out);
|
|
|
|
}
|
|
|
|
},
|
2012-12-13 23:05:00 -08:00
|
|
|
};
|
2014-06-10 11:36:38 -07:00
|
|
|
|
|
|
|
});
|