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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { ReducerRegistry } from '../base/redux';
  2. import {
  3. REMOVE_TRANSCRIPT_MESSAGE, TOGGLE_REQUESTING_SUBTITLES,
  4. UPDATE_TRANSCRIPT_MESSAGE
  5. } from './actionTypes';
  6. /**
  7. * Default State for 'features/transcription' feature
  8. */
  9. const defaultState = {
  10. _transcriptMessages: new Map(),
  11. _requestingSubtitles: false
  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 REMOVE_TRANSCRIPT_MESSAGE:
  21. return _removeTranscriptMessage(state, action);
  22. case UPDATE_TRANSCRIPT_MESSAGE:
  23. return _updateTranscriptMessage(state, action);
  24. case TOGGLE_REQUESTING_SUBTITLES:
  25. return {
  26. ...state,
  27. _requestingSubtitles: !state._requestingSubtitles
  28. };
  29. }
  30. return state;
  31. });
  32. /**
  33. * Reduces a specific Redux action REMOVE_TRANSCRIPT_MESSAGE of the feature
  34. * transcription.
  35. *
  36. * @param {Object} state - The Redux state of the feature transcription.
  37. * @param {Action} action -The Redux action REMOVE_TRANSCRIPT_MESSAGE to reduce.
  38. * @returns {Object} The new state of the feature transcription after the
  39. * reduction of the specified action.
  40. */
  41. function _removeTranscriptMessage(state, { transcriptMessageID }) {
  42. const newTranscriptMessages = new Map(state._transcriptMessages);
  43. // Deletes the key from Map once a final message arrives.
  44. newTranscriptMessages.delete(transcriptMessageID);
  45. return {
  46. ...state,
  47. _transcriptMessages: newTranscriptMessages
  48. };
  49. }
  50. /**
  51. * Reduces a specific Redux action UPDATE_TRANSCRIPT_MESSAGE of the feature
  52. * transcription.
  53. *
  54. * @param {Object} state - The Redux state of the feature transcription.
  55. * @param {Action} action -The Redux action UPDATE_TRANSCRIPT_MESSAGE to reduce.
  56. * @returns {Object} The new state of the feature transcription after the
  57. * reduction of the specified action.
  58. */
  59. function _updateTranscriptMessage(state,
  60. { transcriptMessageID, newTranscriptMessage }) {
  61. const newTranscriptMessages = new Map(state._transcriptMessages);
  62. // Updates the new message for the given key in the Map.
  63. newTranscriptMessages.set(transcriptMessageID, newTranscriptMessage);
  64. return {
  65. ...state,
  66. _transcriptMessages: newTranscriptMessages
  67. };
  68. }