Skip to content

Commit

Permalink
fix: stop interval when window not focused
Browse files Browse the repository at this point in the history
  • Loading branch information
CurryYangxx committed Oct 15, 2024
1 parent 3878f5b commit b335300
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
5 changes: 4 additions & 1 deletion packages/insomnia/src/main/ipc/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type HandleChannels =
| 'webSocket.open'
| 'webSocket.readyState'
| 'writeFile'
| 'isWindowFocused'
| 'extractJsonFileFromPostmanDataDumpArchive';

export const ipcMainHandle = (
Expand Down Expand Up @@ -86,7 +87,9 @@ export type RendererOnChannels =
| 'toggle-preferences-shortcuts'
| 'toggle-preferences'
| 'toggle-sidebar'
| 'updaterStatus';
| 'updaterStatus'
| 'main-window-focus'
| 'main-window-blur';
export const ipcMainOn = (
channel: MainOnChannels,
listener: (
Expand Down
6 changes: 6 additions & 0 deletions packages/insomnia/src/main/ipc/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { CurlBridgeAPI } from '../network/curl';
import { cancelCurlRequest, curlRequest } from '../network/libcurl-promise';
import { addExecutionStep, completeExecutionStep, getExecution, startExecution, type TimingStep, updateLatestStepName } from '../network/request-timing';
import type { WebSocketBridgeAPI } from '../network/websocket';
import { isWindowFocused } from '../window-utils';
import { ipcMainHandle, ipcMainOn, ipcMainOnce, type RendererOnChannels } from './electron';
import extractPostmanDataDumpHandler from './extractPostmanDataDump';
import type { gRPCBridgeAPI } from './grpc';
Expand All @@ -31,6 +32,7 @@ export interface RendererToMainBridgeAPI {
setMenuBarVisibility: (visible: boolean) => void;
installPlugin: typeof installPlugin;
writeFile: (options: { path: string; content: string }) => Promise<string>;
isWindowFocused: () => Promise<boolean>;
cancelCurlRequest: typeof cancelCurlRequest;
curlRequest: typeof curlRequest;
on: (channel: RendererOnChannels, listener: (event: IpcRendererEvent, ...args: any[]) => void) => () => void;
Expand Down Expand Up @@ -98,6 +100,10 @@ export function registerMainHandlers() {
}
});

ipcMainHandle('isWindowFocused', async () => {
return isWindowFocused();
});

ipcMainHandle('curlRequest', (_, options: Parameters<typeof curlRequest>[0]) => {
return curlRequest(options);
});
Expand Down
17 changes: 16 additions & 1 deletion packages/insomnia/src/main/window-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const DEFAULT_HEIGHT = 720;
const MINIMUM_WIDTH = 500;
const MINIMUM_HEIGHT = 400;

const browserWindows = new Map<'Insomnia' | 'HiddenBrowserWindow', ElectronBrowserWindow>();
export const browserWindows = new Map<'Insomnia' | 'HiddenBrowserWindow', ElectronBrowserWindow>();
let localStorage: LocalStorage | null = null;
let hiddenWindowIsBusy = false;

Expand Down Expand Up @@ -271,6 +271,16 @@ export function createWindow({ firstLaunch }: { firstLaunch?: boolean } = {}): E
}
});

mainBrowserWindow.on('focus', () => {
console.log('[main] window focus');
mainBrowserWindow.webContents.send('main-window-focus');
});

mainBrowserWindow.on('blur', () => {
console.log('[main] window blur');
mainBrowserWindow.webContents.send('main-window-blur');
});

const applicationMenu: MenuItemConstructorOptions = {
label: `${MNEMONIC_SYM}Application`,
submenu: [
Expand Down Expand Up @@ -792,3 +802,8 @@ export function createWindowsAndReturnMain({ firstLaunch }: { firstLaunch?: bool
}
return mainWindow;
}

export const isWindowFocused = () => {
const mainWindow = browserWindows.get('Insomnia');
return mainWindow?.isFocused();
};
1 change: 1 addition & 0 deletions packages/insomnia/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const main: Window['main'] = {
curlRequest: options => ipcRenderer.invoke('curlRequest', options),
cancelCurlRequest: options => ipcRenderer.send('cancelCurlRequest', options),
writeFile: options => ipcRenderer.invoke('writeFile', options),
isWindowFocused: () => ipcRenderer.invoke('isWindowFocused'),
on: (channel, listener) => {
ipcRenderer.on(channel, listener);
return () => ipcRenderer.removeListener(channel, listener);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IconName, IconProp } from '@fortawesome/fontawesome-svg-core';
import React, { type FC, useEffect, useState } from 'react';
import React, { type FC, useEffect, useRef, useState } from 'react';
import { Button, Collection, Menu, MenuItem, MenuTrigger, Popover, Section, Tooltip, TooltipTrigger } from 'react-aria-components';
import { useFetcher, useParams, useRevalidator } from 'react-router-dom';
import { useInterval } from 'react-use';
Expand Down Expand Up @@ -40,6 +40,7 @@ export const GitSyncDropdown: FC<Props> = ({ gitRepository, isInsomniaSyncEnable
const [isGitBranchesModalOpen, setIsGitBranchesModalOpen] = useState(false);
const [isGitLogModalOpen, setIsGitLogModalOpen] = useState(false);
const [isGitStagingModalOpen, setIsGitStagingModalOpen] = useState(false);
const [isWindowFocused, setIsWindowFocused] = useState(false);

const gitPushFetcher = useFetcher<PushToGitRemoteResult>();
const gitPullFetcher = useFetcher<PullFromGitRemoteResult>();
Expand All @@ -48,11 +49,28 @@ export const GitSyncDropdown: FC<Props> = ({ gitRepository, isInsomniaSyncEnable
const gitFetchFetcher = useFetcher<GitFetchLoaderData>();
const gitStatusFetcher = useFetcher<GitStatusResult>();

const isCheckingGitChanges = useRef(false);

const loadingPush = gitPushFetcher.state === 'loading';
const loadingPull = gitPullFetcher.state === 'loading';
const loadingFetch = gitFetchFetcher.state === 'loading';
const loadingStatus = gitStatusFetcher.state === 'loading';

useEffect(() => {
(async () => {
const isWindowFocused = await window.main.isWindowFocused();
setIsWindowFocused(isWindowFocused);
})();

window.main.on('main-window-focus', () => {
setIsWindowFocused(true);
});

window.main.on('main-window-blur', () => {
setIsWindowFocused(false);
});
}, []);

useEffect(() => {
if (
gitRepository?.uri &&
Expand Down Expand Up @@ -85,11 +103,14 @@ export const GitSyncDropdown: FC<Props> = ({ gitRepository, isInsomniaSyncEnable
}
}, [gitStatusFetcher, organizationId, projectId, shouldFetchGitRepoStatus, workspaceId]);

useInterval(() => {
requestIdleCallback(() => {
checkGitChanges(workspaceId);
});
}, 30 * 1000);
useInterval(async () => {
if (isCheckingGitChanges.current) {
return;
}
isCheckingGitChanges.current = true;
await checkGitChanges(workspaceId);
isCheckingGitChanges.current = false;
}, isWindowFocused ? 1000 : null);

useEffect(() => {
if (shouldFetchGitRepoStatus) {
Expand Down

0 comments on commit b335300

Please sign in to comment.