From bda70f4f372dc23238e6befc9e1ec2499b539b19 Mon Sep 17 00:00:00 2001 From: Kamil Owczarz Date: Thu, 14 Sep 2023 13:16:16 +0200 Subject: [PATCH 1/4] Add types file --- src/libs/AppStateMonitor/shouldReportActivity/types.ts | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 src/libs/AppStateMonitor/shouldReportActivity/types.ts diff --git a/src/libs/AppStateMonitor/shouldReportActivity/types.ts b/src/libs/AppStateMonitor/shouldReportActivity/types.ts new file mode 100644 index 000000000000..2341c0d5cbf1 --- /dev/null +++ b/src/libs/AppStateMonitor/shouldReportActivity/types.ts @@ -0,0 +1,3 @@ +type ShouldReportActivity = boolean; + +export default ShouldReportActivity; From 0e2c116fd770bdb80c25dae004619bd2fee0d4e4 Mon Sep 17 00:00:00 2001 From: Kamil Owczarz Date: Thu, 14 Sep 2023 13:16:31 +0200 Subject: [PATCH 2/4] Migrate existing files to typescript --- src/libs/AppStateMonitor/{index.js => index.ts} | 11 +++-------- .../shouldReportActivity/index.native.js | 1 - .../shouldReportActivity/index.native.ts | 5 +++++ .../shouldReportActivity/{index.js => index.ts} | 6 +++++- 4 files changed, 13 insertions(+), 10 deletions(-) rename src/libs/AppStateMonitor/{index.js => index.ts} (79%) delete mode 100644 src/libs/AppStateMonitor/shouldReportActivity/index.native.js create mode 100644 src/libs/AppStateMonitor/shouldReportActivity/index.native.ts rename src/libs/AppStateMonitor/shouldReportActivity/{index.js => index.ts} (70%) diff --git a/src/libs/AppStateMonitor/index.js b/src/libs/AppStateMonitor/index.ts similarity index 79% rename from src/libs/AppStateMonitor/index.js rename to src/libs/AppStateMonitor/index.ts index 12370382919e..346673117ef1 100644 --- a/src/libs/AppStateMonitor/index.js +++ b/src/libs/AppStateMonitor/index.ts @@ -6,15 +6,10 @@ let appState = CONST.APP_STATE.ACTIVE; /** * Listener that will only fire the callback when the user has become active. - * - * @param {Function} callback - * @returns {Function} to unsubscribe + * @returns callback to unsubscribe */ -function addBecameActiveListener(callback) { - /** - * @param {String} state - */ - function appStateChangeCallback(state) { +function addBecameActiveListener(callback: () => void) { + function appStateChangeCallback(state: keyof CONST.APP_STATE) { if (shouldReportActivity && (appState === CONST.APP_STATE.INACTIVE || appState === CONST.APP_STATE.BACKGROUND) && state === CONST.APP_STATE.ACTIVE) { callback(); } diff --git a/src/libs/AppStateMonitor/shouldReportActivity/index.native.js b/src/libs/AppStateMonitor/shouldReportActivity/index.native.js deleted file mode 100644 index ff3177babdde..000000000000 --- a/src/libs/AppStateMonitor/shouldReportActivity/index.native.js +++ /dev/null @@ -1 +0,0 @@ -export default true; diff --git a/src/libs/AppStateMonitor/shouldReportActivity/index.native.ts b/src/libs/AppStateMonitor/shouldReportActivity/index.native.ts new file mode 100644 index 000000000000..0e5fdb57a597 --- /dev/null +++ b/src/libs/AppStateMonitor/shouldReportActivity/index.native.ts @@ -0,0 +1,5 @@ +import ShouldReportActivity from './types'; + +const shouldReportActivity: ShouldReportActivity = true; + +export default shouldReportActivity; diff --git a/src/libs/AppStateMonitor/shouldReportActivity/index.js b/src/libs/AppStateMonitor/shouldReportActivity/index.ts similarity index 70% rename from src/libs/AppStateMonitor/shouldReportActivity/index.js rename to src/libs/AppStateMonitor/shouldReportActivity/index.ts index 05939ab7eb8d..db326345714e 100644 --- a/src/libs/AppStateMonitor/shouldReportActivity/index.js +++ b/src/libs/AppStateMonitor/shouldReportActivity/index.ts @@ -1,4 +1,8 @@ +import ShouldReportActivity from './types'; + // We only need to report when the app becomes active on native since web maintains most of it's network functions while // in the "background" and the concept is not quite the same on mobile. We avoid setting this to true for web since // the event would fire much more frequently than it does on native causing performance issues. -export default false; +const shouldReportActivity: ShouldReportActivity = false; + +export default shouldReportActivity; From 1c84e509af5dcdcf6a6adfda2ac026ee6d33899a Mon Sep 17 00:00:00 2001 From: Kamil Owczarz Date: Thu, 14 Sep 2023 14:28:20 +0200 Subject: [PATCH 3/4] Fix types --- src/libs/AppStateMonitor/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/AppStateMonitor/index.ts b/src/libs/AppStateMonitor/index.ts index 346673117ef1..dd2f54d4d94e 100644 --- a/src/libs/AppStateMonitor/index.ts +++ b/src/libs/AppStateMonitor/index.ts @@ -1,15 +1,15 @@ -import {AppState} from 'react-native'; +import {AppState, AppStateStatus} from 'react-native'; import CONST from '../../CONST'; import shouldReportActivity from './shouldReportActivity'; -let appState = CONST.APP_STATE.ACTIVE; +let appState: AppStateStatus = CONST.APP_STATE.ACTIVE; /** * Listener that will only fire the callback when the user has become active. * @returns callback to unsubscribe */ function addBecameActiveListener(callback: () => void) { - function appStateChangeCallback(state: keyof CONST.APP_STATE) { + function appStateChangeCallback(state: AppStateStatus) { if (shouldReportActivity && (appState === CONST.APP_STATE.INACTIVE || appState === CONST.APP_STATE.BACKGROUND) && state === CONST.APP_STATE.ACTIVE) { callback(); } From 05b96ec0ce2acc50e89c1b2d36068b711d02d2b3 Mon Sep 17 00:00:00 2001 From: Kamil Owczarz <91068263+kowczarz@users.noreply.github.com> Date: Fri, 15 Sep 2023 10:25:40 +0200 Subject: [PATCH 4/4] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Fábio Henriques --- src/libs/AppStateMonitor/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/AppStateMonitor/index.ts b/src/libs/AppStateMonitor/index.ts index dd2f54d4d94e..5c206579944d 100644 --- a/src/libs/AppStateMonitor/index.ts +++ b/src/libs/AppStateMonitor/index.ts @@ -8,7 +8,7 @@ let appState: AppStateStatus = CONST.APP_STATE.ACTIVE; * Listener that will only fire the callback when the user has become active. * @returns callback to unsubscribe */ -function addBecameActiveListener(callback: () => void) { +function addBecameActiveListener(callback: () => void): () => void { function appStateChangeCallback(state: AppStateStatus) { if (shouldReportActivity && (appState === CONST.APP_STATE.INACTIVE || appState === CONST.APP_STATE.BACKGROUND) && state === CONST.APP_STATE.ACTIVE) { callback();