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.

reducer.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // @flow
  2. import { PersistencyRegistry, ReducerRegistry } from '../base/redux';
  3. import {
  4. STORE_CURRENT_CONFERENCE,
  5. UPDATE_CONFERENCE_DURATION
  6. } from './actionTypes';
  7. import { LIST_SIZE } from './constants';
  8. import { getLegacyRecentRoomList } from './functions';
  9. /**
  10. * The Redux subtree of this feature.
  11. */
  12. const STORE_NAME = 'features/recent-list';
  13. /**
  14. * Registers the redux store subtree of this feature for persistency.
  15. */
  16. PersistencyRegistry.register(STORE_NAME, {
  17. list: true
  18. });
  19. /**
  20. * Reduces the Redux actions of the feature features/recent-list.
  21. */
  22. ReducerRegistry.register(STORE_NAME, (state = {
  23. list: getLegacyRecentRoomList()
  24. }, action) => {
  25. switch (action.type) {
  26. case STORE_CURRENT_CONFERENCE:
  27. return _storeCurrentConference(state, action);
  28. case UPDATE_CONFERENCE_DURATION:
  29. return _updateConferenceDuration(state, action);
  30. default:
  31. return state;
  32. }
  33. });
  34. /**
  35. * Adds a new list entry to the redux store.
  36. *
  37. * @param {Object} state - The redux state.
  38. * @param {Object} action - The redux action.
  39. * @returns {Object}
  40. */
  41. function _storeCurrentConference(state, action) {
  42. const { locationURL } = action;
  43. const conference = locationURL.href;
  44. // If the current conference is already in the list, we remove it to re-add
  45. // it to the top.
  46. const list = state.list.filter(e => e.conference !== conference);
  47. // This is a reverse sorted array (i.e. newer elements at the end).
  48. list.push({
  49. conference,
  50. conferenceDuration: 0, // we don't have this data yet
  51. date: Date.now()
  52. });
  53. // maximising the size
  54. list.splice(0, list.length - LIST_SIZE);
  55. return {
  56. list
  57. };
  58. }
  59. /**
  60. * Updates the conference length when left.
  61. *
  62. * @param {Object} state - The redux state.
  63. * @param {Object} action - The redux action.
  64. * @returns {Object}
  65. */
  66. function _updateConferenceDuration(state, action) {
  67. const { locationURL } = action;
  68. if (locationURL && locationURL.href) {
  69. const list = state.list;
  70. if (list.length > 0) {
  71. const mostRecentURL = list[list.length - 1];
  72. if (mostRecentURL.conference === locationURL.href) {
  73. // The last conference start was stored so we need to update the
  74. // length.
  75. mostRecentURL.conferenceDuration
  76. = Date.now() - mostRecentURL.date;
  77. return {
  78. list
  79. };
  80. }
  81. }
  82. }
  83. return state;
  84. }