From 9e5c261aff9fb45e5c50bfccc65938299b6b06e2 Mon Sep 17 00:00:00 2001 From: Alberto Di Gioacchino Date: Fri, 22 Jan 2021 12:13:09 +0100 Subject: [PATCH] feat(app-load-balancing): add start and stop polling and listen to status --- .../gameView/components/GameView/GameView.tsx | 3 +++ .../gameView/hooks/useStopPollingOnlineStatus.ts | 16 ++++++++++++++++ .../game-app/src/loadBalancer/sagas/index.ts | 4 +++- .../src/loadBalancer/sagas/loadBalancer.ts | 9 +++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 packages/game-app/src/gameView/hooks/useStopPollingOnlineStatus.ts diff --git a/packages/game-app/src/gameView/components/GameView/GameView.tsx b/packages/game-app/src/gameView/components/GameView/GameView.tsx index 5a8a5285..85fc8a5d 100644 --- a/packages/game-app/src/gameView/components/GameView/GameView.tsx +++ b/packages/game-app/src/gameView/components/GameView/GameView.tsx @@ -13,6 +13,7 @@ import { PanelMode } from '../DeckPanel/DeckPanel'; import TopWidgetsRow from '../TopWidgetsRow'; import ZoomPanContainer from '../ZoomPanContainer'; import ZoomPanContext from '../ZoomPanContext'; +import useStopPollingOnlineStatus from '../../hooks/useStopPollingOnlineStatus'; type GameProps = { zoomIn: () => void; @@ -42,6 +43,8 @@ const GameView: React.FC = ({ zoomIn, zoomOut }) => { setBackGround(s => !s); }, []); + useStopPollingOnlineStatus(); + return ( diff --git a/packages/game-app/src/gameView/hooks/useStopPollingOnlineStatus.ts b/packages/game-app/src/gameView/hooks/useStopPollingOnlineStatus.ts new file mode 100644 index 00000000..d140e6ae --- /dev/null +++ b/packages/game-app/src/gameView/hooks/useStopPollingOnlineStatus.ts @@ -0,0 +1,16 @@ +import { batch, useDispatch } from 'react-redux'; +import { actions as loadBalancerActions } from '../../loadBalancer/slice'; +import { useEffect } from 'react'; + +export default function useStopPollingOnlineStatus() { + const dispatch = useDispatch(); + + useEffect(() => { + return () => { + batch(() => { + dispatch(loadBalancerActions.stopPollingOnlineStatus()); + dispatch(loadBalancerActions.stopListenToOnlineStatus()); + }); + }; + }, [dispatch]); +} diff --git a/packages/game-app/src/loadBalancer/sagas/index.ts b/packages/game-app/src/loadBalancer/sagas/index.ts index 3ec472ac..45e962a8 100644 --- a/packages/game-app/src/loadBalancer/sagas/index.ts +++ b/packages/game-app/src/loadBalancer/sagas/index.ts @@ -5,7 +5,8 @@ import { stopListenToOnlineStatusSaga, updateOnlineStatusSaga, watchStatusChannel, -} from './updateOnlineStatus'; + startPolling, +} from './loadBalancer'; import { all } from 'redux-saga/effects'; export default function* loadBalancerSaga() { @@ -16,5 +17,6 @@ export default function* loadBalancerSaga() { stopListenToOnlineStatusSaga(), pollUpdateStatusSagaWatcher(), watchStatusChannel(), + startPolling(), ]); } diff --git a/packages/game-app/src/loadBalancer/sagas/loadBalancer.ts b/packages/game-app/src/loadBalancer/sagas/loadBalancer.ts index 2fb62f04..838c7479 100644 --- a/packages/game-app/src/loadBalancer/sagas/loadBalancer.ts +++ b/packages/game-app/src/loadBalancer/sagas/loadBalancer.ts @@ -96,3 +96,12 @@ function* executeInitializeRTDB(action: ReturnType) { + yield put(actions.startPollingOnlineStatus()); + yield put(actions.startListenToOnlineStatus()); +} + +export function* startPolling() { + yield takeEvery(actions.updateRTDB, executeStartPolling); +}