Files
palemoon27/toolkit/devtools/event-emitter.js
T
roytam1 bfa8452350 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1084525 - Part 2: Refactor expectState from memory-bridge.js to common.js r=fitzgen (241c8d7cd)
- Bug 1084525 - Part 3: Add attach and detach methods to PromisesActor r=fitzgen (df0dea46d)
- Avoid infinite recursion when lazy-loading devtools modules and stepping into promise code in chrome debugging (bug 1149910). r=jlongster,fitzgen (a8e372285)
- Bug 1136341 - fix source actors representing inline scripts in XUL files so breakpoints work r=ejpbruel (904825023)
- Bug 1158498 - Can't set a JS breakpoint in inline JS;r=jlong (bbf9200ed)
- Bug 1166844 - treat eval'ed code from the console as unnamed eval sources r=ejpbruel (98d19de4e)
- Bug 1166852 - handle named eval sources from the console correctly r=ejpbruel (447b21b07)
- Bug 1163024 - Part 1: Stylesheet actors should use the common DevToolsUtils.fetch(). r=bgrins (2871b6295)
- Bug 1148893 - Miscellaneous fixes to multiple style editor tests. r=bgrins (073392349)
- Bug 1150005 - Don't wait for "editor-selected" event in browser_styleeditor_fetch-from-cache.js as it may have already been emitted. r=bgrins (c68786780)
- Bug 1163024 - Part 2: Ignore network requests from chrome code in a content toolbox. r=ochameau (6fd22227c)
- fix misspatch? (f1c0c1012)
- Bug 1161072 - Destroy inspector actor on disconnect. r=pbrosset (62bc54dfe)
- Bug 1168088 - Remove SpiderMonkey-specific syntax from toolkit/devtools. r=pbrosset (1f177ae4b)
- Bug 1134568 - Implement a preset gallery for CubicBezierWidget incl. 8 preset functions for categories: ease-in, ease-out, and ease-in-out. r=pbrosset (3ae37e757)
- Bug 1055181 - Mochitests for the CSS Filter tooltip; r=pbrosset (981c13b02)
- Bug 1149671 - Use alt/na/shift to step 0.1/1/10 at a time. r=pbrosset (fd70b5f1b)
- Bug 1126457 - Implement a worker utility to easily offload tasks into a worker. r=bgrins (2681ad8a1)
- fix misspatch (7afd26b57)
2021-04-17 08:04:03 +08:00

208 lines
6.2 KiB
JavaScript

/* 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/. */
/**
* EventEmitter.
*/
(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", {});
this.isWorker = false;
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) {
this.EventEmitter = function EventEmitter() {};
module.exports = EventEmitter;
const { Cu, components } = require("chrome");
const Services = require("Services");
const promise = require("promise");
/**
* Decorate an object with event emitter functionality.
*
* @param Object aObjectToDecorate
* Bind all public methods of EventEmitter to
* the aObjectToDecorate object.
*/
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);
};
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
* (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
*/
once: function EventEmitter_once(aEvent, aListener) {
let deferred = promise.defer();
let handler = (aEvent, aFirstArg, ...aRest) => {
this.off(aEvent, handler);
if (aListener) {
aListener.apply(null, [aEvent, aFirstArg, ...aRest]);
}
deferred.resolve(aFirstArg);
};
handler._originalListener = aListener;
this.on(aEvent, handler);
return deferred.promise;
},
/**
* 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);
if (listeners) {
this._eventEmitterListeners.set(aEvent, listeners.filter(l => {
return l !== aListener && l._originalListener !== aListener;
}));
}
},
/**
* Emit an event. All arguments to this method will
* be sent to listener functions.
*/
emit: function EventEmitter_emit(aEvent) {
this.logEvent(aEvent, arguments);
if (!this._eventEmitterListeners || !this._eventEmitterListeners.has(aEvent)) {
return;
}
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(l => l === listener)) {
try {
listener.apply(null, arguments);
}
catch (ex) {
// Prevent a bad listener from interfering with the others.
let msg = ex + ": " + ex.stack;
Cu.reportError(msg);
dump(msg + "\n");
}
}
}
},
logEvent: function(aEvent, args) {
let logging = isWorker ? true : Services.prefs.getBoolPref("devtools.dump.emit");
if (logging) {
let caller, func, path;
if (!isWorker) {
caller = components.stack.caller.caller;
func = caller.name;
let file = caller.filename;
if (file.includes(" -> ")) {
file = caller.filename.split(/ -> /)[1];
}
path = file + ":" + caller.lineNumber;
}
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);
}
},
};
});