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 3.3KB

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