addon-sdk/source/lib/sdk/event/chrome.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 module.metadata = {
michael@0 8 "stability": "unstable"
michael@0 9 };
michael@0 10
michael@0 11 const { Cc, Ci, Cr } = require("chrome");
michael@0 12 const { emit, on, off } = require("./core");
michael@0 13 const { addObserver } = Cc['@mozilla.org/observer-service;1'].
michael@0 14 getService(Ci.nsIObserverService);
michael@0 15
michael@0 16 // Simple class that can be used to instantiate event channel that
michael@0 17 // implements `nsIObserver` interface. It's will is used by `observe`
michael@0 18 // function as observer + event target. It basically proxies observer
michael@0 19 // notifications as to it's registered listeners.
michael@0 20 function ObserverChannel() {}
michael@0 21 Object.freeze(Object.defineProperties(ObserverChannel.prototype, {
michael@0 22 QueryInterface: {
michael@0 23 value: function(iid) {
michael@0 24 if (!iid.equals(Ci.nsIObserver) &&
michael@0 25 !iid.equals(Ci.nsISupportsWeakReference) &&
michael@0 26 !iid.equals(Ci.nsISupports))
michael@0 27 throw Cr.NS_ERROR_NO_INTERFACE;
michael@0 28 return this;
michael@0 29 }
michael@0 30 },
michael@0 31 observe: {
michael@0 32 value: function(subject, topic, data) {
michael@0 33 emit(this, "data", {
michael@0 34 type: topic,
michael@0 35 target: subject,
michael@0 36 data: data
michael@0 37 });
michael@0 38 }
michael@0 39 }
michael@0 40 }));
michael@0 41
michael@0 42 function observe(topic) {
michael@0 43 let observerChannel = new ObserverChannel();
michael@0 44
michael@0 45 // Note: `nsIObserverService` will not hold a weak reference to a
michael@0 46 // observerChannel (since third argument is `true`). There for if it
michael@0 47 // will be GC-ed with all it's event listeners once no other references
michael@0 48 // will be held.
michael@0 49 addObserver(observerChannel, topic, true);
michael@0 50
michael@0 51 return observerChannel;
michael@0 52 }
michael@0 53
michael@0 54 exports.observe = observe;

mercurial