Skip to content

Commit

Permalink
Merge pull request #28016 from software-mansion-labs/@Skalakid/ts/Net…
Browse files Browse the repository at this point in the history
…workConnection

[TS migration] Migrate 'NetworkConnection.js' lib to TypeScript
  • Loading branch information
robertjchen authored Oct 10, 2023
2 parents b2d1c6f + 9779ef4 commit d916eff
Showing 1 changed file with 12 additions and 14 deletions.
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) {
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

0 comments on commit d916eff

Please sign in to comment.