I'm trying to find a way to know when a user is connected and when he disconnects, so that when he disconnects, I save his actions offline and when he connects again, I send them to the server. I'm using Redux Saga, Redux Offline Queue and Netinfo to do it, but I'm not being able to figure it out.
import {put, take} from 'redux-saga/effects';
import {eventChannel} from 'redux-saga';
import NetInfo from '@react-native-community/netinfo';
import {OFFLINE, ONLINE} from 'redux-offline-queue';
export function* startWatchingNetworkConnectivity() {
const channel = eventChannel((emitter) => {
NetInfo.addEventListener((state) => {
emitter(state.isConnected);
});
return () => console.log('Test');
});
try {
while (true) {
const isConnected = yield take(channel);
if (isConnected) {
yield put({type: ONLINE});
} else {
yield put({type: OFFLINE});
}
}
} finally {
channel.close();
}
}
I know that the eventChannel must return a function to unsubscribe, but I want it to never stop while the App is active, so I returned a console.log() just to test. I don't know if the return is the issue, or something else. I'm not able to fire those ONLINE and OFFLINE actions, can someone help me please?
Changing getState() to getOffline().toObject() inside offlineMiddleware.js solved the issue for me.