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

middleware.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* @flow */
  2. import {
  3. SET_AUDIO_AVAILABLE,
  4. SET_AUDIO_MUTED,
  5. SET_VIDEO_AVAILABLE,
  6. SET_VIDEO_MUTED } from '../base/media';
  7. import { MiddlewareRegistry } from '../base/redux';
  8. import { setToolbarButton } from './actions';
  9. import { CLEAR_TOOLBOX_TIMEOUT, SET_TOOLBOX_TIMEOUT } from './actionTypes';
  10. /**
  11. * Middleware which intercepts Toolbox actions to handle changes to the
  12. * visibility timeout of the Toolbox.
  13. *
  14. * @param {Store} store - The redux store.
  15. * @returns {Function}
  16. */
  17. MiddlewareRegistry.register(store => next => action => {
  18. switch (action.type) {
  19. case CLEAR_TOOLBOX_TIMEOUT: {
  20. const { timeoutID } = store.getState()['features/toolbox'];
  21. clearTimeout(timeoutID);
  22. break;
  23. }
  24. case SET_TOOLBOX_TIMEOUT: {
  25. const { timeoutID } = store.getState()['features/toolbox'];
  26. const { handler, timeoutMS } = action;
  27. clearTimeout(timeoutID);
  28. const newTimeoutId = setTimeout(handler, timeoutMS);
  29. action.timeoutID = newTimeoutId;
  30. break;
  31. }
  32. case SET_AUDIO_AVAILABLE:
  33. case SET_AUDIO_MUTED: {
  34. return _setAudioAvailableOrMuted(store, next, action);
  35. }
  36. case SET_VIDEO_AVAILABLE:
  37. case SET_VIDEO_MUTED:
  38. return _setVideoAvailableOrMuted(store, next, action);
  39. }
  40. return next(action);
  41. });
  42. /**
  43. * Adjusts the state of toolbar's microphone button.
  44. *
  45. * @param {Store} store - The Redux store instance.
  46. * @param {Function} next - The redux function to continue dispatching the
  47. * specified {@code action} in the specified {@code store}.
  48. * @param {Object} action - Either SET_AUDIO_AVAILABLE or SET_AUDIO_MUTED.
  49. *
  50. * @returns {*}
  51. */
  52. function _setAudioAvailableOrMuted({ dispatch, getState }, next, action) {
  53. const result = next(action);
  54. const { available, muted } = getState()['features/base/media'].audio;
  55. const i18nKey = available ? 'mute' : 'micDisabled';
  56. dispatch(setToolbarButton('microphone', {
  57. enabled: available,
  58. i18n: `[content]toolbar.${i18nKey}`,
  59. toggled: available ? muted : true
  60. }));
  61. return result;
  62. }
  63. /**
  64. * Adjusts the state of toolbar's camera button.
  65. *
  66. * @param {Store} store - The redux store.
  67. * @param {Function} next - The redux function to continue dispatching the
  68. * specified {@code action} in the specified {@code store}.
  69. * @param {Object} action - Either {@link SET_VIDEO_AVAILABLE} or
  70. * {@link SET_VIDEO_MUTED}.
  71. * @returns {Object} The new state that is the result of the reduction of the
  72. * specified {@code action}.
  73. */
  74. function _setVideoAvailableOrMuted({ dispatch, getState }, next, action) {
  75. const result = next(action);
  76. const { available, muted } = getState()['features/base/media'].video;
  77. const i18nKey = available ? 'videomute' : 'cameraDisabled';
  78. dispatch(setToolbarButton('camera', {
  79. enabled: available,
  80. i18n: `[content]toolbar.${i18nKey}`,
  81. toggled: available ? muted : true
  82. }));
  83. return result;
  84. }