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

middleware.js 3.5KB

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