[FIX] Remove rooms listener (#630)
This commit is contained in:
parent
ce96595dce
commit
d795aea8c4
|
@ -55,6 +55,7 @@ export default function subscribeRoom({ rid }) {
|
|||
disconnectedListener = false;
|
||||
}
|
||||
clearTimeout(timer);
|
||||
timer = false;
|
||||
};
|
||||
|
||||
connectedListener = this.sdk.onStreamData('connected', handleConnected);
|
||||
|
|
|
@ -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()
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -79,7 +79,7 @@ export default class SettingsView extends LoggedView {
|
|||
}, {
|
||||
label: 'Français',
|
||||
value: 'fr'
|
||||
},],
|
||||
}],
|
||||
saving: false
|
||||
};
|
||||
Navigation.events().bindComponent(this);
|
||||
|
|
Loading…
Reference in New Issue