Skip to content

Commit

Permalink
add resubscribing to hub on internet connection back
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolay.natorin committed Jan 11, 2021
1 parent ef99f9e commit 0d094c6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/screens/ChatScreen/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TouchableOpacity, Text, Clipboard, Alert, View, Linking } from 'react-n
import SafeAreaView from 'react-native-safe-area-view';
import { GiftedChat } from 'react-native-gifted-chat';
import _ from 'lodash';
import NetInfo, { useNetInfo } from "@react-native-community/netinfo";
import { useNetInfo } from "@react-native-community/netinfo";

import styles from './styles';
import { signMessage, fromWif } from 'obyte/lib/utils';
Expand Down
39 changes: 31 additions & 8 deletions src/screens/Container/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useDispatch, connect } from "react-redux";
import { AppState, StatusBar, InteractionManager, Platform, Linking, Alert } from 'react-native';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { setJSExceptionHandler, setNativeExceptionHandler } from 'react-native-exception-handler';
import { useNetInfo } from "@react-native-community/netinfo";

import common from '../../constants/common';
import { oClient } from '../../lib/oCustom';
Expand Down Expand Up @@ -36,13 +37,14 @@ setJSExceptionHandler((error, isFatal) => {
}
}, true);

let wasConnected = true;

const App = ({ walletInit, walletAddress }) => {
const dispatch = useDispatch();

const [appReady, setAppReady] = useState(false);
const [redirectParams, setRedirectParams] = useState(null);

const readyRef = useRef({ appReady });
const netInfo = useNetInfo();

const redirect = () => {
if (redirectParams) {
Expand All @@ -59,6 +61,32 @@ const App = ({ walletInit, walletAddress }) => {
}
};

const reconnectToHub = () => {
if (oClient.client.open) {
oClient.client.ws.addEventListener('close', resubscribe);
oClient.close();
} else {
dispatch(reSubscribeToHub());
}
};

useEffect(
() => {
const { isConnected } = netInfo;
if (appReady) {
if (!isConnected) {
wasConnected = false;
} else {
if (!wasConnected) {
reconnectToHub();
}
wasConnected = true;
}
}
},
[netInfo]
);

const handleLinkingUrl = (url) => {
if (url) {
const urlData = parseUrl(url, walletAddress);
Expand Down Expand Up @@ -111,12 +139,7 @@ const App = ({ walletInit, walletAddress }) => {
const changeListener = (appState) => {
if (readyRef.current.appReady) {
if (appState === 'active') {
if (oClient.client.open) {
oClient.client.ws.addEventListener('close', resubscribe);
oClient.close();
} else {
dispatch(reSubscribeToHub());
}
reconnectToHub();
} else {
oClient.client.ws.onclose = () => null;
stopSubscribeToHub();
Expand Down

0 comments on commit 0d094c6

Please sign in to comment.