mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-20 16:55:40 +00:00
da5a13b347
The remote agent currently uses "method" interchangably for the full method string as extracted from JSON input as well as for the function part following the first dot after the method has been split. To avoid namespace clashes, this patch makes a distinction between method, being the input JSON field; the first substring prior to the dot being the domain; and the rest that follows being called the command: method = "<domain>.<command>" This naming seems to be supported by chrome-remote-interface: https://github.com/cyrus-and/chrome-remote-interface/blob/master/lib/api.js#L32 Differential Revision: https://phabricator.services.mozilla.com/D25946 --HG-- extra : moz-landing-system : lando
52 lines
1.2 KiB
JavaScript
52 lines
1.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/. */
|
|
|
|
"use strict";
|
|
|
|
var EXPORTED_SYMBOLS = ["Domain"];
|
|
|
|
class Domain {
|
|
constructor(session) {
|
|
this.session = session;
|
|
this.name = this.constructor.name;
|
|
|
|
this.eventListeners_ = new Set();
|
|
}
|
|
|
|
destructor() {}
|
|
|
|
emit(eventName, params = {}) {
|
|
for (const listener of this.eventListeners_) {
|
|
try {
|
|
if (isEventHandler(listener)) {
|
|
listener.onEvent(eventName, params);
|
|
} else {
|
|
listener.call(this, eventName, params);
|
|
}
|
|
} catch (e) {
|
|
Cu.reportError(e);
|
|
}
|
|
}
|
|
}
|
|
|
|
addEventListener(listener) {
|
|
if (typeof listener != "function" && !isEventHandler(listener)) {
|
|
throw new TypeError();
|
|
}
|
|
this.eventListeners_.add(listener);
|
|
}
|
|
|
|
// static
|
|
|
|
static implements(command) {
|
|
return command && typeof this.prototype[command] == "function";
|
|
}
|
|
}
|
|
|
|
function isEventHandler(listener) {
|
|
return listener &&
|
|
"onEvent" in listener &&
|
|
typeof listener.onEvent == "function";
|
|
}
|