remote/Observer.jsm
author Anne van Kesteren <annevk@annevk.nl>
Tue, 05 Mar 2019 12:18:39 +0000
changeset 464583 8566ae4d152605453ebe6715ec4f2f8af60a95d3
parent 463196 d7e26a1b6ef71bb156732495eacaa7d97b2dc4f2
permissions -rw-r--r--
Bug 1529726 [wpt PR 15414] - HTML: window.length and named access, a=testonly Automatic update from web-platform-tests HTML: window.length and named access For https://github.com/whatwg/html/pull/4368. -- wpt-commits: 3a43f99a56a4c016e3f1cda41330f1a2c0e780ff wpt-pr: 15414

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