2018-07-17 17:31:12 +00:00
|
|
|
import { ReducerRegistry } from '../base/redux';
|
|
|
|
|
|
|
|
import {
|
2018-08-22 17:49:58 +00:00
|
|
|
REMOVE_TRANSCRIPT_MESSAGE, TOGGLE_REQUESTING_SUBTITLES,
|
2018-07-17 17:31:12 +00:00
|
|
|
UPDATE_TRANSCRIPT_MESSAGE
|
|
|
|
} from './actionTypes';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default State for 'features/transcription' feature
|
|
|
|
*/
|
|
|
|
const defaultState = {
|
2018-08-22 17:49:58 +00:00
|
|
|
_transcriptMessages: new Map(),
|
|
|
|
_requestingSubtitles: false
|
2018-07-17 17:31:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen for actions for the transcription feature to be used by the actions
|
|
|
|
* to update the rendered transcription subtitles.
|
|
|
|
*/
|
|
|
|
ReducerRegistry.register('features/subtitles', (
|
|
|
|
state = defaultState, action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case REMOVE_TRANSCRIPT_MESSAGE:
|
|
|
|
return _removeTranscriptMessage(state, action);
|
|
|
|
case UPDATE_TRANSCRIPT_MESSAGE:
|
|
|
|
return _updateTranscriptMessage(state, action);
|
2018-08-22 17:49:58 +00:00
|
|
|
|
|
|
|
case TOGGLE_REQUESTING_SUBTITLES:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
_requestingSubtitles: !state._requestingSubtitles
|
|
|
|
};
|
2018-07-17 17:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reduces a specific Redux action REMOVE_TRANSCRIPT_MESSAGE of the feature
|
|
|
|
* transcription.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The Redux state of the feature transcription.
|
|
|
|
* @param {Action} action -The Redux action REMOVE_TRANSCRIPT_MESSAGE to reduce.
|
|
|
|
* @returns {Object} The new state of the feature transcription after the
|
|
|
|
* reduction of the specified action.
|
|
|
|
*/
|
|
|
|
function _removeTranscriptMessage(state, { transcriptMessageID }) {
|
2018-09-11 18:08:15 +00:00
|
|
|
const newTranscriptMessages = new Map(state._transcriptMessages);
|
2018-07-17 17:31:12 +00:00
|
|
|
|
|
|
|
// Deletes the key from Map once a final message arrives.
|
|
|
|
newTranscriptMessages.delete(transcriptMessageID);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
2018-08-22 17:49:58 +00:00
|
|
|
_transcriptMessages: newTranscriptMessages
|
2018-07-17 17:31:12 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reduces a specific Redux action UPDATE_TRANSCRIPT_MESSAGE of the feature
|
|
|
|
* transcription.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The Redux state of the feature transcription.
|
|
|
|
* @param {Action} action -The Redux action UPDATE_TRANSCRIPT_MESSAGE to reduce.
|
|
|
|
* @returns {Object} The new state of the feature transcription after the
|
|
|
|
* reduction of the specified action.
|
|
|
|
*/
|
|
|
|
function _updateTranscriptMessage(state,
|
|
|
|
{ transcriptMessageID, newTranscriptMessage }) {
|
2018-09-11 18:08:15 +00:00
|
|
|
const newTranscriptMessages = new Map(state._transcriptMessages);
|
2018-07-17 17:31:12 +00:00
|
|
|
|
|
|
|
// Updates the new message for the given key in the Map.
|
|
|
|
newTranscriptMessages.set(transcriptMessageID, newTranscriptMessage);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
2018-08-22 17:49:58 +00:00
|
|
|
_transcriptMessages: newTranscriptMessages
|
2018-07-17 17:31:12 +00:00
|
|
|
};
|
|
|
|
}
|