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

middleware.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. /**
  15. * Middleware that captures Redux actions and uses the ExternalAPI module to
  16. * turn them into native events so the application knows about them.
  17. *
  18. * @param {Store} store - Redux store.
  19. * @returns {Function}
  20. */
  21. MiddlewareRegistry.register(store => next => action => {
  22. const result = next(action);
  23. switch (action.type) {
  24. case CONFERENCE_FAILED:
  25. case CONFERENCE_JOINED:
  26. case CONFERENCE_LEFT:
  27. case CONFERENCE_WILL_JOIN:
  28. case CONFERENCE_WILL_LEAVE: {
  29. const { conference, type, ...data } = action;
  30. // For the above (redux) actions, conference identifies a
  31. // JitsiConference instance. The external API cannot transport such an
  32. // object so we have to transport an "equivalent".
  33. if (conference) {
  34. data.url = toURLString(conference[JITSI_CONFERENCE_URL_KEY]);
  35. }
  36. // The (externa API) event's name is the string representation of the
  37. // (redux) action's type.
  38. const name = _getSymbolDescription(type);
  39. _sendEvent(store, name, data);
  40. break;
  41. }
  42. case LOAD_CONFIG_ERROR: {
  43. const { type, error } = action;
  44. _sendEvent(
  45. store,
  46. _getSymbolDescription(type),
  47. { error: String(error) });
  48. break;
  49. }
  50. }
  51. return result;
  52. });
  53. /**
  54. * Gets the description of a specific <tt>Symbol</tt>.
  55. *
  56. * @param {Symbol} symbol - The <tt>Symbol</tt> to retrieve the description of.
  57. * @private
  58. * @returns {string} The description of <tt>symbol</tt>.
  59. */
  60. function _getSymbolDescription(symbol: Symbol) {
  61. let description = symbol.toString();
  62. if (description.startsWith('Symbol(') && description.endsWith(')')) {
  63. description = description.slice(7, -1);
  64. }
  65. // The polyfill es6-symbol that we use does not appear to comply with the
  66. // Symbol standard and, merely, adds @@ at the beginning of the description.
  67. if (description.startsWith('@@')) {
  68. description = description.slice(2);
  69. }
  70. return description;
  71. }
  72. /**
  73. * Sends a specific event to the native counterpart of the External API. Native
  74. * apps may listen to such events via the mechanisms provided by the (native)
  75. * mobile Jitsi Meet SDK.
  76. *
  77. * @param {Object} store - The redux store associated with the need to send the
  78. * specified event.
  79. * @param {string} name - The name of the event to send.
  80. * @param {Object} data - The details/specifics of the event to send determined
  81. * by/associated with the specified {@code name}.
  82. * @private
  83. * @returns {void}
  84. */
  85. function _sendEvent(store: Object, name: string, data: Object) {
  86. // The JavaScript App needs to provide uniquely identifying information
  87. // to the native ExternalAPI module so that the latter may match the former
  88. // to the native JitsiMeetView which hosts it.
  89. const state = store.getState();
  90. const { app } = state['features/app'];
  91. if (app) {
  92. const { externalAPIScope } = app.props;
  93. if (externalAPIScope) {
  94. NativeModules.ExternalAPI.sendEvent(name, data, externalAPIScope);
  95. }
  96. }
  97. }