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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { ReducerRegistry } from '../base/redux';
  2. import {
  3. REMOVE_TRANSCRIPT_MESSAGE, TOGGLE_REQUESTING_SUBTITLES,
  4. SET_REQUESTING_SUBTITLES, 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. case SET_REQUESTING_SUBTITLES:
  30. return {
  31. ...state,
  32. _requestingSubtitles: action.enabled
  33. };
  34. }
  35. return state;
  36. });
  37. /**
  38. * Reduces a specific Redux action REMOVE_TRANSCRIPT_MESSAGE of the feature
  39. * transcription.
  40. *
  41. * @param {Object} state - The Redux state of the feature transcription.
  42. * @param {Action} action -The Redux action REMOVE_TRANSCRIPT_MESSAGE to reduce.
  43. * @returns {Object} The new state of the feature transcription after the
  44. * reduction of the specified action.
  45. */
  46. function _removeTranscriptMessage(state, { transcriptMessageID }) {
  47. const newTranscriptMessages = new Map(state._transcriptMessages);
  48. // Deletes the key from Map once a final message arrives.
  49. newTranscriptMessages.delete(transcriptMessageID);
  50. return {
  51. ...state,
  52. _transcriptMessages: newTranscriptMessages
  53. };
  54. }
  55. /**
  56. * Reduces a specific Redux action UPDATE_TRANSCRIPT_MESSAGE of the feature
  57. * transcription.
  58. *
  59. * @param {Object} state - The Redux state of the feature transcription.
  60. * @param {Action} action -The Redux action UPDATE_TRANSCRIPT_MESSAGE to reduce.
  61. * @returns {Object} The new state of the feature transcription after the
  62. * reduction of the specified action.
  63. */
  64. function _updateTranscriptMessage(state,
  65. { transcriptMessageID, newTranscriptMessage }) {
  66. const newTranscriptMessages = new Map(state._transcriptMessages);
  67. // Updates the new message for the given key in the Map.
  68. newTranscriptMessages.set(transcriptMessageID, newTranscriptMessage);
  69. return {
  70. ...state,
  71. _transcriptMessages: newTranscriptMessages
  72. };
  73. }