From 267a122951a1959dcbd111a77dccdcb78eccefa4 Mon Sep 17 00:00:00 2001 From: Victor Chabbert Date: Sat, 21 Jan 2017 12:30:07 +0100 Subject: Better implementation of username choice --- frontend/src/sagas/home.js | 5 ----- frontend/src/sagas/usernameChoice.js | 42 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) delete mode 100644 frontend/src/sagas/home.js create mode 100644 frontend/src/sagas/usernameChoice.js (limited to 'frontend/src/sagas') diff --git a/frontend/src/sagas/home.js b/frontend/src/sagas/home.js deleted file mode 100644 index 24f385c9..00000000 --- a/frontend/src/sagas/home.js +++ /dev/null @@ -1,5 +0,0 @@ -function *homeSaga(wsConnection) { - yield console.log('home saga') -} - -export default homeSaga diff --git a/frontend/src/sagas/usernameChoice.js b/frontend/src/sagas/usernameChoice.js new file mode 100644 index 00000000..60a1a091 --- /dev/null +++ b/frontend/src/sagas/usernameChoice.js @@ -0,0 +1,42 @@ +import { call, take, put } from 'redux-saga/effects' +import { eventChannel } from 'redux-saga' + +import { actions, types } from '../redux/user' + +function usernameValidationChannel(socket) { + return eventChannel(emitter => { + const receiveUsernameHandler = socket.subscribe('/user/queue/nameChoice', event => { + emitter(JSON.parse(event.body)) + }) + + const unsubscribe = () => receiveUsernameHandler.unsubscribe() + + return unsubscribe + }) +} + +function *usernameValidation({ socket }) { + const usernameChannel = usernameValidationChannel(socket) + + const user = yield take(usernameChannel) + yield put(actions.setUsername(user.username, user.displayName, user.index)) + usernameChannel.close() +} + +function *sendUsername({ socket }) { + const { username } = yield take(types.CHOOSE_USERNAME) + + yield socket.send('/app/chooseName', JSON.stringify({ + playerName: username + })) +} + +function *usernameChoiceSaga(wsConnection) { + // TODO: Run sendUsername in loop when we have the ability to cancel saga on route change + yield [ + call(sendUsername, wsConnection), + call(usernameValidation, wsConnection), + ] +} + +export default usernameChoiceSaga -- cgit