Skip to content

Commit

Permalink
fix: app unresponsive (#8189)
Browse files Browse the repository at this point in the history
  • Loading branch information
CurryYangxx authored Nov 21, 2024
1 parent c9c722f commit 73b03d0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/insomnia/src/main/ipc/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ export type RendererOnChannels =
| 'toggle-preferences-shortcuts'
| 'toggle-preferences'
| 'toggle-sidebar'
| 'updaterStatus';
| 'updaterStatus'
| 'mainWindowFocusChange';
export const ipcMainOn = (
channel: MainOnChannels,
listener: (
Expand Down
10 changes: 10 additions & 0 deletions packages/insomnia/src/main/window-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,16 @@ export function createWindow({ firstLaunch }: { firstLaunch?: boolean } = {}): E
}
});

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

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

const applicationMenu: MenuItemConstructorOptions = {
label: `${MNEMONIC_SYM}Application`,
submenu: [
Expand Down
28 changes: 24 additions & 4 deletions packages/insomnia/src/ui/components/dropdowns/sync-dropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IconProp } from '@fortawesome/fontawesome-svg-core';
import React, { type FC, Fragment, useEffect, useState } from 'react';
import React, { type FC, Fragment, useCallback, useEffect, useState } from 'react';
import { Button, Collection, Menu, MenuItem, MenuTrigger, Popover, Section, Tooltip, TooltipTrigger } from 'react-aria-components';
import { useFetcher, useParams } from 'react-router-dom';
import { useInterval } from 'react-use';
Expand Down Expand Up @@ -38,6 +38,7 @@ export const SyncDropdown: FC<Props> = ({ gitSyncEnabled }) => {
const [isSyncHistoryModalOpen, setIsSyncHistoryModalOpen] = useState(false);
const [isSyncStagingModalOpen, setIsSyncStagingModalOpen] = useState(false);
const [isSyncBranchesModalOpen, setIsSyncBranchesModalOpen] = useState(false);
const [isWindowFocused, setIsWindowFocused] = useState(true);

const pushFetcher = useFetcher();
const pullFetcher = useFetcher();
Expand All @@ -52,12 +53,31 @@ export const SyncDropdown: FC<Props> = ({ gitSyncEnabled }) => {
}
}, [organizationId, projectId, syncDataLoaderFetcher, workspaceId]);

useInterval(() => {
syncDataActionFetcher.submit({}, {
const triggerSync = useCallback(() => {
const submit = syncDataActionFetcher.submit;
submit({}, {
method: 'POST',
action: `/organization/${organizationId}/project/${projectId}/workspace/${workspaceId}/insomnia-sync/sync-data`,
});
}, ONE_MINUTE_IN_MS);
}, [organizationId, projectId, syncDataActionFetcher.submit, workspaceId]);

useEffect(() => {
const unsubscribe = window.main.on('mainWindowFocusChange', (_, isFocus) => {
setIsWindowFocused(isFocus);
if (isFocus) {
// trigger sync when user comes back to the app
triggerSync();
}
});

return () => {
unsubscribe();
};
}, [triggerSync]);

useInterval(() => {
triggerSync();
}, isWindowFocused ? ONE_MINUTE_IN_MS : null);

const error = checkoutFetcher.data?.error || pullFetcher.data?.error || pushFetcher.data?.error || rollbackFetcher.data?.error;

Expand Down

0 comments on commit 73b03d0

Please sign in to comment.