您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

reducer.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { ReducerRegistry } from '../base/redux';
  2. import {
  3. ADD_TRANSCRIPT_MESSAGE,
  4. REMOVE_TRANSCRIPT_MESSAGE,
  5. UPDATE_TRANSCRIPT_MESSAGE
  6. } from './actionTypes';
  7. /**
  8. * Default State for 'features/transcription' feature
  9. */
  10. const defaultState = {
  11. transcriptMessages: new Map()
  12. };
  13. /**
  14. * Listen for actions for the transcription feature to be used by the actions
  15. * to update the rendered transcription subtitles.
  16. */
  17. ReducerRegistry.register('features/subtitles', (
  18. state = defaultState, action) => {
  19. switch (action.type) {
  20. case ADD_TRANSCRIPT_MESSAGE:
  21. return _addTranscriptMessage(state, action);
  22. case REMOVE_TRANSCRIPT_MESSAGE:
  23. return _removeTranscriptMessage(state, action);
  24. case UPDATE_TRANSCRIPT_MESSAGE:
  25. return _updateTranscriptMessage(state, action);
  26. }
  27. return state;
  28. });
  29. /**
  30. * Reduces a specific Redux action ADD_TRANSCRIPT_MESSAGE of the feature
  31. * transcription.
  32. *
  33. * @param {Object} state - The Redux state of the feature transcription.
  34. * @param {Action} action -The Redux action ADD_TRANSCRIPT_MESSAGE to reduce.
  35. * @returns {Object} The new state of the feature transcription after the
  36. * reduction of the specified action.
  37. */
  38. function _addTranscriptMessage(state,
  39. { transcriptMessageID, participantName }) {
  40. const newTranscriptMessages = new Map(state.transcriptMessages);
  41. // Adds a new key,value pair to the Map once a new message arrives.
  42. newTranscriptMessages.set(transcriptMessageID, { participantName });
  43. return {
  44. ...state,
  45. transcriptMessages: newTranscriptMessages
  46. };
  47. }
  48. /**
  49. * Reduces a specific Redux action REMOVE_TRANSCRIPT_MESSAGE of the feature
  50. * transcription.
  51. *
  52. * @param {Object} state - The Redux state of the feature transcription.
  53. * @param {Action} action -The Redux action REMOVE_TRANSCRIPT_MESSAGE to reduce.
  54. * @returns {Object} The new state of the feature transcription after the
  55. * reduction of the specified action.
  56. */
  57. function _removeTranscriptMessage(state, { transcriptMessageID }) {
  58. const newTranscriptMessages = new Map(state.transcriptMessages);
  59. // Deletes the key from Map once a final message arrives.
  60. newTranscriptMessages.delete(transcriptMessageID);
  61. return {
  62. ...state,
  63. transcriptMessages: newTranscriptMessages
  64. };
  65. }
  66. /**
  67. * Reduces a specific Redux action UPDATE_TRANSCRIPT_MESSAGE of the feature
  68. * transcription.
  69. *
  70. * @param {Object} state - The Redux state of the feature transcription.
  71. * @param {Action} action -The Redux action UPDATE_TRANSCRIPT_MESSAGE to reduce.
  72. * @returns {Object} The new state of the feature transcription after the
  73. * reduction of the specified action.
  74. */
  75. function _updateTranscriptMessage(state,
  76. { transcriptMessageID, newTranscriptMessage }) {
  77. const newTranscriptMessages = new Map(state.transcriptMessages);
  78. // Updates the new message for the given key in the Map.
  79. newTranscriptMessages.set(transcriptMessageID, newTranscriptMessage);
  80. return {
  81. ...state,
  82. transcriptMessages: newTranscriptMessages
  83. };
  84. }