e10s.js 3.2 KB

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