UNPKG

playball

Version:

Watch MLB games from the comfort of your terminal

52 lines 2.19 kB
import React, { useEffect, useRef } from 'react'; import { useDispatch, useSelector } from "react-redux/lib/alternate-renderers.js"; import { fetchGame, selectGame, selectSelectedId, selectFullUpdateRequired, selectDelay } from "../features/games.js"; import PreviewGame from "./PreviewGame.js"; import LiveGame from "./LiveGame.js"; import FinishedGame from "./FinishedGame.js"; import log from "../logger.js"; function Game() { var _game$metaData, _game$gameData; const dispatch = useDispatch(); const game = useSelector(selectGame); const fullUpdateRequired = useSelector(selectFullUpdateRequired); const id = useSelector(selectSelectedId); const delay = useSelector(selectDelay); const timerRef = useRef(null); const timestampRef = useRef(); timestampRef.current = fullUpdateRequired ? null : game === null || game === void 0 || (_game$metaData = game.metaData) === null || _game$metaData === void 0 ? void 0 : _game$metaData.timeStamp; const updateGameData = () => { dispatch(fetchGame({ id, start: timestampRef.current, delay })).unwrap().then(result => { var _result$metaData; const wait = (result && ((_result$metaData = result.metaData) === null || _result$metaData === void 0 ? void 0 : _result$metaData.wait) || 10) * 1000; timerRef.current = setTimeout(updateGameData, wait); }).catch(err => log.error('UPDATE_GAME_DATA:\n' + JSON.stringify(err) + '\n' + err.stack)); }; useEffect(() => { updateGameData(); return () => { clearTimeout(timerRef.current); }; }, [id, delay]); if (!game) { return /*#__PURE__*/React.createElement("element", null); } let Wrapped = null; switch ((_game$gameData = game.gameData) === null || _game$gameData === void 0 || (_game$gameData = _game$gameData.status) === null || _game$gameData === void 0 ? void 0 : _game$gameData.abstractGameCode) { case 'P': Wrapped = PreviewGame; break; case 'L': Wrapped = LiveGame; break; case 'F': Wrapped = FinishedGame; break; } return /*#__PURE__*/React.createElement("element", null, /*#__PURE__*/React.createElement(Wrapped, null)); } export default Game;