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.ts 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { batch } from 'react-redux';
  2. import { AnyAction } from 'redux';
  3. import { IStore } from '../app/types';
  4. import {
  5. PARTICIPANT_JOINED,
  6. PARTICIPANT_KICKED,
  7. PARTICIPANT_LEFT,
  8. PARTICIPANT_UPDATED
  9. } from '../base/participants/actionTypes';
  10. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  11. import {
  12. ADD_TO_OFFSET,
  13. INIT_SEARCH,
  14. INIT_UPDATE_STATS,
  15. RESET_SEARCH_CRITERIA
  16. } from './actionTypes';
  17. import {
  18. clearTimelineBoundary,
  19. initReorderStats,
  20. setTimelineBoundary,
  21. updateSortedSpeakerStatsIds,
  22. updateStats
  23. } from './actions.any';
  24. import { CLEAR_TIME_BOUNDARY_THRESHOLD } from './constants';
  25. import {
  26. filterBySearchCriteria,
  27. getCurrentDuration,
  28. getPendingReorder,
  29. getSortedSpeakerStatsIds,
  30. getTimelineBoundaries,
  31. resetHiddenStats
  32. } from './functions';
  33. MiddlewareRegistry.register(({ dispatch, getState }: IStore) => (next: Function) => (action: AnyAction) => {
  34. switch (action.type) {
  35. case INIT_SEARCH: {
  36. const state = getState();
  37. const stats = filterBySearchCriteria(state);
  38. dispatch(updateStats(stats));
  39. break;
  40. }
  41. case INIT_UPDATE_STATS:
  42. if (action.getSpeakerStats) {
  43. const state = getState();
  44. const speakerStats = { ...action.getSpeakerStats() };
  45. const stats = filterBySearchCriteria(state, speakerStats);
  46. const pendingReorder = getPendingReorder(state);
  47. batch(() => {
  48. if (pendingReorder) {
  49. dispatch(updateSortedSpeakerStatsIds(getSortedSpeakerStatsIds(state, stats) ?? []));
  50. }
  51. dispatch(updateStats(stats));
  52. });
  53. }
  54. break;
  55. case RESET_SEARCH_CRITERIA: {
  56. const state = getState();
  57. const stats = resetHiddenStats(state);
  58. dispatch(updateStats(stats));
  59. break;
  60. }
  61. case PARTICIPANT_JOINED:
  62. case PARTICIPANT_LEFT:
  63. case PARTICIPANT_KICKED:
  64. case PARTICIPANT_UPDATED: {
  65. const { pendingReorder } = getState()['features/speaker-stats'];
  66. if (!pendingReorder) {
  67. dispatch(initReorderStats());
  68. }
  69. break;
  70. }
  71. case ADD_TO_OFFSET: {
  72. const state = getState();
  73. const { timelineBoundary } = state['features/speaker-stats'];
  74. const { right } = getTimelineBoundaries(state);
  75. const currentDuration = getCurrentDuration(state) ?? 0;
  76. if (Math.abs((right + action.value) - currentDuration) < CLEAR_TIME_BOUNDARY_THRESHOLD) {
  77. dispatch(clearTimelineBoundary());
  78. } else if (!timelineBoundary) {
  79. dispatch(setTimelineBoundary(currentDuration ?? 0));
  80. }
  81. break;
  82. }
  83. }
  84. return next(action);
  85. });