e10s.js (3184B)
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/. */ 4 5 "use strict"; 6 7 // The prefix used for RDM messages in content. 8 // see: devtools/client/responsive/browser/content.js 9 const MESSAGE_PREFIX = "ResponsiveMode:"; 10 const REQUEST_DONE_SUFFIX = ":Done"; 11 12 /** 13 * Registers a message `listener` that is called every time messages of 14 * specified `message` is emitted on the given message manager. 15 * 16 * @param {nsIMessageListenerManager} mm 17 * The Message Manager 18 * @param {string} message 19 * The message. It will be prefixed with the constant `MESSAGE_PREFIX` 20 * @param {Function} listener 21 * The listener function that processes the message. 22 */ 23 function on(mm, message, listener) { 24 mm.addMessageListener(MESSAGE_PREFIX + message, listener); 25 } 26 exports.on = on; 27 28 /** 29 * Removes a message `listener` for the specified `message` on the given 30 * message manager. 31 * 32 * @param {nsIMessageListenerManager} mm 33 * The Message Manager 34 * @param {string} message 35 * The message. It will be prefixed with the constant `MESSAGE_PREFIX` 36 * @param {Function} listener 37 * The listener function that processes the message. 38 */ 39 function off(mm, message, listener) { 40 mm.removeMessageListener(MESSAGE_PREFIX + message, listener); 41 } 42 exports.off = off; 43 44 /** 45 * Resolves a promise the next time the specified `message` is sent over the 46 * given message manager. 47 * 48 * @param {nsIMessageListenerManager} mm 49 * The Message Manager 50 * @param {string} message 51 * The message. It will be prefixed with the constant `MESSAGE_PREFIX` 52 * @returns {Promise} 53 * A promise that is resolved when the given message is emitted. 54 */ 55 function once(mm, message) { 56 return new Promise(resolve => { 57 on(mm, message, function onMessage({ data }) { 58 off(mm, message, onMessage); 59 resolve(data); 60 }); 61 }); 62 } 63 exports.once = once; 64 65 /** 66 * Asynchronously emit a `message` to the listeners of the given message 67 * manager. 68 * 69 * @param {nsIMessageListenerManager} mm 70 * The Message Manager 71 * @param {string} message 72 * The message. It will be prefixed with the constant `MESSAGE_PREFIX`. 73 * @param {object} data 74 * A JSON object containing data to be delivered to the listeners. 75 */ 76 function emit(mm, message, data) { 77 mm.sendAsyncMessage(MESSAGE_PREFIX + message, data); 78 } 79 exports.emit = emit; 80 81 /** 82 * Asynchronously send a "request" over the given message manager, and returns 83 * a promise that is resolved when the request is complete. 84 * 85 * @param {nsIMessageListenerManager} mm 86 * The Message Manager 87 * @param {string} message 88 * The message. It will be prefixed with the constant `MESSAGE_PREFIX`, and 89 * also suffixed with `REQUEST_DONE_SUFFIX` for the reply. 90 * @param {object} data 91 * A JSON object containing data to be delivered to the listeners. 92 * @returns {Promise} 93 * A promise that is resolved when the request is done. 94 */ 95 function request(mm, message, data) { 96 const done = once(mm, message + REQUEST_DONE_SUFFIX); 97 98 emit(mm, message, data); 99 100 return done; 101 } 102 exports.request = request;