From 0aeedbb2f2eea4b86902b4af0e1e7ce3399d2f9f Mon Sep 17 00:00:00 2001 From: Diego Mello Date: Tue, 27 Aug 2019 14:41:07 -0300 Subject: [PATCH] [FIX] Unsubscribe rooms listeners on server change (#1153) --- app/lib/rocketchat.js | 7 ++++++- app/sagas/rooms.js | 18 ++---------------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index 53bfbe43..552a474b 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -53,7 +53,12 @@ const STATUSES = ['offline', 'online', 'away', 'busy']; const RocketChat = { TOKEN_KEY, - subscribeRooms, + async subscribeRooms() { + if (this.roomsSub) { + this.roomsSub.stop(); + } + this.roomsSub = await subscribeRooms.call(this); + }, subscribeRoom, canOpenRoom, createChannel({ diff --git a/app/sagas/rooms.js b/app/sagas/rooms.js index 082c633f..19f2e2e6 100644 --- a/app/sagas/rooms.js +++ b/app/sagas/rooms.js @@ -1,5 +1,5 @@ import { - put, select, race, take, fork, cancel, takeLatest, delay + put, select, race, take, fork, cancel, delay } from 'redux-saga/effects'; import { BACKGROUND, INACTIVE } from 'redux-enhancer-react-native-appstate'; @@ -10,18 +10,9 @@ import log from '../utils/log'; import mergeSubscriptionsRooms from '../lib/methods/helpers/mergeSubscriptionsRooms'; import RocketChat from '../lib/rocketchat'; -let roomsSub; - -const removeSub = function removeSub() { - if (roomsSub && roomsSub.stop) { - roomsSub.stop(); - } -}; - const handleRoomsRequest = function* handleRoomsRequest() { try { - removeSub(); - roomsSub = yield RocketChat.subscribeRooms(); + yield RocketChat.subscribeRooms(); const newRoomsUpdatedAt = new Date(); const server = yield select(state => state.server.server); const [serverRecord] = database.databases.serversDB.objects('servers').filtered('id = $0', server); @@ -53,12 +44,7 @@ const handleRoomsRequest = function* handleRoomsRequest() { } }; -const handleLogout = function handleLogout() { - removeSub(); -}; - const root = function* root() { - yield takeLatest(types.LOGOUT, handleLogout); while (true) { const params = yield take(types.ROOMS.REQUEST); const isAuthenticated = yield select(state => state.login.isAuthenticated);