verdnatura-chat/app/sagas/connect.js

45 lines
1.1 KiB
JavaScript
Raw Normal View History

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';
const getServer = ({ server }) => server.server;
const connect = url => RocketChat.connect(url);
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-17 16:55:47 +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() {
yield takeLatest(METEOR.REQUEST, test);
// yield take(METEOR.SUCCESS, watchConnect);
yield takeLatest(METEOR.SUCCESS, watchConnect);
2017-08-17 16:55:47 +00:00
};
export default root;