remote/domains/Domain.jsm
author Victor Porof <vporof@mozilla.com>
Fri, 17 May 2019 15:50:29 +0000
changeset 474353 edafe8d51cbf5f1585db25785ef204936777392e
parent 467983 28eb8a6b33f63322188eb1ca0e4bcb3b3fb43432
child 481406 07ce31daca134d974d5d1ee2c86f6fdc307d9aa4
permissions -rw-r--r--
Bug 1551218 - Part 1: Enable running top-level eslint for the devtools debugger, r=Standard8,jlast Differential Revision: https://phabricator.services.mozilla.com/D30926

/* 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";
}