summaryrefslogtreecommitdiff
path: root/frontend/src/sagas/home.js
blob: 43ab97dc6b643fe97e1a4ea8dc72d8584c3df99a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// @flow
import { push } from 'react-router-redux';
import type { SagaIterator } from 'redux-saga';
import { eventChannel } from 'redux-saga';
import { all, apply, call, put, take } from 'redux-saga/effects';
import type { ApiPlayer } from '../api/model';
import type { SevenWondersSession } from '../api/sevenWondersApi';
import { actions } from '../redux/actions/user';
import { types } from '../redux/actions/user';

function* sendUsername(session: SevenWondersSession): SagaIterator {
  while (true) {
    const { username } = yield take(types.REQUEST_CHOOSE_USERNAME);
    // $FlowFixMe
    yield apply(session, session.chooseName, [username]);
  }
}

function* validateUsername(session: SevenWondersSession): SagaIterator {
  const usernameChannel = yield eventChannel(session.watchNameChoice());
  while (true) {
    const user: ApiPlayer = yield take(usernameChannel);
    yield put(actions.setCurrentPlayer(user));
    yield apply(usernameChannel, usernameChannel.close);
    yield put(push('/games'));
  }
}

export function* homeSaga(session: SevenWondersSession): SagaIterator {
  yield all([call(sendUsername, session), call(validateUsername, session)]);
}
bgstack15