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/gameBrowser.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/gameBrowser.ts')
-rw-r--r-- | frontend/src/sagas/gameBrowser.ts | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/frontend/src/sagas/gameBrowser.ts b/frontend/src/sagas/gameBrowser.ts new file mode 100644 index 00000000..868ec471 --- /dev/null +++ b/frontend/src/sagas/gameBrowser.ts @@ -0,0 +1,55 @@ +import { push } from 'react-router-redux'; +import { eventChannel, SagaIterator } from 'redux-saga'; +import { all, apply, call, put, take } from 'redux-saga/effects'; +import { ApiLobby } from '../api/model'; +import { SevenWondersSession } from '../api/sevenWondersApi'; +import { actions as gameActions, REQUEST_CREATE_GAME, REQUEST_JOIN_GAME } from '../redux/actions/lobby'; + +function* watchGames(session: SevenWondersSession): any { + const gamesChannel = yield eventChannel(session.watchGames()); + try { + while (true) { + const gameList = yield take(gamesChannel); + yield put(gameActions.updateGames(gameList)); + } + } finally { + yield apply(gamesChannel, gamesChannel.close); + } +} + +function* watchLobbyJoined(session: SevenWondersSession): any { + const joinedLobbyChannel = yield eventChannel(session.watchLobbyJoined()); + try { + const joinedLobby: ApiLobby = yield take(joinedLobbyChannel); + yield put(gameActions.updateGames([joinedLobby])); + yield put(gameActions.enterLobby(joinedLobby.id)); + yield put(push(`/lobby/${joinedLobby.id}`)); + } finally { + yield apply(joinedLobbyChannel, joinedLobbyChannel.close); + } +} + +function* createGame(session: SevenWondersSession): SagaIterator { + while (true) { + const { gameName } = yield take(REQUEST_CREATE_GAME); + // $FlowFixMe + yield apply(session, session.createGame, [gameName]); + } +} + +function* joinGame(session: SevenWondersSession): SagaIterator { + while (true) { + const { gameId } = yield take(REQUEST_JOIN_GAME); + // $FlowFixMe + yield apply(session, session.joinGame, [gameId]); + } +} + +export function* gameBrowserSaga(session: SevenWondersSession): SagaIterator { + yield all([ + call(watchGames, session), + call(watchLobbyJoined, session), + call(createGame, session), + call(joinGame, session), + ]); +} |