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

[No QA][TS migration] Migrate 'Visibility' lib to TypeScript #27437

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,20 @@ import ELECTRON_EVENTS from '../../../desktop/ELECTRON_EVENTS';
* 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() {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think electron is typed as 'any', we have to change this @Skalakid

return window.electron.sendSync(ELECTRON_EVENTS.REQUEST_VISIBILITY);
}

/**
* @returns {Boolean}
*/
function hasFocus() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create types.ts file with shared types, similar to this PR

return true;
}

/**
* Adds event listener for changes in visibility state
*
* @param {Function} callback
*
* @return {Function} removes the listener
*/
function onVisibilityChange(callback) {
function onVisibilityChange(callback: () => void) {
// Deliberately strip callback argument to be consistent across implementations
window.electron.on(ELECTRON_EVENTS.FOCUS, () => callback());
window.electron.on(ELECTRON_EVENTS.BLUR, () => callback());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,18 @@

import {AppState} from 'react-native';

/**
* @return {Boolean}
*/
const isVisible = () => AppState.currentState === 'active';
function isVisible() {
return AppState.currentState === 'active';
}

/**
* @returns {Boolean}
*/
function hasFocus() {
return true;
}

/**
* Adds event listener for changes in visibility state
*
* @param {Function} callback
*
* @return {Function} removes the listener
*/
function onVisibilityChange(callback) {
function onVisibilityChange(callback: () => void) {
// Deliberately strip callback argument to be consistent across implementations
const subscription = AppState.addEventListener('change', () => callback());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,22 @@ import {AppState} from 'react-native';

/**
* Detects whether the app is visible or not.
*
* @returns {Boolean}
*/
function isVisible() {
return document.visibilityState === 'visible';
}

/**
* Whether the app is focused.
*
* @returns {Boolean}
*/
function hasFocus() {
return document.hasFocus();
}

/**
* Adds event listener for changes in visibility state
*
* @param {Function} callback
*
* @return {Function} removes the listener
*/
function onVisibilityChange(callback) {
function onVisibilityChange(callback: () => void) {
// Deliberately strip callback argument to be consistent across implementations
const subscription = AppState.addEventListener('change', () => callback());

Expand Down
9 changes: 9 additions & 0 deletions src/types/modules/electron.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare global {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface Window {
electron: Electron;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my IDE Electron is of type any

}
}

// We used the export {} line to mark this file as an external module
export {};
Loading