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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* @flow */
  2. import { LIST_SIZE } from './constants';
  3. import { getRecentUrls, updaterecentUrls } from './functions';
  4. import { CONFERENCE_WILL_LEAVE, SET_ROOM } from '../base/conference';
  5. import { MiddlewareRegistry } from '../base/redux';
  6. /**
  7. * Middleware that captures joined rooms so then it can be saved to
  8. * {@code localStorage}
  9. *
  10. * @param {Store} store - Redux store.
  11. * @returns {Function}
  12. */
  13. MiddlewareRegistry.register(store => next => action => {
  14. switch (action.type) {
  15. case CONFERENCE_WILL_LEAVE:
  16. return _updateConferenceDuration(store, next, action);
  17. case SET_ROOM:
  18. return _storeJoinedRoom(store, next, action);
  19. }
  20. return next(action);
  21. });
  22. /**
  23. * Stores the recently joined room in {@code localStorage}.
  24. *
  25. * @param {Store} store - The redux store in which the specified action is being
  26. * dispatched.
  27. * @param {Dispatch} next - The redux dispatch function to dispatch the
  28. * specified action to the specified store.
  29. * @param {Action} action - The redux action CONFERENCE_JOINED which is being
  30. * dispatched in the specified store.
  31. * @returns {Object} The new state that is the result of the reduction of the
  32. * specified action.
  33. */
  34. function _storeJoinedRoom(store, next, action) {
  35. const result = next(action);
  36. const { room } = action;
  37. if (room) {
  38. const { locationURL } = store.getState()['features/base/connection'];
  39. const conferenceLink = locationURL.href;
  40. // if the current conference is already in the list,
  41. // we remove it to add it
  42. // to the top at the end
  43. const recentUrls = getRecentUrls().filter(
  44. entry => entry.conference !== conferenceLink
  45. );
  46. // please note, this is a reverse sorted array
  47. // (newer elements at the end)
  48. recentUrls.push({
  49. conference: conferenceLink,
  50. date: Date.now(),
  51. conferenceDuration: 0
  52. });
  53. // maximising the size
  54. recentUrls.splice(0, recentUrls.length - LIST_SIZE);
  55. updaterecentUrls(recentUrls);
  56. }
  57. return result;
  58. }
  59. /**
  60. * Updates the conference length when left.
  61. *
  62. * @private
  63. * @param {Store} store - The redux store in which the specified action is being
  64. * dispatched.
  65. * @param {Dispatch} next - The redux dispatch function to dispatch the
  66. * specified action to the specified store.
  67. * @param {Action} action - The redux action CONFERENCE_JOINED which is being
  68. * dispatched in the specified store.
  69. * @returns {Object} The new state that is the result of the reduction of the
  70. * specified action.
  71. */
  72. function _updateConferenceDuration(store, next, action) {
  73. const result = next(action);
  74. const { locationURL } = store.getState()['features/base/connection'];
  75. if (locationURL && locationURL.href) {
  76. const recentUrls = getRecentUrls();
  77. if (recentUrls.length > 0
  78. && recentUrls[recentUrls.length - 1].conference
  79. === locationURL.href) {
  80. // the last conference start was stored
  81. // so we need to update the length
  82. recentUrls[recentUrls.length - 1].conferenceDuration
  83. = Date.now() - recentUrls[recentUrls.length - 1].date;
  84. updaterecentUrls(recentUrls);
  85. }
  86. }
  87. return result;
  88. }