2018-04-24 19:34:03 +00:00
|
|
|
import { put, takeLatest } from 'redux-saga/effects';
|
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 { readySnippetedMessages } from '../actions/snippetedMessages';
|
2018-03-23 16:49:51 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
let sub;
|
|
|
|
let newSub;
|
|
|
|
|
|
|
|
const openSnippetedMessagesRoom = function* openSnippetedMessagesRoom({ rid, limit }) {
|
|
|
|
newSub = yield RocketChat.subscribe('snippetedMessages', rid, limit);
|
|
|
|
yield put(readySnippetedMessages());
|
|
|
|
if (sub) {
|
|
|
|
sub.unsubscribe().catch(e => console.warn('openSnippetedMessagesRoom', e));
|
|
|
|
}
|
|
|
|
sub = newSub;
|
|
|
|
};
|
|
|
|
|
|
|
|
const closeSnippetedMessagesRoom = function* closeSnippetedMessagesRoom() {
|
|
|
|
if (sub) {
|
|
|
|
yield sub.unsubscribe().catch(e => console.warn('closeSnippetedMessagesRoom sub', e));
|
|
|
|
}
|
|
|
|
if (newSub) {
|
|
|
|
yield newSub.unsubscribe().catch(e => console.warn('closeSnippetedMessagesRoom newSub', e));
|
|
|
|
}
|
2018-03-23 16:49:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const root = function* root() {
|
2018-04-24 19:34:03 +00:00
|
|
|
yield takeLatest(types.SNIPPETED_MESSAGES.OPEN, openSnippetedMessagesRoom);
|
|
|
|
yield takeLatest(types.SNIPPETED_MESSAGES.CLOSE, closeSnippetedMessagesRoom);
|
2018-03-23 16:49:51 +00:00
|
|
|
};
|
|
|
|
export default root;
|