Skip to content

Commit

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

[No QA][TS migration] Migrate 'Visibility' lib to TypeScript
  • Loading branch information
flodnv authored Sep 20, 2023
2 parents 52dc94d + 5a9f707 commit b028686
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -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());
Expand All @@ -34,7 +22,7 @@ function onVisibilityChange(callback) {
window.electron.removeAllListeners(ELECTRON_EVENTS.FOCUS);
window.electron.removeAllListeners(ELECTRON_EVENTS.BLUR);
};
}
};

export default {
isVisible,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 5 additions & 16 deletions src/libs/Visibility/index.js → src/libs/Visibility/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
5 changes: 5 additions & 0 deletions src/libs/Visibility/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type IsVisible = () => boolean;
type HasFocus = () => boolean;
type OnVisibilityChange = (callback: () => void) => () => void;

export type {IsVisible, HasFocus, OnVisibilityChange};
18 changes: 18 additions & 0 deletions src/types/modules/electron.d.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>;
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 {};

0 comments on commit b028686

Please sign in to comment.