verdnatura-chat/app/sagas/connect.js

31 lines
818 B
JavaScript
Raw Normal View History

import { take, put, call, fork, takeLatest, select } from 'redux-saga/effects';
2017-08-17 01:06:31 +00:00
import { METEOR } from '../actions/actionsTypes';
import RocketChat from '../lib/rocketchat';
import { connectSuccess, connectFailure } from '../actions/connect';
2017-08-17 01:06:31 +00:00
const getServer = ({ server }) => server;
const connect = url => RocketChat.connect(url);
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
};
2017-08-17 01:06:31 +00:00
const watchConnect = function* watchConnect() {
yield takeLatest(METEOR.REQUEST, test);
2017-08-17 01:06:31 +00:00
while (true) {
2017-08-17 16:55:47 +00:00
yield take(METEOR.DISCONNECT);
2017-08-17 01:06:31 +00:00
}
};
2017-08-17 16:55:47 +00:00
const root = function* root() {
yield fork(watchConnect);
// yield fork(auto);
};
export default root;