diff options
Diffstat (limited to 'frontend/src/sagas')
-rw-r--r-- | frontend/src/sagas/errors.js | 17 | ||||
-rw-r--r-- | frontend/src/sagas/gameBrowser.js | 21 | ||||
-rw-r--r-- | frontend/src/sagas/home.js | 22 | ||||
-rw-r--r-- | frontend/src/sagas/lobby.js | 35 | ||||
-rw-r--r-- | frontend/src/sagas/utils.js | 10 |
5 files changed, 49 insertions, 56 deletions
diff --git a/frontend/src/sagas/errors.js b/frontend/src/sagas/errors.js index 86fa0124..eece98c8 100644 --- a/frontend/src/sagas/errors.js +++ b/frontend/src/sagas/errors.js @@ -1,10 +1,13 @@ -import {apply, cancelled, take} from 'redux-saga/effects'; -import {toastr} from 'react-redux-toastr'; -import {ApiError, SevenWondersSession} from '../api/sevenWondersApi'; -import type {Channel} from 'redux-saga'; +// @flow +import { toastr } from 'react-redux-toastr' +import type { Channel } from 'redux-saga' +import { eventChannel } from 'redux-saga' +import { apply, cancelled, take } from 'redux-saga/effects' +import type { ApiError } from '../api/model' +import type { SevenWondersSession } from '../api/sevenWondersApi' -export default function* errorHandlingSaga(session: SevenWondersSession) { - const errorChannel: Channel<ApiError> = yield apply(session, session.watchErrors, []); +export default function* errorHandlingSaga(session: SevenWondersSession): * { + const errorChannel: Channel<ApiError> = yield eventChannel(session.watchErrors()); try { while (true) { const error: ApiError = yield take(errorChannel); @@ -18,7 +21,7 @@ export default function* errorHandlingSaga(session: SevenWondersSession) { } } -function* handleOneError(err: ApiError) { +function* handleOneError(err: ApiError): * { console.error('Error received on web socket channel', err); const msg = buildMsg(err); yield apply(toastr, toastr.error, [msg, { icon: 'error' }]); diff --git a/frontend/src/sagas/gameBrowser.js b/frontend/src/sagas/gameBrowser.js index 871908f6..5b815f8d 100644 --- a/frontend/src/sagas/gameBrowser.js +++ b/frontend/src/sagas/gameBrowser.js @@ -1,17 +1,16 @@ // @flow -import { call, put, take, apply } from 'redux-saga/effects'; -import { push } from 'react-router-redux'; +import { normalize } from 'normalizr' +import { push } from 'react-router-redux' +import { eventChannel } from 'redux-saga' +import { apply, call, put, take } from 'redux-saga/effects' +import type { SevenWondersSession } from '../api/sevenWondersApi' -import { normalize } from 'normalizr'; -import { game as gameSchema, gameList as gameListSchema } from '../schemas/games'; - -import { actions as gameActions, types } from '../redux/games'; -import { actions as playerActions } from '../redux/players'; -import type { SevenWondersSession } from '../api/sevenWondersApi'; -import { createChannel } from './utils'; +import { actions as gameActions, types } from '../redux/games' +import { actions as playerActions } from '../redux/players' +import { game as gameSchema, gameList as gameListSchema } from '../schemas/games' function* watchGames(session: SevenWondersSession): * { - const gamesChannel = yield createChannel(session, session.watchGames); + const gamesChannel = yield eventChannel(session.watchGames()); try { while (true) { const gameList = yield take(gamesChannel); @@ -26,7 +25,7 @@ function* watchGames(session: SevenWondersSession): * { } function* watchLobbyJoined(session: SevenWondersSession): * { - const joinedLobbyChannel = yield createChannel(session, session.watchLobbyJoined); + const joinedLobbyChannel = yield eventChannel(session.watchLobbyJoined()); try { const joinedLobby = yield take(joinedLobbyChannel); const normalized = normalize(joinedLobby, gameSchema); diff --git a/frontend/src/sagas/home.js b/frontend/src/sagas/home.js index b51bf4dc..0b30f784 100644 --- a/frontend/src/sagas/home.js +++ b/frontend/src/sagas/home.js @@ -1,28 +1,30 @@ -import { apply, call, put, take } from 'redux-saga/effects'; -import { push } from 'react-router-redux'; +// @flow +import { apply, call, put, take } from 'redux-saga/effects' +import { push } from 'react-router-redux' +import { eventChannel } from 'redux-saga' -import { actions, types } from '../redux/players'; -import type { SevenWondersSession } from '../api/sevenWondersApi'; -import { createChannel } from './utils'; +import { actions, types } from '../redux/players' +import type { SevenWondersSession } from '../api/sevenWondersApi' +import type { ApiPlayer } from '../api/model' -function* sendUsername(session: SevenWondersSession) { +function* sendUsername(session: SevenWondersSession): * { while (true) { const { username } = yield take(types.REQUEST_CHOOSE_USERNAME); yield apply(session, session.chooseName, [username]); } } -function* validateUsername(session: SevenWondersSession) { - const usernameChannel = yield createChannel(session, session.watchNameChoice); +function* validateUsername(session: SevenWondersSession): * { + const usernameChannel = yield eventChannel(session.watchNameChoice()); while (true) { - const user = yield take(usernameChannel); + const user: ApiPlayer = yield take(usernameChannel); yield put(actions.setCurrentPlayer(user)); yield apply(usernameChannel, usernameChannel.close); yield put(push('/games')); } } -function* homeSaga(session: SevenWondersSession) { +function* homeSaga(session: SevenWondersSession): * { yield [call(sendUsername, session), call(validateUsername, session)]; } diff --git a/frontend/src/sagas/lobby.js b/frontend/src/sagas/lobby.js index cc704086..93e0960f 100644 --- a/frontend/src/sagas/lobby.js +++ b/frontend/src/sagas/lobby.js @@ -1,24 +1,23 @@ -import { call, put, take, apply } from 'redux-saga/effects'; -import type { Channel } from 'redux-saga'; - -import { push } from 'react-router-redux'; - -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 { SevenWondersSession } from '../api/sevenWondersApi'; -import { createChannel } from './utils'; +// @flow +import { normalize } from 'normalizr' +import { push } from 'react-router-redux' +import type { Channel } from 'redux-saga' +import { eventChannel } from 'redux-saga' +import { apply, call, put, take } from 'redux-saga/effects' +import { SevenWondersSession } from '../api/sevenWondersApi' +import { actions as gameActions, types } from '../redux/games' +import { actions as playerActions } from '../redux/players' + +import { game as gameSchema } from '../schemas/games' function getCurrentGameId(): number { const path = window.location.pathname; return path.split('lobby/')[1]; } -function* watchLobbyUpdates(session: SevenWondersSession) { +function* watchLobbyUpdates(session: SevenWondersSession): * { const currentGameId: number = getCurrentGameId(); - const lobbyUpdatesChannel: Channel = yield createChannel(session, session.watchLobbyUpdated, currentGameId); + const lobbyUpdatesChannel: Channel = yield eventChannel(session.watchLobbyUpdated(currentGameId)); try { while (true) { const lobby = yield take(lobbyUpdatesChannel); @@ -31,9 +30,9 @@ function* watchLobbyUpdates(session: SevenWondersSession) { } } -function* watchGameStart(session: SevenWondersSession) { +function* watchGameStart(session: SevenWondersSession): * { const currentGameId = getCurrentGameId(); - const gameStartedChannel = yield createChannel(session, session.watchGameStarted, currentGameId); + const gameStartedChannel = yield eventChannel(session.watchGameStarted(currentGameId)); try { yield take(gameStartedChannel); yield put(gameActions.enterGame()); @@ -43,14 +42,14 @@ function* watchGameStart(session: SevenWondersSession) { } } -function* startGame(session: SevenWondersSession) { +function* startGame(session: SevenWondersSession): * { while (true) { yield take(types.REQUEST_START_GAME); yield apply(session, session.startGame, []); } } -function* lobbySaga(session: SevenWondersSession) { +function* lobbySaga(session: SevenWondersSession): * { yield [call(watchLobbyUpdates, session), call(watchGameStart, session), call(startGame, session)]; } diff --git a/frontend/src/sagas/utils.js b/frontend/src/sagas/utils.js deleted file mode 100644 index 28017c87..00000000 --- a/frontend/src/sagas/utils.js +++ /dev/null @@ -1,10 +0,0 @@ -import { SevenWondersSession } from '../api/sevenWondersApi'; -import { eventChannel } from 'redux-saga'; - -type Emitter = (value: any) => void; - -export function createChannel(session: SevenWondersSession, methodWithCallback: () => void, ...args: Array<any>) { - return eventChannel((emitter: Emitter) => { - return methodWithCallback.call(session, ...args, emitter); - }); -} |