2018-04-24 19:34:03 +00:00
|
|
|
import { put, takeLatest } from 'redux-saga/effects';
|
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 { readyPinnedMessages } from '../actions/pinnedMessages';
|
2018-02-19 21:19:39 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
let sub;
|
|
|
|
let newSub;
|
|
|
|
|
|
|
|
const openPinnedMessagesRoom = function* openPinnedMessagesRoom({ rid, limit }) {
|
|
|
|
newSub = yield RocketChat.subscribe('pinnedMessages', rid, limit);
|
|
|
|
yield put(readyPinnedMessages());
|
|
|
|
if (sub) {
|
|
|
|
sub.unsubscribe().catch(e => console.warn('openPinnedMessagesRoom', e));
|
|
|
|
}
|
|
|
|
sub = newSub;
|
|
|
|
};
|
|
|
|
|
|
|
|
const closePinnedMessagesRoom = function* closePinnedMessagesRoom() {
|
|
|
|
if (sub) {
|
|
|
|
yield sub.unsubscribe().catch(e => console.warn('closePinnedMessagesRoom sub', e));
|
|
|
|
}
|
|
|
|
if (newSub) {
|
|
|
|
yield newSub.unsubscribe().catch(e => console.warn('closePinnedMessagesRoom newSub', e));
|
|
|
|
}
|
2018-02-19 21:19:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const root = function* root() {
|
2018-04-24 19:34:03 +00:00
|
|
|
yield takeLatest(types.PINNED_MESSAGES.OPEN, openPinnedMessagesRoom);
|
|
|
|
yield takeLatest(types.PINNED_MESSAGES.CLOSE, closePinnedMessagesRoom);
|
2018-02-19 21:19:39 +00:00
|
|
|
};
|
|
|
|
export default root;
|