import { updateCurrentItem } from './utils' ; import { getPersistentOptions } from './options' ; import { put , select , takeLatest } from 'redux-saga/effects' ; import { ActionTypes as PlayerActionTypes } from '../player/actionTypes' ; import { ActionTypes } from "./actionTypes" ; import { AppStore } from "../store" ; import { Bundle } from "../linker" ; export default function ( bundle ) { bundle . addReducer ( PlayerActionTypes . PlayerReady , playerReadyReducer ) ; bundle . addReducer ( PlayerActionTypes . PlayerSeeked , playerSeekedReducer ) ; bundle . addReducer ( PlayerActionTypes . PlayerTick , playerTickReducer ) ; bundle . addSaga ( function * ( ) { yield takeLatest ( PlayerActionTypes . PlayerReady , function * ( ) { const { language } = getPersistentOptions ( ) ; const state = yield select ( ) ; const { availableOptions } = state . subtitles ; if ( language && language !== "none" && language in availableOptions ) { const option = availableOptions [ language ] ; yield put ( { type : ActionTypes . SubtitlesLoadFromUrl , payload : option } ) ; } } ) ; } ) ; } function playerReadyReducer ( state , { payload : { baseDataUrl , data } } ) { const availableOptions = { } ; const { langOptions } = state . subtitles ; ( data . subtitles || [ ] ) . forEach ( function ( key ) { const url = `${ baseDataUrl }_${ key }.srt` ; const option = langOptions . find ( option => option . value === key ) ; availableOptions [ key ] = { key , url , ... option } ; } ) ; state . subtitles . availableOptions = availableOptions ; state . subtitles . items = [ ] ; state . subtitles . filteredItems = [ ] ; state . subtitles . currentIndex = 0 ; state . subtitles . loadedKey = 'none' ; } function playerSeekedReducer ( state , action ) { const { seekTo } = action ; updateCurrentItem ( state . subtitles , Math . round ( seekTo ) ) ; } function playerTickReducer ( state , { payload : { audioTime } } ) { return updateCurrentItem ( state . subtitles , Math . round ( audioTime ) ) ; }