verdnatura-chat/app/sagas/connect.js

38 lines
1009 B
JavaScript
Raw Normal View History

2017-08-17 16:55:47 +00:00
import { take, put, call, fork, takeLatest } from 'redux-saga/effects';
2017-08-17 01:06:31 +00:00
import { METEOR } from '../actions/actionsTypes';
import RocketChat from '../lib/rocketchat';
2017-08-17 16:55:47 +00:00
import { connectSuccess, connectRequest, connectFailure } from '../actions/connect';
2017-08-17 01:06:31 +00:00
function connect(...args) {
return RocketChat.connect(...args);
}
2017-08-17 16:55:47 +00:00
const auto = function* auto() {
while (true) {
yield take(METEOR.DISCONNECT);
console.log('\n\n[METEOR DISCONNECT]\n\n');
yield put(connectRequest());
}
};
const test = function* test() {
const response = yield call(connect);
yield put(connectSuccess(response));
console.log('\n\n[METEOR CONNECTED]\n\n');
};
2017-08-17 01:06:31 +00:00
const watchConnect = function* watchConnect() {
while (true) {
try {
2017-08-17 16:55:47 +00:00
yield takeLatest(METEOR.REQUEST, test);
2017-08-17 01:06:31 +00:00
} catch (err) {
yield put(connectFailure(err.status));
}
2017-08-17 16:55:47 +00:00
yield take(METEOR.DISCONNECT);
console.log('\n\n[METEOR DISCONNECT]\n\n');
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;