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

middleware.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* @flow */
  2. import { SET_CONFIG } from '../config';
  3. import { setLoggingConfig } from '../logging';
  4. import { PARTICIPANT_LEFT } from '../participants';
  5. import { MiddlewareRegistry } from '../redux';
  6. import { disposeLib, initLib, setWebRTCReady } from './actions';
  7. import { LIB_DID_INIT, LIB_INIT_ERROR } from './actionTypes';
  8. import { WEBRTC_NOT_READY, WEBRTC_NOT_SUPPORTED } from './constants';
  9. /**
  10. * Middleware that captures PARTICIPANT_LEFT action for a local participant
  11. * (which signalizes that we finally left the app) and disposes lib-jitsi-meet.
  12. * Also captures SET_CONFIG action and disposes previous instance (if any) of
  13. * lib-jitsi-meet, and initializes a new one with new config.
  14. *
  15. * @param {Store} store - Redux store.
  16. * @private
  17. * @returns {Function}
  18. */
  19. MiddlewareRegistry.register(store => next => action => {
  20. switch (action.type) {
  21. case LIB_DID_INIT:
  22. store.dispatch(setWebRTCReady(true));
  23. break;
  24. case LIB_INIT_ERROR:
  25. return _libInitError(store, next, action);
  26. case PARTICIPANT_LEFT:
  27. action.participant.local && store.dispatch(disposeLib());
  28. break;
  29. case SET_CONFIG:
  30. return _setConfig(store, next, action);
  31. }
  32. return next(action);
  33. });
  34. /**
  35. * Notifies the feature base/lib-jitsi-meet that the action LIB_INIT_ERROR is
  36. * being dispatched within a specific Redux store.
  37. *
  38. * @param {Store} store - The Redux store in which the specified action is being
  39. * dispatched.
  40. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  41. * specified action to the specified store.
  42. * @param {Action} action - The Redux action LIB_INIT_ERROR which is being
  43. * dispatched in the specified store.
  44. * @private
  45. * @returns {Object} The new state that is the result of the reduction of the
  46. * specified action.
  47. */
  48. function _libInitError(store, next, action) {
  49. const nextState = next(action);
  50. const { error } = action;
  51. if (error) {
  52. let webRTCReady;
  53. switch (error.name) {
  54. case WEBRTC_NOT_READY:
  55. webRTCReady = error.webRTCReadyPromise;
  56. break;
  57. case WEBRTC_NOT_SUPPORTED:
  58. webRTCReady = false;
  59. break;
  60. }
  61. typeof webRTCReady === 'undefined'
  62. || store.dispatch(setWebRTCReady(webRTCReady));
  63. }
  64. return nextState;
  65. }
  66. /**
  67. * Notifies the feature base/lib-jitsi-meet that the action SET_CONFIG is being
  68. * dispatched within a specific Redux store.
  69. *
  70. * @param {Store} store - The Redux store in which the specified action is being
  71. * dispatched.
  72. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  73. * specified action to the specified store.
  74. * @param {Action} action - The Redux action SET_CONFIG which is being
  75. * dispatched in the specified store.
  76. * @private
  77. * @returns {Object} The new state that is the result of the reduction of the
  78. * specified action.
  79. */
  80. function _setConfig({ dispatch, getState }, next, action) {
  81. const { initialized } = getState()['features/base/lib-jitsi-meet'];
  82. // XXX Since the config is changing, the library lib-jitsi-meet must be
  83. // initialized again with the new config. Consequently, it may need to be
  84. // disposed of first.
  85. // TODO Currently, disposeLib actually does not dispose of lib-jitsi-meet
  86. // because lib-jitsi-meet does not implement such functionality.
  87. if (initialized) {
  88. dispatch(disposeLib());
  89. }
  90. // Let the new config into the Redux store (because initLib will read it
  91. // from there).
  92. const result = next(action);
  93. // FIXME Obviously, the following is bad design. However, I'm currently
  94. // introducing the features base/config and base/logging and I'm trying to
  95. // minimize the scope of the changes while I'm attempting to preserve
  96. // compatibility with the existing partially React-ified Web source code and
  97. // what was already executing on React Native. Additionally, I do not care
  98. // to load logging_config.js on React Native.
  99. dispatch(setLoggingConfig(window.loggingConfig));
  100. dispatch(initLib());
  101. return result;
  102. }