Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS migration] Migrate 'NetworkConnection.js' lib to TypeScript #28016

26 changes: 12 additions & 14 deletions src/libs/NetworkConnection.js → src/libs/NetworkConnection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'underscore';
import Onyx from 'react-native-onyx';
import NetInfo from '@react-native-community/netinfo';
import throttle from 'lodash/throttle';
import AppStateMonitor from './AppStateMonitor';
import Log from './Log';
import * as NetworkActions from './actions/Network';
Expand All @@ -13,15 +13,17 @@ let hasPendingNetworkCheck = false;

// Holds all of the callbacks that need to be triggered when the network reconnects
let callbackID = 0;
const reconnectionCallbacks = {};
const reconnectionCallbacks: Record<string, () => Promise<void>> = {};

/**
* Loop over all reconnection callbacks and fire each one
*/
const triggerReconnectionCallbacks = _.throttle(
const triggerReconnectionCallbacks = throttle(
(reason) => {
Log.info(`[NetworkConnection] Firing reconnection callbacks because ${reason}`);
_.each(reconnectionCallbacks, (callback) => callback());
Object.values(reconnectionCallbacks).forEach((callback) => {
callback();
});
},
5000,
{trailing: false},
Expand All @@ -30,10 +32,8 @@ const triggerReconnectionCallbacks = _.throttle(
/**
* Called when the offline status of the app changes and if the network is "reconnecting" (going from offline to online)
* then all of the reconnection callbacks are triggered
*
* @param {Boolean} isCurrentlyOffline
*/
function setOfflineStatus(isCurrentlyOffline) {
function setOfflineStatus(isCurrentlyOffline: boolean): void {
NetworkActions.setIsOffline(isCurrentlyOffline);

// When reconnecting, ie, going from offline to online, all the reconnection callbacks
Expand Down Expand Up @@ -72,7 +72,7 @@ Onyx.connect({
* internet connectivity or not. This is more reliable than the Pusher
* `disconnected` event which takes about 10-15 seconds to emit.
*/
function subscribeToNetInfo() {
function subscribeToNetInfo(): void {
// Note: We are disabling the configuration for NetInfo when using the local web API since requests can get stuck in a 'Pending' state and are not reliable indicators for "offline".
// If you need to test the "recheck" feature then switch to the production API proxy server.
if (!CONFIG.IS_USING_LOCAL_WEB) {
Expand Down Expand Up @@ -101,7 +101,7 @@ function subscribeToNetInfo() {
// Subscribe to the state change event via NetInfo so we can update
// whether a user has internet connectivity or not.
NetInfo.addEventListener((state) => {
Log.info('[NetworkConnection] NetInfo state change', false, state);
Log.info('[NetworkConnection] NetInfo state change', false, {...state});
if (shouldForceOffline) {
robertjchen marked this conversation as resolved.
Show resolved Hide resolved
Log.info('[NetworkConnection] Not setting offline status because shouldForceOffline = true');
return;
Expand All @@ -120,11 +120,9 @@ function listenForReconnect() {

/**
* Register callback to fire when we reconnect
*
* @param {Function} callback - must return a Promise
* @returns {Function} unsubscribe method
* @returns unsubscribe method
*/
function onReconnect(callback) {
function onReconnect(callback: () => Promise<void>): () => void {
const currentID = callbackID;
callbackID++;
reconnectionCallbacks[currentID] = callback;
Expand All @@ -135,7 +133,7 @@ function onReconnect(callback) {
* Delete all queued reconnection callbacks
*/
function clearReconnectionCallbacks() {
_.each(_.keys(reconnectionCallbacks), (key) => delete reconnectionCallbacks[key]);
Object.keys(reconnectionCallbacks).forEach((key) => delete reconnectionCallbacks[key]);
}

/**
Expand Down
Loading