[FIX] Change server issue (#960)

* [FIX] Lazy fetch server info

* [FIX] Multiple servers issues
This commit is contained in:
Diego Mello 2019-06-05 16:11:29 -03:00 committed by GitHub
parent 109a247c8d
commit 3cd84a10f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 71 additions and 61 deletions

View File

@ -174,71 +174,75 @@ const RocketChat = {
this.getUserPresence(); this.getUserPresence();
}, },
connect({ server, user }) { connect({ server, user }) {
database.setActiveDB(server); return new Promise((resolve) => {
reduxStore.dispatch(connectRequest()); database.setActiveDB(server);
reduxStore.dispatch(connectRequest());
if (this.connectTimeout) { if (this.connectTimeout) {
clearTimeout(this.connectTimeout); clearTimeout(this.connectTimeout);
} }
if (this.sdk) { if (this.sdk) {
this.sdk.disconnect(); this.sdk.disconnect();
this.sdk = null; this.sdk = null;
} }
// Use useSsl: false only if server url starts with http:// // Use useSsl: false only if server url starts with http://
const useSsl = !/http:\/\//.test(server); const useSsl = !/http:\/\//.test(server);
this.sdk = new RocketchatClient({ host: server, protocol: 'ddp', useSsl }); this.sdk = new RocketchatClient({ host: server, protocol: 'ddp', useSsl });
this.getSettings(); this.getSettings();
this.sdk.connect() this.sdk.connect()
.then(() => { .then(() => {
if (user && user.token) { if (user && user.token) {
reduxStore.dispatch(loginRequest({ resume: user.token })); reduxStore.dispatch(loginRequest({ resume: user.token }));
}
})
.catch((err) => {
console.log('connect error', err);
// when `connect` raises an error, we try again in 10 seconds
this.connectTimeout = setTimeout(() => {
this.connect({ server, user });
}, 10000);
});
this.sdk.onStreamData('connected', () => {
reduxStore.dispatch(connectSuccess());
const { isAuthenticated } = reduxStore.getState().login;
if (isAuthenticated) {
this.getUserPresence();
} }
})
.catch((err) => {
console.log('connect error', err);
// when `connect` raises an error, we try again in 10 seconds
this.connectTimeout = setTimeout(() => {
this.connect({ server, user });
}, 10000);
}); });
this.sdk.onStreamData('connected', () => { this.sdk.onStreamData('close', () => {
reduxStore.dispatch(connectSuccess()); reduxStore.dispatch(disconnect());
const { isAuthenticated } = reduxStore.getState().login; });
if (isAuthenticated) {
this.getUserPresence();
}
});
this.sdk.onStreamData('close', () => { this.sdk.onStreamData('users', protectedFunction(ddpMessage => RocketChat._setUser(ddpMessage)));
reduxStore.dispatch(disconnect());
});
this.sdk.onStreamData('users', protectedFunction(ddpMessage => RocketChat._setUser(ddpMessage))); this.sdk.onStreamData('stream-notify-logged', protectedFunction((ddpMessage) => {
const { eventName } = ddpMessage.fields;
this.sdk.onStreamData('stream-notify-logged', protectedFunction((ddpMessage) => { if (eventName === 'user-status') {
const { eventName } = ddpMessage.fields; const userStatus = ddpMessage.fields.args[0];
if (eventName === 'user-status') { const [id, username, status] = userStatus;
const userStatus = ddpMessage.fields.args[0]; if (username) {
const [id, username, status] = userStatus; database.memoryDatabase.write(() => {
if (username) { try {
database.memoryDatabase.write(() => { database.memoryDatabase.create('activeUsers', {
try { id, username, status: STATUSES[status]
database.memoryDatabase.create('activeUsers', { }, true);
id, username, status: STATUSES[status] } catch (error) {
}, true); console.log(error);
} catch (error) { }
console.log(error); });
} }
});
} }
} }));
}));
resolve();
});
}, },
register(credentials) { register(credentials) {

View File

@ -39,11 +39,11 @@ const handleSelectServer = function* handleSelectServer({ server, version, fetch
if (userStringified) { if (userStringified) {
const user = JSON.parse(userStringified); const user = JSON.parse(userStringified);
RocketChat.connect({ server, user }); yield RocketChat.connect({ server, user });
yield put(setUser(user)); yield put(setUser(user));
yield put(actions.appStart('inside')); yield put(actions.appStart('inside'));
} else { } else {
RocketChat.connect({ server }); yield RocketChat.connect({ server });
yield put(actions.appStart('outside')); yield put(actions.appStart('outside'));
} }

View File

@ -60,7 +60,11 @@ const Header = React.memo(({
} }
return ( return (
<View style={styles.container}> <View style={styles.container}>
<TouchableOpacity onPress={onPress} testID='rooms-list-header-server-dropdown-button'> <TouchableOpacity
onPress={onPress}
testID='rooms-list-header-server-dropdown-button'
disabled={connecting || isFetching}
>
{connecting ? <Text style={styles.updating}>{I18n.t('Connecting')}</Text> : null} {connecting ? <Text style={styles.updating}>{I18n.t('Connecting')}</Text> : null}
{isFetching ? <Text style={styles.updating}>{I18n.t('Updating')}</Text> : null} {isFetching ? <Text style={styles.updating}>{I18n.t('Updating')}</Text> : null}
<View style={styles.button}> <View style={styles.button}>

View File

@ -40,13 +40,14 @@ const styles = StyleSheet.create({
}); });
const HeaderTitle = React.memo(({ connecting, isFetching }) => { const HeaderTitle = React.memo(({ connecting, isFetching }) => {
let title = I18n.t('Messages');
if (connecting) { if (connecting) {
return <Text style={styles.title}>{I18n.t('Connecting')}</Text>; title = I18n.t('Connecting');
} }
if (isFetching) { if (isFetching) {
return <Text style={styles.title}>{I18n.t('Updating')}</Text>; title = I18n.t('Updating');
} }
return <Text style={styles.title}>{I18n.t('Messages')}</Text>; return <Text style={styles.title}>{title}</Text>;
}); });
const Header = React.memo(({ const Header = React.memo(({
@ -57,6 +58,7 @@ const Header = React.memo(({
onPress={onPress} onPress={onPress}
testID='rooms-list-header-server-dropdown-button' testID='rooms-list-header-server-dropdown-button'
style={styles.container} style={styles.container}
disabled={connecting || isFetching}
> >
<HeaderTitle connecting={connecting} isFetching={isFetching} /> <HeaderTitle connecting={connecting} isFetching={isFetching} />
<View style={styles.button}> <View style={styles.button}>

View File

@ -11,7 +11,7 @@ import Header from './Header';
showServerDropdown: state.rooms.showServerDropdown, showServerDropdown: state.rooms.showServerDropdown,
showSortDropdown: state.rooms.showSortDropdown, showSortDropdown: state.rooms.showSortDropdown,
showSearchHeader: state.rooms.showSearchHeader, showSearchHeader: state.rooms.showSearchHeader,
connecting: state.meteor.connecting, connecting: state.meteor.connecting || state.server.loading,
isFetching: state.rooms.isFetching, isFetching: state.rooms.isFetching,
serverName: state.settings.Site_Name serverName: state.settings.Site_Name
}), dispatch => ({ }), dispatch => ({