dispatch commands to video refactored

This commit is contained in:
nklhrstv 2020-05-27 15:58:51 +03:00
parent c7f9543932
commit d667bee547
2 changed files with 19 additions and 25 deletions

View file

@ -16,16 +16,14 @@ const Video = React.forwardRef(({ className, ...props }, ref) => {
const onImplementationChangedRef = useLiveRef(props.onImplementationChanged);
const videoElementRef = React.useRef(null);
const videoRef = React.useRef(null);
const dispatch = React.useCallback((args) => {
if (args && args.commandName === 'load' && args.commandArgs) {
const Video = selectVideoImplementation(args.commandArgs.shell, args.commandArgs.stream);
if (typeof Video !== 'function') {
videoRef.current = null;
} else if (videoRef.current === null || videoRef.current.constructor !== Video) {
dispatch({ commandName: 'destroy' });
const dispatch = React.useCallback((action) => {
if (action && action.type === 'command' && action.commandName === 'load' && action.commandArgs) {
const Video = selectVideoImplementation(action.commandArgs);
if (videoRef.current === null || videoRef.current.constructor !== Video) {
dispatch({ type: 'command', commandName: 'destroy' });
videoRef.current = new Video({
containerElement: videoElementRef.current,
shell: args.commandArgs.shell
...action.commandArgs,
containerElement: videoElementRef.current
});
videoRef.current.on('ended', () => {
if (typeof onEndedRef.current === 'function') {
@ -60,17 +58,17 @@ const Video = React.forwardRef(({ className, ...props }, ref) => {
if (videoRef.current !== null) {
try {
videoRef.current.dispatch(args);
} catch (e) {
videoRef.current.dispatch(action);
} catch (error) {
// eslint-disable-next-line no-console
console.error(videoRef.current.constructor.manifest.name, e);
console.error(videoRef.current.constructor.manifest.name, error);
}
}
}, []);
React.useImperativeHandle(ref, () => ({ dispatch }), []);
React.useEffect(() => {
return () => {
dispatch({ commandName: 'destroy' });
dispatch({ type: 'command', commandName: 'destroy' });
};
}, []);
return (

View file

@ -1,25 +1,21 @@
// Copyright (C) 2017-2020 Smart code 203358507
const { HTMLVideo, YouTubeVideo, MPVVideo, withStreamingServer, withHTMLSubtitles } = require('stremio-video');
const { ChromecastVideo, HTMLVideo, YouTubeVideo, withStreamingServer, withHTMLSubtitles } = require('stremio-video');
const selectVideoImplementation = (shell, stream) => {
const selectVideoImplementation = (args) => {
// TODO handle stream.behaviorHints
// TODO handle IFrameVideo
// TODO handle ChromecastVideo
// TODO handle MPVVideo
if (shell) {
return withHTMLSubtitles(withStreamingServer(MPVVideo));
if (args.chromecastTransport && args.chromecastTransport.getSessionState() === cast.framework.SessionState.SESSION_STARTED) {
return ChromecastVideo;
}
if (stream) {
if (typeof stream.ytId === 'string') {
return withHTMLSubtitles(YouTubeVideo);
} else if (typeof stream.url === 'string' || typeof stream.infoHash === 'string') {
return withHTMLSubtitles(withStreamingServer(HTMLVideo));
}
if (args.stream && typeof args.stream.ytId === 'string') {
return withHTMLSubtitles(YouTubeVideo);
}
return null;
return withHTMLSubtitles(withStreamingServer(HTMLVideo));
};
module.exports = selectVideoImplementation;