diff --git a/src/libs/Visibility/index.desktop.js b/src/libs/Visibility/index.desktop.ts similarity index 73% rename from src/libs/Visibility/index.desktop.js rename to src/libs/Visibility/index.desktop.ts index e3a7eab5bf56..bdc8b9a4267a 100644 --- a/src/libs/Visibility/index.desktop.js +++ b/src/libs/Visibility/index.desktop.ts @@ -1,31 +1,19 @@ import ELECTRON_EVENTS from '../../../desktop/ELECTRON_EVENTS'; +import {HasFocus, IsVisible, OnVisibilityChange} from './types'; /** * Detects whether the app is visible or not. Electron supports document.visibilityState, * but switching to another app while Electron is partially occluded will not trigger a state of hidden * so we ask the main process synchronously whether the BrowserWindow.isFocused() - * - * @returns {Boolean} */ -function isVisible() { - return window.electron.sendSync(ELECTRON_EVENTS.REQUEST_VISIBILITY); -} +const isVisible: IsVisible = () => !!window.electron.sendSync(ELECTRON_EVENTS.REQUEST_VISIBILITY); -/** - * @returns {Boolean} - */ -function hasFocus() { - return true; -} +const hasFocus: HasFocus = () => true; /** * Adds event listener for changes in visibility state - * - * @param {Function} callback - * - * @return {Function} removes the listener */ -function onVisibilityChange(callback) { +const onVisibilityChange: OnVisibilityChange = (callback) => { // Deliberately strip callback argument to be consistent across implementations window.electron.on(ELECTRON_EVENTS.FOCUS, () => callback()); window.electron.on(ELECTRON_EVENTS.BLUR, () => callback()); @@ -34,7 +22,7 @@ function onVisibilityChange(callback) { window.electron.removeAllListeners(ELECTRON_EVENTS.FOCUS); window.electron.removeAllListeners(ELECTRON_EVENTS.BLUR); }; -} +}; export default { isVisible, diff --git a/src/libs/Visibility/index.native.js b/src/libs/Visibility/index.native.ts similarity index 63% rename from src/libs/Visibility/index.native.js rename to src/libs/Visibility/index.native.ts index b888cf1a2b9f..695df3651da7 100644 --- a/src/libs/Visibility/index.native.js +++ b/src/libs/Visibility/index.native.ts @@ -2,32 +2,21 @@ // they do not use the Notification lib. import {AppState} from 'react-native'; +import {HasFocus, IsVisible, OnVisibilityChange} from './types'; -/** - * @return {Boolean} - */ -const isVisible = () => AppState.currentState === 'active'; +const isVisible: IsVisible = () => AppState.currentState === 'active'; -/** - * @returns {Boolean} - */ -function hasFocus() { - return true; -} +const hasFocus: HasFocus = () => true; /** * Adds event listener for changes in visibility state - * - * @param {Function} callback - * - * @return {Function} removes the listener */ -function onVisibilityChange(callback) { +const onVisibilityChange: OnVisibilityChange = (callback) => { // Deliberately strip callback argument to be consistent across implementations const subscription = AppState.addEventListener('change', () => callback()); return () => subscription.remove(); -} +}; export default { isVisible, diff --git a/src/libs/Visibility/index.js b/src/libs/Visibility/index.ts similarity index 61% rename from src/libs/Visibility/index.js rename to src/libs/Visibility/index.ts index 91a003168ccd..4cf18b010a07 100644 --- a/src/libs/Visibility/index.js +++ b/src/libs/Visibility/index.ts @@ -1,36 +1,25 @@ import {AppState} from 'react-native'; +import {HasFocus, IsVisible, OnVisibilityChange} from './types'; /** * Detects whether the app is visible or not. - * - * @returns {Boolean} */ -function isVisible() { - return document.visibilityState === 'visible'; -} +const isVisible: IsVisible = () => document.visibilityState === 'visible'; /** * Whether the app is focused. - * - * @returns {Boolean} */ -function hasFocus() { - return document.hasFocus(); -} +const hasFocus: HasFocus = () => document.hasFocus(); /** * Adds event listener for changes in visibility state - * - * @param {Function} callback - * - * @return {Function} removes the listener */ -function onVisibilityChange(callback) { +const onVisibilityChange: OnVisibilityChange = (callback) => { // Deliberately strip callback argument to be consistent across implementations const subscription = AppState.addEventListener('change', () => callback()); return () => subscription.remove(); -} +}; export default { isVisible, diff --git a/src/libs/Visibility/types.ts b/src/libs/Visibility/types.ts new file mode 100644 index 000000000000..0332f24740ce --- /dev/null +++ b/src/libs/Visibility/types.ts @@ -0,0 +1,5 @@ +type IsVisible = () => boolean; +type HasFocus = () => boolean; +type OnVisibilityChange = (callback: () => void) => () => void; + +export type {IsVisible, HasFocus, OnVisibilityChange}; diff --git a/src/types/modules/electron.d.ts b/src/types/modules/electron.d.ts new file mode 100644 index 000000000000..09e33f29ba38 --- /dev/null +++ b/src/types/modules/electron.d.ts @@ -0,0 +1,18 @@ +// TODO: Move this type to desktop/contextBridge.js once it is converted to TS +type ContextBridgeApi = { + send: (channel: string, data?: unknown) => void; + sendSync: (channel: string, data?: unknown) => unknown; + invoke: (channel: string, ...args: unknown) => Promise; + on: (channel: string, func: () => void) => void; + removeAllListeners: (channel: string) => void; +}; + +declare global { + // eslint-disable-next-line @typescript-eslint/consistent-type-definitions + interface Window { + electron: ContextBridgeApi; + } +} + +// We used the export {} line to mark this file as an external module +export {};