@amityco/ts-sdk-react-native
Version:
Amity Social Cloud Typescript SDK
33 lines (27 loc) • 1.1 kB
text/typescript
export const onOffline = (callback: () => void) => {
if (typeof window !== 'undefined' && window.addEventListener) {
window.addEventListener('offline', callback);
return () => window.removeEventListener('offline', callback);
}
if (typeof document !== 'undefined' && document.addEventListener) {
document.addEventListener('offline', callback);
return () => document.removeEventListener('offline', callback);
}
// @TODO: Please update code below to follow our coding conventions and our TS rules
if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
// eslint-disable-next-line @typescript-eslint/no-empty-function
let unsubscribeFn = () => {};
import('@react-native-community/netinfo').then(NetInfo => {
unsubscribeFn = NetInfo.addEventListener(state => {
if (state.isConnected) return;
callback();
});
});
return () => {
unsubscribeFn();
};
}
// Handle unsupported environment
console.error('Unsupported environment');
return () => console.error('Unsupported environment');
};