remote/Observer.jsm
author Bobby Holley <bobbyholley@gmail.com>
Tue, 09 Apr 2019 18:40:44 +0000
changeset 468618 ac23ad5ef0c18d7bf5b55fd9d07cfd1c0da33033
parent 463196 d7e26a1b6ef71bb156732495eacaa7d97b2dc4f2
child 481406 07ce31daca134d974d5d1ee2c86f6fdc307d9aa4
permissions -rw-r--r--
Bug 1542862 - Eagerly fail when building Android on OSX with rust 1.33 and point to the bug/workaround. r=glandium Differential Revision: https://phabricator.services.mozilla.com/D26589

/* 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 = ["Observer"];

const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

class Observer {
  static observe(type, observer) {
    Services.obs.addObserver(observer, type);
  }

  static unobserve(type, observer) {
    Services.obs.removeObserver(observer, type);
  }

  static once(type, observer = () => {}) {
    return new Promise(resolve => {
      const wrappedObserver = (first, ...rest) => {
        Observer.unobserve(type, wrappedObserver);
        observer.call(first, ...rest);
        resolve();
      };
      Observer.observe(type, wrappedObserver);
    });
  }
}