Bug 1568151 - Replace `target.getInspector()` by `target.getFront("inspector")`....
[gecko.git] / remote / Observer.jsm
blob32d3c2dbd7df7a51e35588686e5bdf16240cb2dd
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 var EXPORTED_SYMBOLS = ["Observer"];
9 const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
11 class Observer {
12   static observe(type, observer) {
13     Services.obs.addObserver(observer, type);
14   }
16   static unobserve(type, observer) {
17     Services.obs.removeObserver(observer, type);
18   }
20   static once(type, observer = () => {}) {
21     return new Promise(resolve => {
22       const wrappedObserver = (first, ...rest) => {
23         Observer.unobserve(type, wrappedObserver);
24         observer.call(first, ...rest);
25         resolve();
26       };
27       Observer.observe(type, wrappedObserver);
28     });
29   }