您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* @flow */
  2. import { NativeModules } from 'react-native';
  3. import {
  4. CONFERENCE_FAILED,
  5. CONFERENCE_JOINED,
  6. CONFERENCE_LEFT,
  7. CONFERENCE_WILL_JOIN,
  8. CONFERENCE_WILL_LEAVE,
  9. JITSI_CONFERENCE_URL_KEY
  10. } from '../../base/conference';
  11. import { LOAD_CONFIG_ERROR } from '../../base/config';
  12. import { MiddlewareRegistry } from '../../base/redux';
  13. import { toURLString } from '../../base/util';
  14. import { ENTER_PICTURE_IN_PICTURE } from '../picture-in-picture';
  15. /**
  16. * Middleware that captures Redux actions and uses the ExternalAPI module to
  17. * turn them into native events so the application knows about them.
  18. *
  19. * @param {Store} store - Redux store.
  20. * @returns {Function}
  21. */
  22. MiddlewareRegistry.register(store => next => action => {
  23. const result = next(action);
  24. switch (action.type) {
  25. case CONFERENCE_FAILED: {
  26. const { error, ...data } = action;
  27. // XXX Certain CONFERENCE_FAILED errors are recoverable i.e. they have
  28. // prevented the user from joining a specific conference but the app may
  29. // be able to eventually join the conference. For example, the app will
  30. // ask the user for a password upon
  31. // JitsiConferenceErrors.PASSWORD_REQUIRED and will retry joining the
  32. // conference afterwards. Such errors are to not reach the native
  33. // counterpart of the External API (or at least not in the
  34. // fatality/finality semantics attributed to
  35. // conferenceFailed:/onConferenceFailed).
  36. if (!error.recoverable) {
  37. _sendConferenceEvent(store, /* action */ {
  38. error: _toErrorString(error),
  39. ...data
  40. });
  41. }
  42. break;
  43. }
  44. case CONFERENCE_JOINED:
  45. case CONFERENCE_LEFT:
  46. case CONFERENCE_WILL_JOIN:
  47. case CONFERENCE_WILL_LEAVE:
  48. _sendConferenceEvent(store, action);
  49. break;
  50. case ENTER_PICTURE_IN_PICTURE:
  51. _sendEvent(store, _getSymbolDescription(action.type), /* data */ {});
  52. break;
  53. case LOAD_CONFIG_ERROR: {
  54. const { error, locationURL, type } = action;
  55. _sendEvent(store, _getSymbolDescription(type), /* data */ {
  56. error: _toErrorString(error),
  57. url: toURLString(locationURL)
  58. });
  59. break;
  60. }
  61. }
  62. return result;
  63. });
  64. /**
  65. * Returns a {@code String} representation of a specific error {@code Object}.
  66. *
  67. * @param {Error|Object|string} error - The error {@code Object} to return a
  68. * {@code String} representation of.
  69. * @returns {string} A {@code String} representation of the specified
  70. * {@code error}.
  71. */
  72. function _toErrorString(
  73. error: Error | { message: ?string, name: ?string } | string) {
  74. // XXX In lib-jitsi-meet and jitsi-meet we utilize errors in the form of
  75. // strings, Error instances, and plain objects which resemble Error.
  76. return (
  77. error
  78. ? typeof error === 'string'
  79. ? error
  80. : Error.prototype.toString.apply(error)
  81. : '');
  82. }
  83. /**
  84. * Gets the description of a specific {@code Symbol}.
  85. *
  86. * @param {Symbol} symbol - The {@code Symbol} to retrieve the description of.
  87. * @private
  88. * @returns {string} The description of {@code symbol}.
  89. */
  90. function _getSymbolDescription(symbol: Symbol) {
  91. let description = symbol.toString();
  92. if (description.startsWith('Symbol(') && description.endsWith(')')) {
  93. description = description.slice(7, -1);
  94. }
  95. // The polyfill es6-symbol that we use does not appear to comply with the
  96. // Symbol standard and, merely, adds @@ at the beginning of the description.
  97. if (description.startsWith('@@')) {
  98. description = description.slice(2);
  99. }
  100. return description;
  101. }
  102. /**
  103. * Sends an event to the native counterpart of the External API for a specific
  104. * conference-related redux action.
  105. *
  106. * @param {Store} store - The redux store.
  107. * @param {Action} action - The redux action.
  108. * @returns {void}
  109. */
  110. function _sendConferenceEvent(
  111. store: Object,
  112. { conference, type, ...data }: {
  113. conference: Object,
  114. type: Symbol,
  115. url: ?string
  116. }) {
  117. // For these (redux) actions, conference identifies a JitsiConference
  118. // instance. The external API cannot transport such an object so we have to
  119. // transport an "equivalent".
  120. if (conference) {
  121. data.url = toURLString(conference[JITSI_CONFERENCE_URL_KEY]);
  122. }
  123. _sendEvent(store, _getSymbolDescription(type), data);
  124. }
  125. /**
  126. * Sends a specific event to the native counterpart of the External API. Native
  127. * apps may listen to such events via the mechanisms provided by the (native)
  128. * mobile Jitsi Meet SDK.
  129. *
  130. * @param {Object} store - The redux store.
  131. * @param {string} name - The name of the event to send.
  132. * @param {Object} data - The details/specifics of the event to send determined
  133. * by/associated with the specified {@code name}.
  134. * @private
  135. * @returns {void}
  136. */
  137. function _sendEvent(
  138. { getState }: { getState: Function },
  139. name: string,
  140. data: Object) {
  141. // The JavaScript App needs to provide uniquely identifying information
  142. // to the native ExternalAPI module so that the latter may match the former
  143. // to the native JitsiMeetView which hosts it.
  144. const { app } = getState()['features/app'];
  145. if (app) {
  146. const { externalAPIScope } = app.props;
  147. if (externalAPIScope) {
  148. NativeModules.ExternalAPI.sendEvent(name, data, externalAPIScope);
  149. }
  150. }
  151. }