2018-04-24 19:34:03 +00:00
|
|
|
import { put, takeLatest } from 'redux-saga/effects';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2018-02-19 21:19:39 +00:00
|
|
|
import * as types from '../actions/actionsTypes';
|
|
|
|
import RocketChat from '../lib/rocketchat';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { readyStarredMessages } from '../actions/starredMessages';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../utils/log';
|
2018-02-19 21:19:39 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
let sub;
|
|
|
|
let newSub;
|
|
|
|
|
|
|
|
const openStarredMessagesRoom = function* openStarredMessagesRoom({ rid, limit }) {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
|
|
|
newSub = yield RocketChat.subscribe('starredMessages', rid, limit);
|
|
|
|
yield put(readyStarredMessages());
|
|
|
|
if (sub) {
|
|
|
|
sub.unsubscribe();
|
|
|
|
}
|
|
|
|
sub = newSub;
|
|
|
|
} catch (e) {
|
|
|
|
log('openStarredMessagesRoom', e);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const closeStarredMessagesRoom = function* closeStarredMessagesRoom() {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
|
|
|
if (sub) {
|
|
|
|
yield sub.unsubscribe();
|
|
|
|
}
|
|
|
|
if (newSub) {
|
|
|
|
yield newSub.unsubscribe();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log('closeStarredMessagesRoom', e);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2018-02-19 21:19:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const root = function* root() {
|
2018-04-24 19:34:03 +00:00
|
|
|
yield takeLatest(types.STARRED_MESSAGES.OPEN, openStarredMessagesRoom);
|
|
|
|
yield takeLatest(types.STARRED_MESSAGES.CLOSE, closeStarredMessagesRoom);
|
2018-02-19 21:19:39 +00:00
|
|
|
};
|
|
|
|
export default root;
|