mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-24 20:36:40 +07:00
player: refactored sagas
This commit is contained in:
parent
5102c738b3
commit
7d4df40963
4 changed files with 29 additions and 20 deletions
|
@ -1,11 +1,8 @@
|
|||
import { IResultWithStatus, IEmbed } from '../types';
|
||||
import { api, resultMiddleware, errorMiddleware } from '~/utils/api';
|
||||
import { api, cleanResult } from '~/utils/api';
|
||||
import { API } from '~/constants/api';
|
||||
import { ApiGetEmbedYoutubeResult } from '~/redux/player/types';
|
||||
|
||||
export const getEmbedYoutube = (
|
||||
ids: string[]
|
||||
): Promise<IResultWithStatus<{ items: Record<string, IEmbed> }>> =>
|
||||
export const getEmbedYoutube = (ids: string[]) =>
|
||||
api
|
||||
.get(API.EMBED.YOUTUBE, { params: { ids: ids.join(',') } })
|
||||
.then(resultMiddleware)
|
||||
.catch(errorMiddleware);
|
||||
.get<ApiGetEmbedYoutubeResult>(API.EMBED.YOUTUBE, { params: { ids: ids.join(',') } })
|
||||
.then(cleanResult);
|
||||
|
|
|
@ -5,13 +5,13 @@ import { IFile, IEmbed } from '../types';
|
|||
|
||||
export type IPlayerState = Readonly<{
|
||||
status: typeof PLAYER_STATES[keyof typeof PLAYER_STATES];
|
||||
file: IFile;
|
||||
file?: IFile;
|
||||
youtubes: Record<string, IEmbed>;
|
||||
}>;
|
||||
|
||||
const INITIAL_STATE: IPlayerState = {
|
||||
status: PLAYER_STATES.UNSET,
|
||||
file: null,
|
||||
file: undefined,
|
||||
youtubes: {},
|
||||
};
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
3
src/redux/player/types.ts
Normal file
3
src/redux/player/types.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
import { IEmbed } from '~/redux/types';
|
||||
|
||||
export type ApiGetEmbedYoutubeResult = { items: Record<string, IEmbed> };
|
Loading…
Add table
Add a link
Reference in a new issue