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

middleware.js 4.0KB

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