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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. let name = type.toString();
  38. // XXX We are using Symbol for (redux) action types at the time of this
  39. // writing so the Symbol's description should be used.
  40. if (name.startsWith('Symbol(') && name.endsWith(')')) {
  41. name = name.slice(7, -1);
  42. }
  43. // The polyfill es6-symbol that we use does not appear to comply with
  44. // the Symbol standard and, merely, adds @@ at the beginning of the
  45. // description.
  46. if (name.startsWith('@@')) {
  47. name = name.slice(2);
  48. }
  49. _sendEvent(store, name, data);
  50. break;
  51. }
  52. }
  53. return result;
  54. });
  55. /**
  56. * Sends a specific event to the native counterpart of the External API. Native
  57. * apps may listen to such events via the mechanisms provided by the (native)
  58. * mobile Jitsi Meet SDK.
  59. *
  60. * @param {Object} store - The redux store associated with the need to send the
  61. * specified event.
  62. * @param {string} name - The name of the event to send.
  63. * @param {Object} data - The details/specifics of the event to send determined
  64. * by/associated with the specified {@code name}.
  65. * @private
  66. * @returns {void}
  67. */
  68. function _sendEvent(store: Object, name: string, data: Object) {
  69. // The JavaScript App needs to provide uniquely identifying information
  70. // to the native ExternalAPI module so that the latter may match the former
  71. // to the native JitsiMeetView which hosts it.
  72. const state = store.getState();
  73. const { app } = state['features/app'];
  74. if (app) {
  75. const { externalAPIScope } = app.props;
  76. if (externalAPIScope) {
  77. NativeModules.ExternalAPI.sendEvent(name, data, externalAPIScope);
  78. }
  79. }
  80. }