diff options
author | jbion <joffrey.bion@amadeus.com> | 2019-05-05 11:22:58 +0200 |
---|---|---|
committer | jbion <joffrey.bion@amadeus.com> | 2019-05-06 18:33:14 +0200 |
commit | 347877687301ec43367105a7f8c49fe16129fc00 (patch) | |
tree | 4999cf1978527658f70a21ce59d5f5d08a7169fd /frontend/src/sagas/lobby.ts | |
parent | Convert reducers to typescript (diff) | |
download | seven-wonders-347877687301ec43367105a7f8c49fe16129fc00.tar.gz seven-wonders-347877687301ec43367105a7f8c49fe16129fc00.tar.bz2 seven-wonders-347877687301ec43367105a7f8c49fe16129fc00.zip |
Convert redux sagas to TypeScript
Diffstat (limited to 'frontend/src/sagas/lobby.ts')
-rw-r--r-- | frontend/src/sagas/lobby.ts | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/frontend/src/sagas/lobby.ts b/frontend/src/sagas/lobby.ts new file mode 100644 index 00000000..09360b02 --- /dev/null +++ b/frontend/src/sagas/lobby.ts @@ -0,0 +1,44 @@ +import { push } from 'react-router-redux'; +import { Channel, eventChannel, SagaIterator } from 'redux-saga'; +import { all, apply, call, put, take } from 'redux-saga/effects'; +import { SevenWondersSession } from '../api/sevenWondersApi'; +import { actions as gameActions, ENTER_LOBBY, REQUEST_START_GAME } from '../redux/actions/lobby'; + +function* watchLobbyUpdates(session: SevenWondersSession, lobbyId: number): any { + const lobbyUpdatesChannel: Channel<Object> = yield eventChannel(session.watchLobbyUpdated(lobbyId)); + try { + while (true) { + const lobby = yield take(lobbyUpdatesChannel); + yield put(gameActions.updateGames([lobby])); + } + } finally { + yield apply(lobbyUpdatesChannel, lobbyUpdatesChannel.close); + } +} + +function* watchGameStart(session: SevenWondersSession, lobbyId: number): any { + const gameStartedChannel = yield eventChannel(session.watchGameStarted(lobbyId)); + try { + yield take(gameStartedChannel); + yield put(gameActions.enterGame(lobbyId)); + yield put(push(`/game/${lobbyId}`)); + } finally { + yield apply(gameStartedChannel, gameStartedChannel.close); + } +} + +function* startGame(session: SevenWondersSession): SagaIterator { + while (true) { + yield take(REQUEST_START_GAME); + yield apply(session, session.startGame); + } +} + +export function* lobbySaga(session: SevenWondersSession): SagaIterator { + const { gameId } = yield take(ENTER_LOBBY); + yield all([ + call(watchLobbyUpdates, session, gameId), + call(watchGameStart, session, gameId), + call(startGame, session) + ]); +} |