diff options
Diffstat (limited to 'frontend/src/sagas')
-rw-r--r-- | frontend/src/sagas/errors.js | 38 | ||||
-rw-r--r-- | frontend/src/sagas/home.js | 41 | ||||
-rw-r--r-- | frontend/src/sagas/lobby.js | 68 |
3 files changed, 83 insertions, 64 deletions
diff --git a/frontend/src/sagas/errors.js b/frontend/src/sagas/errors.js index ba1ae40d..42994610 100644 --- a/frontend/src/sagas/errors.js +++ b/frontend/src/sagas/errors.js @@ -1,36 +1,40 @@ -import { apply, call, cancelled, put, take } from 'redux-saga/effects' +import { apply, call, cancelled, put, take } from "redux-saga/effects"; -import { createSubscriptionChannel } from '../utils/websocket' -import { actions } from '../redux/errors' +import { createSubscriptionChannel } from "../utils/websocket"; +import { actions } from "../redux/errors"; -import {toastr} from 'react-redux-toastr' +import { toastr } from "react-redux-toastr"; -export default function *errorHandlingSaga({ socket }) { - const errorChannel = yield call(createSubscriptionChannel, socket, '/user/queue/errors') +export default function* errorHandlingSaga({ socket }) { + const errorChannel = yield call( + createSubscriptionChannel, + socket, + "/user/queue/errors" + ); try { while (true) { - const error = yield take(errorChannel) - yield* handleOneError(error) + const error = yield take(errorChannel); + yield* handleOneError(error); } } finally { if (yield cancelled()) { - console.log('Error management saga cancelled') - yield apply(errorChannel, errorChannel.close) + console.log("Error management saga cancelled"); + yield apply(errorChannel, errorChannel.close); } } } -function *handleOneError(err) { - console.error("Error received on web socket channel", err) - const msg = buildMsg(err) - yield apply(toastr, toastr.error, [msg, {icon: 'error'}]) - yield put(actions.errorReceived(err)) +function* handleOneError(err) { + console.error("Error received on web socket channel", err); + const msg = buildMsg(err); + yield apply(toastr, toastr.error, [msg, { icon: "error" }]); + yield put(actions.errorReceived(err)); } function buildMsg(err) { if (err.details.length > 0) { - return err.details.map(d => d.message).join('\n') + return err.details.map(d => d.message).join("\n"); } else { - return err.message + return err.message; } } diff --git a/frontend/src/sagas/home.js b/frontend/src/sagas/home.js index 151fcb57..15f95162 100644 --- a/frontend/src/sagas/home.js +++ b/frontend/src/sagas/home.js @@ -1,32 +1,39 @@ -import { apply, call, put, take } from 'redux-saga/effects' -import { createSubscriptionChannel } from '../utils/websocket' -import { push } from 'react-router-redux' +import { apply, call, put, take } from "redux-saga/effects"; +import { createSubscriptionChannel } from "../utils/websocket"; +import { push } from "react-router-redux"; -import { actions, types } from '../redux/players' +import { actions, types } from "../redux/players"; -function *sendUsername({ socket }) { +function* sendUsername({ socket }) { while (true) { - const {username} = yield take(types.REQUEST_CHOOSE_USERNAME) - yield apply(socket, socket.send, ['/app/chooseName', JSON.stringify({playerName: username})]) + const { username } = yield take(types.REQUEST_CHOOSE_USERNAME); + yield apply(socket, socket.send, [ + "/app/chooseName", + JSON.stringify({ playerName: username }) + ]); } } -function *validateUsername({ socket }) { - const usernameChannel = yield call(createSubscriptionChannel, socket, '/user/queue/nameChoice') +function* validateUsername({ socket }) { + const usernameChannel = yield call( + createSubscriptionChannel, + socket, + "/user/queue/nameChoice" + ); while (true) { - const user = yield take(usernameChannel) - yield put(actions.setCurrentPlayer(user)) - yield apply(usernameChannel, usernameChannel.close) - yield put(push('/games')) + const user = yield take(usernameChannel); + yield put(actions.setCurrentPlayer(user)); + yield apply(usernameChannel, usernameChannel.close); + yield put(push("/games")); } } -function *usernameChoiceSaga(wsConnection) { +function* usernameChoiceSaga(wsConnection) { // TODO: Run sendUsername in loop when we have the ability to cancel saga on route change yield [ call(sendUsername, wsConnection), - call(validateUsername, wsConnection), - ] + call(validateUsername, wsConnection) + ]; } -export default usernameChoiceSaga +export default usernameChoiceSaga; diff --git a/frontend/src/sagas/lobby.js b/frontend/src/sagas/lobby.js index f002c897..f092fdb7 100644 --- a/frontend/src/sagas/lobby.js +++ b/frontend/src/sagas/lobby.js @@ -1,58 +1,66 @@ -import { call, put, take, apply } from 'redux-saga/effects' -import { createSubscriptionChannel } from '../utils/websocket' -import { push } from 'react-router-redux' +import { call, put, take, apply } from "redux-saga/effects"; +import { createSubscriptionChannel } from "../utils/websocket"; +import { push } from "react-router-redux"; -import { normalize } from 'normalizr' -import { game as gameSchema } from '../schemas/games' +import { normalize } from "normalizr"; +import { game as gameSchema } from "../schemas/games"; -import { actions as gameActions, types } from '../redux/games' -import { actions as playerActions } from '../redux/players' +import { actions as gameActions, types } from "../redux/games"; +import { actions as playerActions } from "../redux/players"; function getCurrentGameId() { - const path = window.location.pathname - return path.split('lobby/')[1] + const path = window.location.pathname; + return path.split("lobby/")[1]; } -function *watchLobbyUpdates({ socket }) { - const currentGameId = getCurrentGameId() - const lobbyUpdatesChannel = yield call(createSubscriptionChannel, socket, `/topic/lobby/${currentGameId}/updated`) +function* watchLobbyUpdates({ socket }) { + const currentGameId = getCurrentGameId(); + const lobbyUpdatesChannel = yield call( + createSubscriptionChannel, + socket, + `/topic/lobby/${currentGameId}/updated` + ); try { while (true) { - const lobby = yield take(lobbyUpdatesChannel) - const normalized = normalize(lobby, gameSchema) - yield put(gameActions.updateGames(normalized.entities.games)) - yield put(playerActions.updatePlayers(normalized.entities.players)) + const lobby = yield take(lobbyUpdatesChannel); + const normalized = normalize(lobby, gameSchema); + yield put(gameActions.updateGames(normalized.entities.games)); + yield put(playerActions.updatePlayers(normalized.entities.players)); } } finally { - yield apply(lobbyUpdatesChannel, lobbyUpdatesChannel.close) + yield apply(lobbyUpdatesChannel, lobbyUpdatesChannel.close); } } -function *watchGameStart({ socket }) { - const currentGameId = getCurrentGameId() - const gameStartedChannel = yield call(createSubscriptionChannel, socket, `/topic/lobby/${currentGameId}/started`) +function* watchGameStart({ socket }) { + const currentGameId = getCurrentGameId(); + const gameStartedChannel = yield call( + createSubscriptionChannel, + socket, + `/topic/lobby/${currentGameId}/started` + ); try { - yield take(gameStartedChannel) - yield put(gameActions.enterGame()) - yield put(push('/game')) + yield take(gameStartedChannel); + yield put(gameActions.enterGame()); + yield put(push("/game")); } finally { - yield apply(gameStartedChannel, gameStartedChannel.close) + yield apply(gameStartedChannel, gameStartedChannel.close); } } -function *startGame({ socket }) { +function* startGame({ socket }) { while (true) { - yield take(types.REQUEST_START_GAME) - yield apply(socket, socket.send, ['/app/lobby/startGame', {}]) + yield take(types.REQUEST_START_GAME); + yield apply(socket, socket.send, ["/app/lobby/startGame", {}]); } } -function *lobbySaga(socketConnection) { +function* lobbySaga(socketConnection) { yield [ call(watchLobbyUpdates, socketConnection), call(watchGameStart, socketConnection), call(startGame, socketConnection) - ] + ]; } -export default lobbySaga +export default lobbySaga; |