You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

middleware.js 3.7KB

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