player related action moved to usePlayer

This commit is contained in:
nklhrstv 2020-03-31 13:53:27 +03:00
parent 816ee24690
commit 43ab0745b5
2 changed files with 25 additions and 21 deletions

View file

@ -16,7 +16,7 @@ const styles = require('./styles');
const Player = ({ urlParams }) => {
const { core } = useServices();
const player = usePlayer(urlParams);
const [player, updateLibraryItemState, pushToLibrary] = usePlayer(urlParams);
const [settings, updateSettings] = useSettings();
const routeFocused = useRouteFocused();
const toast = useToast();
@ -219,29 +219,13 @@ const Player = ({ urlParams }) => {
}, [settings.subtitles_offset]);
React.useEffect(() => {
if (videoState.time !== null && !isNaN(videoState.time) && videoState.duration !== null && !isNaN(videoState.duration)) {
core.dispatch({
action: 'Player',
args: {
action: 'UpdateLibraryItemState',
args: {
time: videoState.time,
duration: videoState.duration
}
}
}, 'player');
updateLibraryItemState(videoState.time, videoState.duration);
}
}, [videoState.time, videoState.duration]);
React.useEffect(() => {
const interval = setInterval(() => {
core.dispatch({
action: 'Player',
args: {
action: 'PushToLibrary'
}
}, 'player');
}, 30000);
const intervalId = setInterval(pushToLibrary, 30000);
return () => {
clearInterval(interval);
clearInterval(intervalId);
};
}, []);
React.useLayoutEffect(() => {

View file

@ -1,5 +1,6 @@
const React = require('react');
const pako = require('pako');
const { useServices } = require('stremio/services');
const { useModelState } = require('stremio/common');
const initPlayerState = () => ({
@ -82,6 +83,7 @@ const mapPlayerStateWithCtx = (player, ctx) => {
};
const usePlayer = (urlParams) => {
const { core } = useServices();
const loadPlayerAction = React.useMemo(() => {
try {
return {
@ -133,12 +135,30 @@ const usePlayer = (urlParams) => {
};
}
}, [urlParams]);
return useModelState({
const updateLibraryItemState = React.useCallback((time, duration) => {
core.dispatch({
action: 'Player',
args: {
action: 'UpdateLibraryItemState',
args: { time, duration }
}
}, 'player');
}, []);
const pushToLibrary = React.useCallback(() => {
core.dispatch({
action: 'Player',
args: {
action: 'PushToLibrary'
}
}, 'player');
}, []);
const player = useModelState({
model: 'player',
action: loadPlayerAction,
init: initPlayerState,
mapWithCtx: mapPlayerStateWithCtx
});
return [player, updateLibraryItemState, pushToLibrary];
};
module.exports = usePlayer;