Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

PostMessageTransportBackend.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import Postis from 'postis';
  2. /**
  3. * The default options for postis.
  4. *
  5. * @type {Object}
  6. */
  7. const DEFAULT_POSTIS_OPTIONS = {
  8. window: window.opener || window.parent
  9. };
  10. /**
  11. * The list of methods of incomming postis messages that we have to support for
  12. * backward compatability for the users that are directly sending messages to
  13. * Jitsi Meet (without using external_api.js)
  14. *
  15. * @type {string[]}
  16. */
  17. const LEGACY_INCOMING_METHODS = [
  18. 'avatar-url',
  19. 'display-name',
  20. 'email',
  21. 'toggle-audio',
  22. 'toggle-chat',
  23. 'toggle-contact-list',
  24. 'toggle-film-strip',
  25. 'toggle-share-screen',
  26. 'toggle-video',
  27. 'video-hangup'
  28. ];
  29. /**
  30. * The list of methods of outgoing postis messages that we have to support for
  31. * backward compatability for the users that are directly listening to the
  32. * postis messages send by Jitsi Meet(without using external_api.js).
  33. *
  34. * @type {string[]}
  35. */
  36. const LEGACY_OUTGOING_METHODS = [
  37. 'display-name-change',
  38. 'incoming-message',
  39. 'outgoing-message',
  40. 'participant-joined',
  41. 'participant-left',
  42. 'video-conference-joined',
  43. 'video-conference-left',
  44. 'video-ready-to-close'
  45. ];
  46. /**
  47. * The postis method used for all messages.
  48. *
  49. * @type {string}
  50. */
  51. const POSTIS_METHOD_NAME = 'data';
  52. /**
  53. * Implements message transport using the postMessage API.
  54. */
  55. export default class PostMessageTransportBackend {
  56. /**
  57. * Creates new PostMessageTransportBackend instance.
  58. *
  59. * @param {Object} options - Optional parameters for configuration of the
  60. * transport.
  61. */
  62. constructor({ enableLegacyFormat, postisOptions } = {}) {
  63. this.postis = Postis({
  64. ...DEFAULT_POSTIS_OPTIONS,
  65. ...postisOptions
  66. });
  67. this._enableLegacyFormat = enableLegacyFormat;
  68. if (!this._enableLegacyFormat) {
  69. // backward compatability
  70. LEGACY_INCOMING_METHODS.forEach(method =>
  71. this.postis.listen(
  72. method,
  73. params => this._legacyDataReceiveCallback(method, params)));
  74. }
  75. this._receiveCallback = () => {
  76. // Do nothing until a callback is set by the consumer of
  77. // PostMessageTransportBackend via setReceiveCallback.
  78. };
  79. this.postis.listen(
  80. POSTIS_METHOD_NAME,
  81. data => this._receiveCallback(data));
  82. }
  83. /**
  84. * Handles incomming legacy postis data.
  85. *
  86. * @param {string} method - The method property from postis data object.
  87. * @param {Any} params - The params property from postis data object.
  88. * @returns {void}
  89. */
  90. _legacyDataReceiveCallback(method, params = {}) {
  91. this._receiveCallback({
  92. data: {
  93. name: method,
  94. data: params
  95. }
  96. });
  97. }
  98. /**
  99. * Sends the passed data via postis using the old format.
  100. *
  101. * @param {Object} data - The data to be sent.
  102. * @returns {void}
  103. */
  104. _sendLegacyData({ data, name }) {
  105. if (name && LEGACY_OUTGOING_METHODS.indexOf(name) !== -1) {
  106. this.postis.send({
  107. method: name,
  108. params: data
  109. });
  110. }
  111. }
  112. /**
  113. * Disposes the allocated resources.
  114. *
  115. * @returns {void}
  116. */
  117. dispose() {
  118. this.postis.destroy();
  119. }
  120. /**
  121. * Sends the passed data.
  122. *
  123. * @param {Object} data - The data to be sent.
  124. * @returns {void}
  125. */
  126. send(data) {
  127. this.postis.send({
  128. method: POSTIS_METHOD_NAME,
  129. params: data
  130. });
  131. if (!this._enableLegacyFormat) {
  132. // For the legacy use case we don't need any new fields defined in
  133. // Transport class. That's why we are passing only the original
  134. // object passed by the consumer of the Transport class which is
  135. // data.data.
  136. this._sendLegacyData(data.data);
  137. }
  138. }
  139. /**
  140. * Sets the callback for receiving data.
  141. *
  142. * @param {Function} callback - The new callback.
  143. * @returns {void}
  144. */
  145. setReceiveCallback(callback) {
  146. this._receiveCallback = callback;
  147. }
  148. }