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

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