diff options
Diffstat (limited to 'devtools/client/responsive.html/utils')
-rw-r--r-- | devtools/client/responsive.html/utils/e10s.js | 103 | ||||
-rw-r--r-- | devtools/client/responsive.html/utils/enum.js | 21 | ||||
-rw-r--r-- | devtools/client/responsive.html/utils/l10n.js | 16 | ||||
-rw-r--r-- | devtools/client/responsive.html/utils/message.js | 38 | ||||
-rw-r--r-- | devtools/client/responsive.html/utils/moz.build | 12 |
5 files changed, 190 insertions, 0 deletions
diff --git a/devtools/client/responsive.html/utils/e10s.js b/devtools/client/responsive.html/utils/e10s.js new file mode 100644 index 000000000..f45add6b0 --- /dev/null +++ b/devtools/client/responsive.html/utils/e10s.js @@ -0,0 +1,103 @@ +/* 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"; + +const { defer } = require("promise"); + +// The prefix used for RDM messages in content. +// see: devtools/client/responsivedesign/responsivedesign-child.js +const MESSAGE_PREFIX = "ResponsiveMode:"; +const REQUEST_DONE_SUFFIX = ":Done"; + +/** + * Registers a message `listener` that is called every time messages of + * specified `message` is emitted on the given message manager. + * @param {nsIMessageListenerManager} mm + * The Message Manager + * @param {String} message + * The message. It will be prefixed with the constant `MESSAGE_PREFIX` + * @param {Function} listener + * The listener function that processes the message. + */ +function on(mm, message, listener) { + mm.addMessageListener(MESSAGE_PREFIX + message, listener); +} +exports.on = on; + +/** + * Removes a message `listener` for the specified `message` on the given + * message manager. + * @param {nsIMessageListenerManager} mm + * The Message Manager + * @param {String} message + * The message. It will be prefixed with the constant `MESSAGE_PREFIX` + * @param {Function} listener + * The listener function that processes the message. + */ +function off(mm, message, listener) { + mm.removeMessageListener(MESSAGE_PREFIX + message, listener); +} +exports.off = off; + +/** + * Resolves a promise the next time the specified `message` is sent over the + * given message manager. + * @param {nsIMessageListenerManager} mm + * The Message Manager + * @param {String} message + * The message. It will be prefixed with the constant `MESSAGE_PREFIX` + * @returns {Promise} + * A promise that is resolved when the given message is emitted. + */ +function once(mm, message) { + let { resolve, promise } = defer(); + + on(mm, message, function onMessage({data}) { + off(mm, message, onMessage); + resolve(data); + }); + + return promise; +} +exports.once = once; + +/** + * Asynchronously emit a `message` to the listeners of the given message + * manager. + * + * @param {nsIMessageListenerManager} mm + * The Message Manager + * @param {String} message + * The message. It will be prefixed with the constant `MESSAGE_PREFIX`. + * @param {Object} data + * A JSON object containing data to be delivered to the listeners. + */ +function emit(mm, message, data) { + mm.sendAsyncMessage(MESSAGE_PREFIX + message, data); +} +exports.emit = emit; + +/** + * Asynchronously send a "request" over the given message manager, and returns + * a promise that is resolved when the request is complete. + * + * @param {nsIMessageListenerManager} mm + * The Message Manager + * @param {String} message + * The message. It will be prefixed with the constant `MESSAGE_PREFIX`, and + * also suffixed with `REQUEST_DONE_SUFFIX` for the reply. + * @param {Object} data + * A JSON object containing data to be delivered to the listeners. + * @returns {Promise} + * A promise that is resolved when the request is done. + */ +function request(mm, message, data) { + let done = once(mm, message + REQUEST_DONE_SUFFIX); + + emit(mm, message, data); + + return done; +} +exports.request = request; diff --git a/devtools/client/responsive.html/utils/enum.js b/devtools/client/responsive.html/utils/enum.js new file mode 100644 index 000000000..cab8ff1ce --- /dev/null +++ b/devtools/client/responsive.html/utils/enum.js @@ -0,0 +1,21 @@ +/* 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"; + +module.exports = { + + /** + * Create a simple enum-like object with keys mirrored to values from an array. + * This makes comparison to a specfic value simpler without having to repeat and + * mis-type the value. + */ + createEnum(array, target = {}) { + for (let key of array) { + target[key] = key; + } + return target; + } + +}; diff --git a/devtools/client/responsive.html/utils/l10n.js b/devtools/client/responsive.html/utils/l10n.js new file mode 100644 index 000000000..515182462 --- /dev/null +++ b/devtools/client/responsive.html/utils/l10n.js @@ -0,0 +1,16 @@ +/* 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"; + +const { LocalizationHelper } = require("devtools/shared/l10n"); +const STRINGS_URI = "devtools/client/locales/responsive.properties"; +const L10N = new LocalizationHelper(STRINGS_URI); + +module.exports = { + getStr: (...args) => L10N.getStr(...args), + getFormatStr: (...args) => L10N.getFormatStr(...args), + getFormatStrWithNumbers: (...args) => L10N.getFormatStrWithNumbers(...args), + numberWithDecimals: (...args) => L10N.numberWithDecimals(...args), +}; diff --git a/devtools/client/responsive.html/utils/message.js b/devtools/client/responsive.html/utils/message.js new file mode 100644 index 000000000..d5c5b012f --- /dev/null +++ b/devtools/client/responsive.html/utils/message.js @@ -0,0 +1,38 @@ +/* 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"; + +const promise = require("promise"); + +const REQUEST_DONE_SUFFIX = ":done"; + +function wait(win, type) { + let deferred = promise.defer(); + + let onMessage = event => { + if (event.data.type !== type) { + return; + } + win.removeEventListener("message", onMessage); + deferred.resolve(); + }; + win.addEventListener("message", onMessage); + + return deferred.promise; +} + +function post(win, type) { + win.postMessage({ type }, "*"); +} + +function request(win, type) { + let done = wait(win, type + REQUEST_DONE_SUFFIX); + post(win, type); + return done; +} + +exports.wait = wait; +exports.post = post; +exports.request = request; diff --git a/devtools/client/responsive.html/utils/moz.build b/devtools/client/responsive.html/utils/moz.build new file mode 100644 index 000000000..a716eae0c --- /dev/null +++ b/devtools/client/responsive.html/utils/moz.build @@ -0,0 +1,12 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +DevToolsModules( + 'e10s.js', + 'enum.js', + 'l10n.js', + 'message.js', +) |