1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 12:56:41 +07:00

player: refactored sagas

This commit is contained in:
Fedor Katurov 2021-03-03 11:17:54 +07:00
parent 5102c738b3
commit 7d4df40963
4 changed files with 29 additions and 20 deletions

View file

@ -14,7 +14,12 @@ import { getEmbedYoutube } from './api';
import { selectPlayer } from './selectors';
function* setFileAndPlaySaga({ file }: ReturnType<typeof playerSetFile>) {
if (!file) {
return;
}
yield put(playerSetFile(file));
Player.set(getURL(file));
Player.play();
}
@ -37,7 +42,7 @@ function seekSaga({ seek }: ReturnType<typeof playerSeek>) {
function* stoppedSaga() {
yield put(playerSetStatus(PLAYER_STATES.UNSET));
yield put(playerSetFile(null));
yield put(playerSetFile(undefined));
}
function* getYoutubeInfo() {
@ -49,34 +54,38 @@ function* getYoutubeInfo() {
ticker,
}: { action: ReturnType<typeof playerGetYoutubeInfo>; ticker: any } = yield race({
action: take(PLAYER_ACTIONS.GET_YOUTUBE_INFO),
...(ids.length > 0 ? { ticker: delay(1000) } : {}),
...(ids.length > 0 ? { ticker: delay(500) } : {}),
});
if (action) {
ids.push(action.url);
}
if (ticker || ids.length > 25) {
const result: Unwrap<typeof getEmbedYoutube> = yield call(getEmbedYoutube, ids);
if (!ticker && ids.length <= 25) {
// Try to collect more items in next 500ms
continue;
}
if (!result.error && result.data.items && Object.keys(result.data.items).length) {
try {
const data: Unwrap<typeof getEmbedYoutube> = yield call(getEmbedYoutube, ids);
if (data.items && Object.keys(data.items).length) {
const { youtubes }: ReturnType<typeof selectPlayer> = yield select(selectPlayer);
yield put(playerSet({ youtubes: { ...youtubes, ...result.data.items } }));
yield put(playerSet({ youtubes: { ...youtubes, ...data.items } }));
}
ids = [];
}
} catch {}
}
}
export default function* playerSaga() {
yield fork(getYoutubeInfo);
yield takeLatest(PLAYER_ACTIONS.SET_FILE_AND_PLAY, setFileAndPlaySaga);
yield takeLatest(PLAYER_ACTIONS.PAUSE, pauseSaga);
yield takeLatest(PLAYER_ACTIONS.PLAY, playSaga);
yield takeLatest(PLAYER_ACTIONS.SEEK, seekSaga);
yield takeLatest(PLAYER_ACTIONS.STOP, stopSaga);
yield takeLatest(PLAYER_ACTIONS.STOPPED, stoppedSaga);
yield fork(getYoutubeInfo);
// yield takeEvery(PLAYER_ACTIONS.GET_YOUTUBE_INFO, getYoutubeInfo);
}