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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // @flow
  2. import { CONFERENCE_WILL_LEAVE, SET_ROOM } from '../base/conference';
  3. import { MiddlewareRegistry } from '../base/redux';
  4. import { LIST_SIZE } from './constants';
  5. import { getRecentURLs, updateRecentURLs } from './functions';
  6. /**
  7. * Middleware that captures joined rooms so they can be saved into
  8. * {@code window.localStorage}.
  9. *
  10. * @param {Store} store - The 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 into {@code window.localStorage}.
  24. *
  25. * @param {Store} store - The redux store in which the specified action is being
  26. * dispatched.
  27. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  28. * specified action to the specified store.
  29. * @param {Action} action - The redux action {@code SET_ROOM} which is being
  30. * dispatched in the specified store.
  31. * @private
  32. * @returns {Object} The new state that is the result of the reduction of the
  33. * specified action.
  34. */
  35. function _storeJoinedRoom(store, next, action) {
  36. const result = next(action);
  37. const { room } = action;
  38. if (room) {
  39. const { locationURL } = store.getState()['features/base/connection'];
  40. const conference = locationURL.href;
  41. // If the current conference is already in the list, we remove it to add
  42. // it to the top at the end.
  43. const recentURLs
  44. = getRecentURLs()
  45. .filter(e => e.conference !== conference);
  46. // XXX This is a reverse sorted array (i.e. newer elements at the end).
  47. recentURLs.push({
  48. conference,
  49. conferenceDuration: 0,
  50. date: Date.now()
  51. });
  52. // maximising the size
  53. recentURLs.splice(0, recentURLs.length - LIST_SIZE);
  54. updateRecentURLs(recentURLs);
  55. }
  56. return result;
  57. }
  58. /**
  59. * Updates the conference length when left.
  60. *
  61. * @param {Store} store - The redux store in which the specified action is being
  62. * dispatched.
  63. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  64. * specified action to the specified store.
  65. * @param {Action} action - The redux action {@code CONFERENCE_WILL_LEAVE} which
  66. * is being dispatched in the specified store.
  67. * @private
  68. * @returns {Object} The new state that is the result of the reduction of the
  69. * specified action.
  70. */
  71. function _updateConferenceDuration({ getState }, next, action) {
  72. const result = next(action);
  73. const { locationURL } = getState()['features/base/connection'];
  74. if (locationURL && locationURL.href) {
  75. const recentURLs = getRecentURLs();
  76. if (recentURLs.length > 0) {
  77. const mostRecentURL = recentURLs[recentURLs.length - 1];
  78. if (mostRecentURL.conference === locationURL.href) {
  79. // The last conference start was stored so we need to update the
  80. // length.
  81. mostRecentURL.conferenceDuration
  82. = Date.now() - mostRecentURL.date;
  83. updateRecentURLs(recentURLs);
  84. }
  85. }
  86. }
  87. return result;
  88. }