Skip to content

Commit

Permalink
feat(app-load-balancing): add start and stop polling and listen to st…
Browse files Browse the repository at this point in the history
…atus
  • Loading branch information
albertodigioacchino committed Jan 25, 2021
1 parent 2fcc81c commit 9e5c261
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,6 +43,8 @@ const GameView: React.FC<GameProps> = ({ zoomIn, zoomOut }) => {
setBackGround(s => !s);
}, []);

useStopPollingOnlineStatus();

return (
<ZoomPanContext initialPan={initialPan}>
<CardsGameListeners panelModeRef={panelModeRef} onEvent={onCardEvent} currentGameState={state}>
Expand Down
16 changes: 16 additions & 0 deletions packages/game-app/src/gameView/hooks/useStopPollingOnlineStatus.ts
Original file line number Diff line number Diff line change
@@ -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]);
}
4 changes: 3 additions & 1 deletion packages/game-app/src/loadBalancer/sagas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
stopListenToOnlineStatusSaga,
updateOnlineStatusSaga,
watchStatusChannel,
} from './updateOnlineStatus';
startPolling,
} from './loadBalancer';
import { all } from 'redux-saga/effects';

export default function* loadBalancerSaga() {
Expand All @@ -16,5 +17,6 @@ export default function* loadBalancerSaga() {
stopListenToOnlineStatusSaga(),
pollUpdateStatusSagaWatcher(),
watchStatusChannel(),
startPolling(),
]);
}
9 changes: 9 additions & 0 deletions packages/game-app/src/loadBalancer/sagas/loadBalancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,12 @@ function* executeInitializeRTDB(action: ReturnType<typeof loadGameActions.saveGa
export function* initializeRTDB() {
yield takeEvery(loadGameActions.saveGame, executeInitializeRTDB);
}

function* executeStartPolling(action: ReturnType<typeof actions.updateRTDB>) {
yield put(actions.startPollingOnlineStatus());
yield put(actions.startListenToOnlineStatus());
}

export function* startPolling() {
yield takeEvery(actions.updateRTDB, executeStartPolling);
}

0 comments on commit 9e5c261

Please sign in to comment.