Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

reducer.ts 2.9KB

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