[FIX] Remove rooms listener (#630)

This commit is contained in:
Diego Mello 2019-02-14 14:43:56 -02:00 committed by GitHub
parent ce96595dce
commit d795aea8c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 10 deletions

View File

@ -55,6 +55,7 @@ export default function subscribeRoom({ rid }) {
disconnectedListener = false; disconnectedListener = false;
} }
clearTimeout(timer); clearTimeout(timer);
timer = false;
}; };
connectedListener = this.sdk.onStreamData('connected', handleConnected); connectedListener = this.sdk.onStreamData('connected', handleConnected);

View File

@ -7,6 +7,12 @@ import random from '../../../utils/random';
import store from '../../createStore'; import store from '../../createStore';
import { roomsRequest } from '../../../actions/rooms'; import { roomsRequest } from '../../../actions/rooms';
const removeListener = listener => listener.stop();
let connectedListener;
let disconnectedListener;
let streamListener;
export default async function subscribeRooms() { export default async function subscribeRooms() {
let timer = null; let timer = null;
const loop = () => { const loop = () => {
@ -21,19 +27,19 @@ export default async function subscribeRooms() {
}, 5000); }, 5000);
}; };
this.sdk.onStreamData('connected', () => { const handleConnected = () => {
store.dispatch(roomsRequest()); store.dispatch(roomsRequest());
clearTimeout(timer); clearTimeout(timer);
timer = false; timer = false;
}); };
this.sdk.onStreamData('close', () => { const handleDisconnected = () => {
if (this.sdk.userId) { if (this.sdk.userId) {
loop(); loop();
} }
}); };
this.sdk.onStreamData('stream-notify-user', protectedFunction((ddpMessage) => { const handleStreamMessageReceived = protectedFunction((ddpMessage) => {
if (ddpMessage.msg === 'added') { if (ddpMessage.msg === 'added') {
return; return;
} }
@ -92,11 +98,36 @@ export default async function subscribeRooms() {
database.create('messages', message, true); 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 { try {
await this.sdk.subscribeNotifyUser(); await this.sdk.subscribeNotifyUser();
} catch (e) { } catch (e) {
log('subscribeRooms', e); log('subscribeRooms', e);
} }
return {
stop: () => stop()
};
} }

View File

@ -133,10 +133,15 @@ const RocketChat = {
this.activeUsers[ddpMessage.id] = { ...this.activeUsers[ddpMessage.id], ...activeUser, ...ddpMessage.fields }; this.activeUsers[ddpMessage.id] = { ...this.activeUsers[ddpMessage.id], ...activeUser, ...ddpMessage.fields };
} }
}, },
loginSuccess({ user }) { async loginSuccess({ user }) {
reduxStore.dispatch(setUser(user)); reduxStore.dispatch(setUser(user));
reduxStore.dispatch(roomsRequest()); reduxStore.dispatch(roomsRequest());
this.subscribeRooms();
if (this.roomsSub) {
this.roomsSub.stop();
}
this.roomsSub = await this.subscribeRooms();
this.sdk.subscribe('activeUsers'); this.sdk.subscribe('activeUsers');
this.sdk.subscribe('roles'); this.sdk.subscribe('roles');
this.getPermissions(); this.getPermissions();
@ -335,6 +340,10 @@ const RocketChat = {
} }
}, },
async logout({ server }) { async logout({ server }) {
if (this.roomsSub) {
this.roomsSub.stop();
}
try { try {
await this.removePushToken(); await this.removePushToken();
} catch (error) { } catch (error) {

View File

@ -76,10 +76,10 @@ export default class SettingsView extends LoggedView {
}, { }, {
label: '简体中文', label: '简体中文',
value: 'zh-CN' value: 'zh-CN'
},{ }, {
label: 'Français', label: 'Français',
value: 'fr' value: 'fr'
},], }],
saving: false saving: false
}; };
Navigation.events().bindComponent(this); Navigation.events().bindComponent(this);