michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * michael@0: * Some helpers for common operations in the App Manager: michael@0: * michael@0: * . mergeStores: merge several store into one. michael@0: * . l10n: resolves strings from app-manager.properties. michael@0: * michael@0: */ michael@0: michael@0: let Utils = (function() { michael@0: const Cu = Components.utils; michael@0: const {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); michael@0: const {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {}); michael@0: const {require} = devtools; michael@0: const EventEmitter = require("devtools/toolkit/event-emitter"); michael@0: michael@0: michael@0: function _createSetEventForwarder(key, finalStore) { michael@0: return function(event, path, value) { michael@0: finalStore.emit("set", [key].concat(path), value); michael@0: }; michael@0: } michael@0: michael@0: function mergeStores(stores) { michael@0: let finalStore = {object:{}}; michael@0: michael@0: EventEmitter.decorate(finalStore); michael@0: michael@0: let setEventForwarders = {}; michael@0: michael@0: for (let key in stores) { michael@0: let store = stores[key]; michael@0: finalStore.object[key] = store.object; michael@0: setEventForwarders[key] = _createSetEventForwarder(key, finalStore); michael@0: store.on("set", setEventForwarders[key]); michael@0: } michael@0: michael@0: finalStore.destroy = () => { michael@0: for (let key in stores) { michael@0: let store = stores[key]; michael@0: store.off("set", setEventForwarders[key]); michael@0: if (store.destroy) { michael@0: store.destroy(); michael@0: } michael@0: } michael@0: }; michael@0: michael@0: return finalStore; michael@0: } michael@0: michael@0: michael@0: let strings = Services.strings.createBundle("chrome://browser/locale/devtools/app-manager.properties"); michael@0: michael@0: function l10n (property, args = []) { michael@0: if (args && args.length > 0) { michael@0: return strings.formatStringFromName(property, args, args.length); michael@0: } else { michael@0: return strings.GetStringFromName(property); michael@0: } michael@0: } michael@0: michael@0: return { michael@0: mergeStores: mergeStores, michael@0: l10n: l10n michael@0: } michael@0: })();