[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;
}
clearTimeout(timer);
timer = false;
};
connectedListener = this.sdk.onStreamData('connected', handleConnected);

View File

@ -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()
};
}

View File

@ -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) {

View File

@ -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);