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.any.ts 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { IReduxState } from '../app/types';
  2. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  3. import { ADD_GIF_FOR_PARTICIPANT, HIDE_GIF_FOR_PARTICIPANT, SHOW_GIF_FOR_PARTICIPANT } from './actionTypes';
  4. import { removeGif } from './actions';
  5. import { GIF_DEFAULT_TIMEOUT } from './constants';
  6. import { getGifForParticipant } from './function.any';
  7. /**
  8. * Middleware which intercepts Gifs actions to handle changes to the
  9. * visibility timeout of the Gifs.
  10. *
  11. * @param {Store} store - The redux store.
  12. * @returns {Function}
  13. */
  14. MiddlewareRegistry.register(store => next => action => {
  15. const { dispatch, getState } = store;
  16. const state = getState();
  17. switch (action.type) {
  18. case ADD_GIF_FOR_PARTICIPANT: {
  19. const id = action.participantId;
  20. const { giphy } = state['features/base/config'];
  21. _clearGifTimeout(state, id);
  22. const timeoutID = setTimeout(() => dispatch(removeGif(id)), giphy?.tileTime || GIF_DEFAULT_TIMEOUT);
  23. action.timeoutID = timeoutID;
  24. break;
  25. }
  26. case SHOW_GIF_FOR_PARTICIPANT: {
  27. const id = action.participantId;
  28. _clearGifTimeout(state, id);
  29. break;
  30. }
  31. case HIDE_GIF_FOR_PARTICIPANT: {
  32. const { giphy } = state['features/base/config'];
  33. const id = action.participantId;
  34. const timeoutID = setTimeout(() => dispatch(removeGif(id)), giphy?.tileTime || GIF_DEFAULT_TIMEOUT);
  35. action.timeoutID = timeoutID;
  36. break;
  37. }
  38. }
  39. return next(action);
  40. });
  41. /**
  42. * Clears GIF timeout.
  43. *
  44. * @param {IReduxState} state - Redux state.
  45. * @param {string} id - Id of the participant for whom to clear the timeout.
  46. * @returns {void}
  47. */
  48. function _clearGifTimeout(state: IReduxState, id: string) {
  49. const gif = getGifForParticipant(state, id);
  50. clearTimeout(gif?.timeoutID ?? -1);
  51. }