Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

middleware.js 3.2KB

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