mychips-react-sdk
Version:
MyChips Offerwall
33 lines (29 loc) • 1.11 kB
text/typescript
// src/services/ConnectivityService.ts
// defines the ConnectivityProvider interface and a default (optional) NetInfo-based provider.
export interface ConnectivityProvider {
subscribe(handler: (isConnected: boolean) => void): () => void;
getCurrentStatus?: () => boolean | Promise<boolean>;
}
/**
* Default implementation that uses @react-native-community/netinfo
* ONLY if it is installed in the host app. We load it dynamically to avoid hard dependency.
*/
export function getDefaultNetInfoProvider(): ConnectivityProvider | null {
try {
const mod = require('@react-native-community/netinfo');
const NetInfo = mod?.default ?? mod; // support both default and named export
return {
subscribe(handler) {
const unsub = NetInfo.addEventListener((state: any) => {
handler(Boolean(state?.isConnected));
});
return typeof unsub === 'function' ? unsub : () => unsub?.remove?.();
},
getCurrentStatus() {
return NetInfo.fetch().then((state: any) => Boolean(state?.isConnected));
},
};
} catch {
return null;
}
}