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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { isEqual } from 'lodash-es';
  2. import { UPDATE_CONFERENCE_METADATA } from '../base/conference/actionTypes';
  3. import ReducerRegistry from '../base/redux/ReducerRegistry';
  4. import {
  5. ADD_FILE,
  6. REMOVE_FILE,
  7. UPDATE_FILE_UPLOAD_PROGRESS
  8. } from './actionTypes';
  9. import { FILE_SHARING_PREFIX } from './constants';
  10. import { IFileMetadata } from './types';
  11. export interface IFileSharingState {
  12. files: Map<string, IFileMetadata>;
  13. }
  14. const DEFAULT_STATE = {
  15. files: new Map<string, IFileMetadata>()
  16. };
  17. ReducerRegistry.register<IFileSharingState>('features/file-sharing',
  18. (state = DEFAULT_STATE, action): IFileSharingState => {
  19. switch (action.type) {
  20. case ADD_FILE: {
  21. const newFiles = new Map(state.files);
  22. newFiles.set(action.file.fileId, action.file);
  23. return {
  24. files: newFiles
  25. };
  26. }
  27. case REMOVE_FILE: {
  28. const newFiles = new Map(state.files);
  29. newFiles.delete(action.fileId);
  30. return {
  31. files: newFiles
  32. };
  33. }
  34. case UPDATE_FILE_UPLOAD_PROGRESS: {
  35. const newFiles = new Map(state.files);
  36. const file = newFiles.get(action.fileId);
  37. if (file) {
  38. newFiles.set(action.fileId, { ...file, progress: action.progress });
  39. }
  40. return {
  41. files: newFiles
  42. };
  43. }
  44. case UPDATE_CONFERENCE_METADATA: {
  45. const { metadata } = action;
  46. if (!metadata) {
  47. return state;
  48. }
  49. const files = new Map();
  50. for (const [ key, value ] of Object.entries(metadata)) {
  51. if (key.startsWith(FILE_SHARING_PREFIX)) {
  52. const fileId = key.substring(FILE_SHARING_PREFIX.length + 1);
  53. files.set(fileId, value);
  54. }
  55. }
  56. if (files.size === 0) {
  57. return state;
  58. }
  59. const newFiles: Map<string, IFileMetadata> = new Map(state.files);
  60. for (const [ key, value ] of files) {
  61. // Deleted files will not have fileId.
  62. if (!value.fileId) {
  63. newFiles.delete(key);
  64. } else {
  65. newFiles.set(key, value);
  66. }
  67. }
  68. if (isEqual(newFiles, state.files)) {
  69. return state;
  70. }
  71. return {
  72. files: newFiles
  73. };
  74. }
  75. default:
  76. return state;
  77. }
  78. });