diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/js/src/containers/App/saga.js | 20 | ||||
-rw-r--r-- | src/main/js/src/containers/GameBrowser/saga.js | 21 | ||||
-rw-r--r-- | src/main/js/src/sagas.js | 24 | ||||
-rw-r--r-- | src/main/js/src/utils/createWebSocketConnection.js | 13 |
4 files changed, 37 insertions, 41 deletions
diff --git a/src/main/js/src/containers/App/saga.js b/src/main/js/src/containers/App/saga.js index 4ccf6019..f1f72dfd 100644 --- a/src/main/js/src/containers/App/saga.js +++ b/src/main/js/src/containers/App/saga.js @@ -1,6 +1,5 @@ -import { call, put, take } from 'redux-saga/effects' +import { put, take } from 'redux-saga/effects' import { eventChannel } from 'redux-saga' -import createWebSocketConnection from "../../utils/createWebSocketConnection"; function createSocketChannel(socket) { return eventChannel(emit => { @@ -16,18 +15,9 @@ function createSocketChannel(socket) { }) } -export function* watchOnErrors() { - let socketChannel - try { - const { socket } = yield call(createWebSocketConnection) - socketChannel = createSocketChannel(socket) - } catch (error) { - console.error('Error connecting to socket', error) - } - - if (!socketChannel) { - return - } +export function* watchOnErrors(socketConnection) { + const { socket } = socketConnection + const socketChannel = createSocketChannel(socket) while (true) { const payload = yield take(socketChannel) @@ -35,4 +25,4 @@ export function* watchOnErrors() { } } -export default watchOnErrors
\ No newline at end of file +export default watchOnErrors diff --git a/src/main/js/src/containers/GameBrowser/saga.js b/src/main/js/src/containers/GameBrowser/saga.js index f09b5932..66d26e38 100644 --- a/src/main/js/src/containers/GameBrowser/saga.js +++ b/src/main/js/src/containers/GameBrowser/saga.js @@ -1,6 +1,5 @@ -import { call, put, take } from 'redux-saga/effects' +import { put, take } from 'redux-saga/effects' import { eventChannel } from 'redux-saga' -import createWebSocketConnection from '../../utils/createWebSocketConnection' import { NEW_GAME, JOIN_GAME } from './constants' import { newGame, joinGame } from './actions' @@ -30,22 +29,14 @@ function createSocketChannel(socket) { }) } -export function* watchOnNewGames() { - let socketChannel - try { - const { socket } = yield call(createWebSocketConnection) - socketChannel = createSocketChannel(socket) - } catch (error) { - console.error('Cannot connect to socket', error) - } +export function* watchOnNewGames(socketConnection) { - if (!socketChannel) { - return - } + const { socket } = socketConnection + const socketChannel = createSocketChannel(socket) while (true) { const { type, response } = yield take(socketChannel) - console.info('RESPONSE', response) + switch (type) { case NEW_GAME: yield put(newGame(response)) @@ -54,7 +45,7 @@ export function* watchOnNewGames() { yield put(joinGame(response)) break; default: - console.error('Unknown error') + console.error('Unknown type') } } } diff --git a/src/main/js/src/sagas.js b/src/main/js/src/sagas.js index 4b8b057b..92235434 100644 --- a/src/main/js/src/sagas.js +++ b/src/main/js/src/sagas.js @@ -1,11 +1,27 @@ -import { fork } from 'redux-saga/effects' +import { fork, call } from 'redux-saga/effects' + +import createWsConnection from './utils/createWebSocketConnection' import counterSaga from './containers/Counter/saga' import errorSaga from './containers/App/saga' import newGamesSaga from './containers/GameBrowser/saga' +function* wsAwareSagas() { + let wsConnection + try { + wsConnection = yield call(createWsConnection) + } catch (error) { + console.error('Could not connect to socket') + return + } + + yield fork(errorSaga, wsConnection) + yield fork(newGamesSaga, wsConnection) +} + export default function* rootSaga() { - yield fork(counterSaga) - yield fork(errorSaga) - yield fork(newGamesSaga) + yield [ + call(counterSaga), + call(wsAwareSagas) + ] } diff --git a/src/main/js/src/utils/createWebSocketConnection.js b/src/main/js/src/utils/createWebSocketConnection.js index 00eb7ba0..200bedbe 100644 --- a/src/main/js/src/utils/createWebSocketConnection.js +++ b/src/main/js/src/utils/createWebSocketConnection.js @@ -2,12 +2,11 @@ import SockJS from 'sockjs-client' import Stomp from 'webstomp-client' const wsURL = 'http://localhost:8080/seven-wonders-websocket' -let socket = null -export default () => { - return new Promise((resolve, reject) => { - socket = Stomp.over(new SockJS(wsURL)) - socket.connect({}, (frame) => { +const createConnection = (headers = {}) => new Promise((resolve, reject) => { + let socket = Stomp.over(new SockJS(wsURL)) + socket.connect(headers, (frame) => { return resolve({ frame, socket }) }, reject) - }) -} +}) + +export default createConnection |