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

middleware.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. _sendEvent(store, _getSymbolDescription(type), data);
  37. break;
  38. }
  39. case LOAD_CONFIG_ERROR: {
  40. const { error, locationURL, type } = action;
  41. _sendEvent(store, _getSymbolDescription(type), {
  42. error: String(error),
  43. url: toURLString(locationURL)
  44. });
  45. break;
  46. }
  47. }
  48. return result;
  49. });
  50. /**
  51. * Gets the description of a specific {@code Symbol}.
  52. *
  53. * @param {Symbol} symbol - The {@code Symbol} to retrieve the description of.
  54. * @private
  55. * @returns {string} The description of {@code symbol}.
  56. */
  57. function _getSymbolDescription(symbol: Symbol) {
  58. let description = symbol.toString();
  59. if (description.startsWith('Symbol(') && description.endsWith(')')) {
  60. description = description.slice(7, -1);
  61. }
  62. // The polyfill es6-symbol that we use does not appear to comply with the
  63. // Symbol standard and, merely, adds @@ at the beginning of the description.
  64. if (description.startsWith('@@')) {
  65. description = description.slice(2);
  66. }
  67. return description;
  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. if (externalAPIScope) {
  91. NativeModules.ExternalAPI.sendEvent(name, data, externalAPIScope);
  92. }
  93. }
  94. }