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

middleware.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 { REQUEST_PIP_MODE } 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 LOAD_CONFIG_ERROR: {
  51. const { error, locationURL, type } = action;
  52. _sendEvent(store, _getSymbolDescription(type), /* data */ {
  53. error: _toErrorString(error),
  54. url: toURLString(locationURL)
  55. });
  56. break;
  57. }
  58. case REQUEST_PIP_MODE:
  59. _sendEvent(store, _getSymbolDescription(action.type), /* data */ {});
  60. }
  61. return result;
  62. });
  63. /**
  64. * Returns a {@code String} representation of a specific error {@code Object}.
  65. *
  66. * @param {Error|Object|string} error - The error {@code Object} to return a
  67. * {@code String} representation of.
  68. * @returns {string} A {@code String} representation of the specified
  69. * {@code error}.
  70. */
  71. function _toErrorString(
  72. error: Error | { message: ?string, name: ?string } | string) {
  73. // XXX In lib-jitsi-meet and jitsi-meet we utilize errors in the form of
  74. // strings, Error instances, and plain objects which resemble Error.
  75. return (
  76. error
  77. ? typeof error === 'string'
  78. ? error
  79. : Error.prototype.toString.apply(error)
  80. : '');
  81. }
  82. /**
  83. * Gets the description of a specific {@code Symbol}.
  84. *
  85. * @param {Symbol} symbol - The {@code Symbol} to retrieve the description of.
  86. * @private
  87. * @returns {string} The description of {@code symbol}.
  88. */
  89. function _getSymbolDescription(symbol: Symbol) {
  90. let description = symbol.toString();
  91. if (description.startsWith('Symbol(') && description.endsWith(')')) {
  92. description = description.slice(7, -1);
  93. }
  94. // The polyfill es6-symbol that we use does not appear to comply with the
  95. // Symbol standard and, merely, adds @@ at the beginning of the description.
  96. if (description.startsWith('@@')) {
  97. description = description.slice(2);
  98. }
  99. return description;
  100. }
  101. /**
  102. * Sends an event to the native counterpart of the External API for a specific
  103. * conference-related redux action.
  104. *
  105. * @param {Store} store - The redux store.
  106. * @param {Action} action - The redux action.
  107. * @returns {void}
  108. */
  109. function _sendConferenceEvent(
  110. store: Object,
  111. { conference, type, ...data }: {
  112. conference: Object,
  113. type: Symbol,
  114. url: ?string
  115. }) {
  116. // For these (redux) actions, conference identifies a JitsiConference
  117. // instance. The external API cannot transport such an object so we have to
  118. // transport an "equivalent".
  119. if (conference) {
  120. data.url = toURLString(conference[JITSI_CONFERENCE_URL_KEY]);
  121. }
  122. _sendEvent(store, _getSymbolDescription(type), data);
  123. }
  124. /**
  125. * Sends a specific event to the native counterpart of the External API. Native
  126. * apps may listen to such events via the mechanisms provided by the (native)
  127. * mobile Jitsi Meet SDK.
  128. *
  129. * @param {Object} store - The redux store.
  130. * @param {string} name - The name of the event to send.
  131. * @param {Object} data - The details/specifics of the event to send determined
  132. * by/associated with the specified {@code name}.
  133. * @private
  134. * @returns {void}
  135. */
  136. function _sendEvent(
  137. { getState }: { getState: Function },
  138. name: string,
  139. data: Object) {
  140. // The JavaScript App needs to provide uniquely identifying information
  141. // to the native ExternalAPI module so that the latter may match the former
  142. // to the native JitsiMeetView which hosts it.
  143. const { app } = getState()['features/app'];
  144. if (app) {
  145. const { externalAPIScope } = app.props;
  146. if (externalAPIScope) {
  147. NativeModules.ExternalAPI.sendEvent(name, data, externalAPIScope);
  148. }
  149. }
  150. }