2018-04-24 19:34:03 +00:00
|
|
|
import { put, takeLatest } from 'redux-saga/effects';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2018-03-23 16:49:51 +00:00
|
|
|
import * as types from '../actions/actionsTypes';
|
|
|
|
import RocketChat from '../lib/rocketchat';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { readyRoomFiles } from '../actions/roomFiles';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../utils/log';
|
2018-03-23 16:49:51 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
let sub;
|
|
|
|
let newSub;
|
|
|
|
|
|
|
|
const openRoomFiles = function* openRoomFiles({ rid, limit }) {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2018-10-15 20:22:42 +00:00
|
|
|
sub = yield RocketChat.subscribe('roomFiles', rid, limit);
|
2018-05-18 17:55:08 +00:00
|
|
|
yield put(readyRoomFiles());
|
|
|
|
if (sub) {
|
2018-05-29 17:09:20 +00:00
|
|
|
sub.unsubscribe().catch(err => console.warn(err));
|
2018-05-18 17:55:08 +00:00
|
|
|
}
|
|
|
|
sub = newSub;
|
|
|
|
} catch (e) {
|
|
|
|
log('openRoomFiles', e);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const closeRoomFiles = function* closeRoomFiles() {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2018-10-15 20:22:42 +00:00
|
|
|
// yield sub.unsubscribe(sub);
|
|
|
|
// sub = null;
|
2018-05-18 17:55:08 +00:00
|
|
|
if (sub) {
|
|
|
|
yield sub.unsubscribe();
|
|
|
|
}
|
|
|
|
if (newSub) {
|
|
|
|
yield newSub.unsubscribe();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log('closeRoomFiles', e);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2018-03-23 16:49:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const root = function* root() {
|
2018-04-24 19:34:03 +00:00
|
|
|
yield takeLatest(types.ROOM_FILES.OPEN, openRoomFiles);
|
|
|
|
yield takeLatest(types.ROOM_FILES.CLOSE, closeRoomFiles);
|
2018-03-23 16:49:51 +00:00
|
|
|
};
|
|
|
|
export default root;
|