@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) { return new Promise(resolve => { on(mm, message, function onMessage({ data }) { off(mm, message, onMessage); resolve(data); }); }); } 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) { const done = once(mm, message + REQUEST_DONE_SUFFIX); emit(mm, message, data); return done; } exports.request = request; PK