2017-12-20 19:20:06 +00:00
|
|
|
import { call, takeLatest, select, take, race } from 'redux-saga/effects';
|
|
|
|
import { delay } from 'redux-saga';
|
2017-08-17 01:06:31 +00:00
|
|
|
import { METEOR } from '../actions/actionsTypes';
|
|
|
|
import RocketChat from '../lib/rocketchat';
|
|
|
|
|
2017-09-01 19:42:50 +00:00
|
|
|
const getServer = ({ server }) => server.server;
|
2017-08-18 21:30:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
const connect = url => RocketChat.connect(url);
|
2017-12-20 19:20:06 +00:00
|
|
|
const watchConnect = function* watchConnect() {
|
|
|
|
const { disconnect } = yield race({
|
|
|
|
disconnect: take(METEOR.DISCONNECT),
|
|
|
|
disconnected_by_user: take(METEOR.DISCONNECT_BY_USER)
|
|
|
|
});
|
|
|
|
if (disconnect) {
|
|
|
|
while (true) {
|
|
|
|
const { connected } = yield race({
|
|
|
|
connected: take(METEOR.SUCCESS),
|
|
|
|
timeout: call(delay, 1000)
|
|
|
|
});
|
|
|
|
if (connected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
yield RocketChat.reconnect();
|
|
|
|
}
|
2017-08-18 21:30:16 +00:00
|
|
|
}
|
2017-08-17 16:55:47 +00:00
|
|
|
};
|
2017-12-20 19:20:06 +00:00
|
|
|
const test = function* test() {
|
|
|
|
// try {
|
|
|
|
const server = yield select(getServer);
|
|
|
|
// const response =
|
|
|
|
yield call(connect, server);
|
|
|
|
// yield put(connectSuccess(response));
|
|
|
|
// } catch (err) {
|
|
|
|
// yield put(connectFailure(err.status));
|
|
|
|
// }
|
|
|
|
};
|
|
|
|
|
2017-08-17 16:55:47 +00:00
|
|
|
const root = function* root() {
|
2017-09-01 19:42:50 +00:00
|
|
|
yield takeLatest(METEOR.REQUEST, test);
|
2017-12-20 19:20:06 +00:00
|
|
|
// yield take(METEOR.SUCCESS, watchConnect);
|
|
|
|
yield takeLatest(METEOR.SUCCESS, watchConnect);
|
2017-08-17 16:55:47 +00:00
|
|
|
};
|
|
|
|
export default root;
|