[FIX] SDK issues (#621)

* Remove listeners from room
* Properly close connections on change server
* Minor layout change on connecting badge
This commit is contained in:
Diego Mello 2019-02-12 14:14:11 -02:00 committed by GitHub
parent 5a2957fe5d
commit 68f5a94f5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 74 additions and 53 deletions

View File

@ -1,6 +1,6 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { import {
Text, StyleSheet, ActivityIndicator, Animated, TouchableWithoutFeedback, Easing Text, StyleSheet, ActivityIndicator, Animated, Easing
} from 'react-native'; } from 'react-native';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
@ -50,20 +50,30 @@ class ConnectionBadge extends Component {
static propTypes = { static propTypes = {
connecting: PropTypes.bool, connecting: PropTypes.bool,
connected: PropTypes.bool, connected: PropTypes.bool,
disconnected: PropTypes.bool // eslint-disable-line disconnected: PropTypes.bool
} }
constructor(props) { constructor(props) {
super(props); super(props);
this.animatedValue = new Animated.Value(0); this.animatedValue = new Animated.Value(0);
if (props.connecting) {
this.show();
}
} }
componentDidUpdate() { componentDidUpdate() {
this.show(); const { connected, disconnected } = this.props;
this.show(connected || disconnected);
}
componentWillUnmount() {
if (this.timeout) {
clearTimeout(this.timeout);
}
} }
// eslint-disable-next-line react/sort-comp // eslint-disable-next-line react/sort-comp
animate = debounce((toValue) => { animate = debounce((toValue, autoHide) => {
Animated.timing( Animated.timing(
this.animatedValue, this.animatedValue,
{ {
@ -73,7 +83,7 @@ class ConnectionBadge extends Component {
useNativeDriver: true useNativeDriver: true
}, },
).start(() => { ).start(() => {
if (toValue === 1) { if (toValue === 1 && autoHide) {
if (this.timeout) { if (this.timeout) {
clearTimeout(this.timeout); clearTimeout(this.timeout);
} }
@ -84,8 +94,8 @@ class ConnectionBadge extends Component {
}); });
}, 300); }, 300);
show = () => { show = (autoHide) => {
this.animate(1); this.animate(1, autoHide);
} }
hide = () => { hide = () => {
@ -102,29 +112,23 @@ class ConnectionBadge extends Component {
if (connecting) { if (connecting) {
return ( return (
<TouchableWithoutFeedback onPress={this.hide}> <Animated.View style={[styles.container, { transform: [{ translateY }] }]}>
<Animated.View style={[styles.container, { transform: [{ translateY }] }]}> <ActivityIndicator color='#9EA2A8' style={styles.activityIndicator} />
<ActivityIndicator color='#9EA2A8' style={styles.activityIndicator} /> <Text style={[styles.text, styles.textConnecting]}>{I18n.t('Connecting')}</Text>
<Text style={[styles.text, styles.textConnecting]}>{I18n.t('Connecting')}</Text> </Animated.View>
</Animated.View>
</TouchableWithoutFeedback>
); );
} else if (connected) { } else if (connected) {
return ( return (
<TouchableWithoutFeedback onPress={this.hide}> <Animated.View style={[styles.container, styles.containerConnected, { transform: [{ translateY }] }]}>
<Animated.View style={[styles.container, styles.containerConnected, { transform: [{ translateY }] }]}> <Text style={styles.text}>{I18n.t('Connected')}</Text>
<Text style={styles.text}>{I18n.t('Connected')}</Text> </Animated.View>
</Animated.View>
</TouchableWithoutFeedback>
); );
} }
return ( return (
<TouchableWithoutFeedback onPress={this.hide}> <Animated.View style={[styles.container, styles.containerOffline, { transform: [{ translateY }] }]}>
<Animated.View style={[styles.container, styles.containerOffline, { transform: [{ translateY }] }]}> <Text style={styles.text}>{I18n.t('Offline')}</Text>
<Text style={styles.text}>{I18n.t('Offline')}</Text> </Animated.View>
</Animated.View>
</TouchableWithoutFeedback>
); );
} }
} }

View File

@ -1,18 +1,12 @@
import log from '../../../utils/log'; import log from '../../../utils/log';
const unsubscribe = subscriptions => subscriptions.forEach(sub => sub.unsubscribe().catch(() => console.log('unsubscribeRoom'))); const unsubscribe = subscriptions => subscriptions.forEach(sub => sub.unsubscribe().catch(() => console.log('unsubscribeRoom')));
const removeListener = listener => listener.stop();
let promises; let promises;
let timer = null; let timer = null;
let connectedListener;
const stop = () => { let disconnectedListener;
if (promises) {
promises.then(unsubscribe);
promises = false;
}
clearTimeout(timer);
};
export default function subscribeRoom({ rid }) { export default function subscribeRoom({ rid }) {
if (promises) { if (promises) {
@ -23,31 +17,48 @@ export default function subscribeRoom({ rid }) {
if (timer) { if (timer) {
return; return;
} }
timer = setTimeout(async() => { timer = setTimeout(() => {
try { try {
clearTimeout(timer); clearTimeout(timer);
timer = false; timer = false;
if (this.sdk.userId) { this.loadMissedMessages({ rid });
await this.loadMissedMessages({ rid }); loop();
loop();
}
} catch (e) { } catch (e) {
loop(); loop();
} }
}, 5000); }, 5000);
}; };
this.sdk.onStreamData('connected', () => { const handleConnected = () => {
if (this.sdk.userId) { this.loadMissedMessages({ rid });
this.loadMissedMessages({ rid });
}
clearTimeout(timer); clearTimeout(timer);
timer = false; timer = false;
}); };
this.sdk.onStreamData('close', () => { const handleDisconnected = () => {
loop(); if (this.sdk.userId) {
}); loop();
}
};
const stop = () => {
if (promises) {
promises.then(unsubscribe);
promises = false;
}
if (connectedListener) {
connectedListener.then(removeListener);
connectedListener = false;
}
if (disconnectedListener) {
disconnectedListener.then(removeListener);
disconnectedListener = false;
}
clearTimeout(timer);
};
connectedListener = this.sdk.onStreamData('connected', handleConnected);
disconnectedListener = this.sdk.onStreamData('close', handleDisconnected);
try { try {
promises = this.sdk.subscribeRoom(rid); promises = this.sdk.subscribeRoom(rid);

View File

@ -141,7 +141,7 @@ const RocketChat = {
this.sdk.subscribe('roles'); this.sdk.subscribe('roles');
this.getPermissions(); this.getPermissions();
this.getCustomEmoji(); this.getCustomEmoji();
this.registerPushToken().then(result => console.log(result)).catch(e => console.log(e)); this.registerPushToken().catch(e => console.log(e));
}, },
connect({ server, user }) { connect({ server, user }) {
database.setActiveDB(server); database.setActiveDB(server);
@ -151,6 +151,11 @@ const RocketChat = {
clearTimeout(this.connectTimeout); clearTimeout(this.connectTimeout);
} }
if (this.sdk) {
this.sdk.disconnect();
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);

View File

@ -10,7 +10,8 @@ export default function connect(state = initialState, action) {
case METEOR.REQUEST: case METEOR.REQUEST:
return { return {
...state, ...state,
connecting: true connecting: true,
connected: false
}; };
case METEOR.SUCCESS: case METEOR.SUCCESS:
return { return {

View File

@ -1,7 +1,7 @@
import * as types from '../actions/actionsTypes'; import * as types from '../actions/actionsTypes';
const initialState = { const initialState = {
isFetching: true, isFetching: false,
failure: false, failure: false,
errorMessage: {}, errorMessage: {},
searchText: '', searchText: '',

View File

@ -21,7 +21,7 @@
}, },
"dependencies": { "dependencies": {
"@remobile/react-native-toast": "^1.0.7", "@remobile/react-native-toast": "^1.0.7",
"@rocket.chat/sdk": "1.0.0-alpha.24", "@rocket.chat/sdk": "1.0.0-alpha.25",
"deep-equal": "^1.0.1", "deep-equal": "^1.0.1",
"ejson": "^2.1.2", "ejson": "^2.1.2",
"js-base64": "^2.5.1", "js-base64": "^2.5.1",

View File

@ -988,10 +988,10 @@
resolved "https://registry.yarnpkg.com/@remobile/react-native-toast/-/react-native-toast-1.0.7.tgz#b2e3684cdb13e1c9d9b4ed08e667157d4ad0fab2" resolved "https://registry.yarnpkg.com/@remobile/react-native-toast/-/react-native-toast-1.0.7.tgz#b2e3684cdb13e1c9d9b4ed08e667157d4ad0fab2"
integrity sha512-iOD1PRnTSVr9sDWQdesIpfRrwJhHfeEQe5BpalQxC5OhM9thpiE6cu2NlW1KBWl0RJG4ZiJaF1xLlCo9YxU6dA== integrity sha512-iOD1PRnTSVr9sDWQdesIpfRrwJhHfeEQe5BpalQxC5OhM9thpiE6cu2NlW1KBWl0RJG4ZiJaF1xLlCo9YxU6dA==
"@rocket.chat/sdk@1.0.0-alpha.24": "@rocket.chat/sdk@1.0.0-alpha.25":
version "1.0.0-alpha.24" version "1.0.0-alpha.25"
resolved "https://registry.yarnpkg.com/@rocket.chat/sdk/-/sdk-1.0.0-alpha.24.tgz#8a6d3ff869c050b34f62bb7ef972d14c1a4d4f79" resolved "https://registry.yarnpkg.com/@rocket.chat/sdk/-/sdk-1.0.0-alpha.25.tgz#7fd6fce64e29c2ab9eb77000f1a65bdd3b0379bb"
integrity sha512-5EXVWkzQLGyKB5EdLZdpH86/gQ2rYY12OZ1ClRpAqYZs8svBKOn9pGBEWBW9i0C6ZowedFm8SdQjX4VHw+60hg== integrity sha512-/IfiLhTZooJQX1kimPZiY0FNSDw3kZcL/yquxD14kZbKRKm0ZSg5BJeWFpkinQq5Y9XjySX+k3mjmXX3Es97Cg==
dependencies: dependencies:
"@types/event-emitter" "^0.3.2" "@types/event-emitter" "^0.3.2"
"@types/eventemitter3" "^2.0.2" "@types/eventemitter3" "^2.0.2"