diff --git a/app/lib/methods/subscriptions/room.js b/app/lib/methods/subscriptions/room.js index fa350836..410de373 100644 --- a/app/lib/methods/subscriptions/room.js +++ b/app/lib/methods/subscriptions/room.js @@ -55,6 +55,7 @@ export default function subscribeRoom({ rid }) { disconnectedListener = false; } clearTimeout(timer); + timer = false; }; connectedListener = this.sdk.onStreamData('connected', handleConnected); diff --git a/app/lib/methods/subscriptions/rooms.js b/app/lib/methods/subscriptions/rooms.js index 84fff4a2..f0abe771 100644 --- a/app/lib/methods/subscriptions/rooms.js +++ b/app/lib/methods/subscriptions/rooms.js @@ -7,6 +7,12 @@ import random from '../../../utils/random'; import store from '../../createStore'; import { roomsRequest } from '../../../actions/rooms'; +const removeListener = listener => listener.stop(); + +let connectedListener; +let disconnectedListener; +let streamListener; + export default async function subscribeRooms() { let timer = null; const loop = () => { @@ -21,19 +27,19 @@ export default async function subscribeRooms() { }, 5000); }; - this.sdk.onStreamData('connected', () => { + const handleConnected = () => { store.dispatch(roomsRequest()); clearTimeout(timer); timer = false; - }); + }; - this.sdk.onStreamData('close', () => { + const handleDisconnected = () => { if (this.sdk.userId) { loop(); } - }); + }; - this.sdk.onStreamData('stream-notify-user', protectedFunction((ddpMessage) => { + const handleStreamMessageReceived = protectedFunction((ddpMessage) => { if (ddpMessage.msg === 'added') { return; } @@ -92,11 +98,36 @@ export default async function subscribeRooms() { database.create('messages', message, true); })); } - })); + }); + + const stop = () => { + if (connectedListener) { + connectedListener.then(removeListener); + connectedListener = false; + } + if (disconnectedListener) { + disconnectedListener.then(removeListener); + disconnectedListener = false; + } + if (streamListener) { + streamListener.then(removeListener); + streamListener = false; + } + clearTimeout(timer); + timer = false; + }; + + connectedListener = this.sdk.onStreamData('connected', handleConnected); + disconnectedListener = this.sdk.onStreamData('close', handleDisconnected); + streamListener = this.sdk.onStreamData('stream-notify-user', handleStreamMessageReceived); try { await this.sdk.subscribeNotifyUser(); } catch (e) { log('subscribeRooms', e); } + + return { + stop: () => stop() + }; } diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index 9d4f673b..fbb73eaf 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -133,10 +133,15 @@ const RocketChat = { this.activeUsers[ddpMessage.id] = { ...this.activeUsers[ddpMessage.id], ...activeUser, ...ddpMessage.fields }; } }, - loginSuccess({ user }) { + async loginSuccess({ user }) { reduxStore.dispatch(setUser(user)); reduxStore.dispatch(roomsRequest()); - this.subscribeRooms(); + + if (this.roomsSub) { + this.roomsSub.stop(); + } + this.roomsSub = await this.subscribeRooms(); + this.sdk.subscribe('activeUsers'); this.sdk.subscribe('roles'); this.getPermissions(); @@ -335,6 +340,10 @@ const RocketChat = { } }, async logout({ server }) { + if (this.roomsSub) { + this.roomsSub.stop(); + } + try { await this.removePushToken(); } catch (error) { diff --git a/app/views/SettingsView/index.js b/app/views/SettingsView/index.js index 86f3e184..86050b5f 100644 --- a/app/views/SettingsView/index.js +++ b/app/views/SettingsView/index.js @@ -76,10 +76,10 @@ export default class SettingsView extends LoggedView { }, { label: '简体中文', value: 'zh-CN' - },{ + }, { label: 'Français', value: 'fr' - },], + }], saving: false }; Navigation.events().bindComponent(this);