From bcb2cbc9c6576c0248cbc8eb2f8ec528a87dfd5d Mon Sep 17 00:00:00 2001 From: Conner <65339198+connercsbn@users.noreply.github.com> Date: Sun, 22 May 2022 10:52:34 -0700 Subject: [PATCH 1/2] allow for separate library refreshes --- electron/main.ts | 17 +++++++++++------ src/components/UI/ActionIcons/index.tsx | 7 +++++-- src/screens/Game/GameSubMenu/index.tsx | 2 +- src/screens/Library/index.tsx | 1 + src/state/GlobalState.tsx | 23 +++++++++++++++++------ src/types.ts | 3 ++- 6 files changed, 37 insertions(+), 16 deletions(-) diff --git a/electron/main.ts b/electron/main.ts index dea4321458..22f8f0b5f7 100644 --- a/electron/main.ts +++ b/electron/main.ts @@ -597,9 +597,13 @@ ipcMain.handle( /// IPC handlers begin here. -ipcMain.handle('checkGameUpdates', async () => { - const legendaryUpdates = await LegendaryLibrary.get().listUpdateableGames() - const gogUpdates = await GOGLibrary.get().listUpdateableGames() +ipcMain.handle('checkGameUpdates', async (event, libraries) => { + const legendaryUpdates = libraries.includes('epic') + ? await LegendaryLibrary.get().listUpdateableGames() + : [] + const gogUpdates = libraries.includes('gog') + ? await GOGLibrary.get().listUpdateableGames() + : [] return [...legendaryUpdates, ...gogUpdates] }) @@ -754,10 +758,11 @@ if (existsSync(installed)) { }) } -ipcMain.handle('refreshLibrary', async (e, fullRefresh) => { +ipcMain.handle('refreshLibrary', async (e, fullRefresh, libraries) => { await Promise.allSettled([ - GOGLibrary.get().sync(), - LegendaryLibrary.get().getGames('info', fullRefresh) + libraries.includes('epic') && + LegendaryLibrary.get().getGames('info', fullRefresh), + libraries.includes('gog') && GOGLibrary.get().sync() ]) }) diff --git a/src/components/UI/ActionIcons/index.tsx b/src/components/UI/ActionIcons/index.tsx index 8e42d6de41..a1817818b0 100644 --- a/src/components/UI/ActionIcons/index.tsx +++ b/src/components/UI/ActionIcons/index.tsx @@ -21,13 +21,15 @@ interface Props { sortInstalled: boolean toggleSortDescending: () => void toggleSortinstalled: () => void + category: string } export default function ActionIcons({ sortDescending, toggleSortDescending, sortInstalled, - toggleSortinstalled + toggleSortinstalled, + category }: Props) { const { t } = useTranslation() const { refreshLibrary, handleLayout, layout } = useContext(ContextProvider) @@ -85,7 +87,8 @@ export default function ActionIcons({ refreshLibrary({ checkForUpdates: true, fullRefresh: true, - runInBackground: false + runInBackground: false, + libraries: [category] }) } > diff --git a/src/screens/Game/GameSubMenu/index.tsx b/src/screens/Game/GameSubMenu/index.tsx index 809e82e65a..ca284889a5 100644 --- a/src/screens/Game/GameSubMenu/index.tsx +++ b/src/screens/Game/GameSubMenu/index.tsx @@ -79,7 +79,7 @@ export default function GamesSubmenu({ }) if (path) { await renderer.invoke('changeInstallPath', [appName, path, runner]) - await refresh() + await refresh([runner]) } return } diff --git a/src/screens/Library/index.tsx b/src/screens/Library/index.tsx index 93245e1579..43c5498a4e 100644 --- a/src/screens/Library/index.tsx +++ b/src/screens/Library/index.tsx @@ -105,6 +105,7 @@ export default function Library(): JSX.Element { sortDescending={sortDescending} toggleSortDescending={() => handleSortDescending()} sortInstalled={sortInstalled} + category={category} toggleSortinstalled={() => handleSortInstalled()} /> diff --git a/src/state/GlobalState.tsx b/src/state/GlobalState.tsx index 43224b0006..a90f99e9ec 100644 --- a/src/state/GlobalState.tsx +++ b/src/state/GlobalState.tsx @@ -237,7 +237,8 @@ export class GlobalState extends PureComponent { this.handleCategory(runner) this.refreshLibrary({ fullRefresh: true, - runInBackground: false + runInBackground: false, + libraries: [runner] }) } @@ -289,7 +290,10 @@ export class GlobalState extends PureComponent { window.location.reload() } - refresh = async (checkUpdates?: boolean): Promise => { + refresh = async ( + libraries: string[], + checkUpdates?: boolean + ): Promise => { console.log('refreshing') let updates = this.state.gameUpdates @@ -309,7 +313,7 @@ export class GlobalState extends PureComponent { try { updates = checkUpdates - ? await ipcRenderer.invoke('checkGameUpdates') + ? await ipcRenderer.invoke('checkGameUpdates', libraries) : this.state.gameUpdates } catch (error) { ipcRenderer.send('logError', error) @@ -338,7 +342,8 @@ export class GlobalState extends PureComponent { refreshLibrary = async ({ checkForUpdates, fullRefresh, - runInBackground = true + runInBackground = true, + libraries = ['gog', 'epic'] }: RefreshOptions): Promise => { if (this.state.refreshing) return @@ -348,11 +353,11 @@ export class GlobalState extends PureComponent { }) ipcRenderer.send('logInfo', 'Refreshing Library') try { - await ipcRenderer.invoke('refreshLibrary', fullRefresh) + await ipcRenderer.invoke('refreshLibrary', fullRefresh, libraries) } catch (error) { ipcRenderer.send('logError', error) } - this.refresh(checkForUpdates) + this.refresh(libraries, checkForUpdates) } refreshWineVersionInfo = async (fetch: boolean): Promise => { @@ -531,10 +536,16 @@ export class GlobalState extends PureComponent { recentGames }) + const libraries: string[] = [ + gogUser && 'gog', + legendaryUser && 'epic' + ].filter(Boolean) as string[] + if (legendaryUser || gogUser) { this.refreshLibrary({ checkForUpdates: true, fullRefresh: true, + libraries, runInBackground: Boolean(epic.library.length) }) } diff --git a/src/types.ts b/src/types.ts index d76501a25a..f869ff11c4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -70,7 +70,7 @@ export interface ContextType { libraryTopSection: string handleLibraryTopSection: (value: LibraryTopSectionOptions) => void platform: NodeJS.Platform | string - refresh: (checkUpdates?: boolean) => Promise + refresh: (libraries: string[], checkUpdates?: boolean) => Promise refreshLibrary: (options: RefreshOptions) => Promise refreshWineVersionInfo: (fetch: boolean) => void refreshing: boolean @@ -267,6 +267,7 @@ export interface Path { export type RefreshOptions = { checkForUpdates?: boolean fullRefresh?: boolean + libraries?: string[] runInBackground?: boolean } From 260afd4f595dd79af2573555eea9461b72b6d8e3 Mon Sep 17 00:00:00 2001 From: Conner <65339198+connercsbn@users.noreply.github.com> Date: Mon, 23 May 2022 08:26:07 -0700 Subject: [PATCH 2/2] Change library parameter to Runner type; use switch statements --- electron/main.ts | 41 ++++++++++++++++--------- src/components/UI/ActionIcons/index.tsx | 9 +++--- src/screens/Game/GameSubMenu/index.tsx | 2 +- src/screens/Library/index.tsx | 2 +- src/state/GlobalState.tsx | 34 +++++++++----------- src/types.ts | 4 +-- 6 files changed, 50 insertions(+), 42 deletions(-) diff --git a/electron/main.ts b/electron/main.ts index 22f8f0b5f7..463c300e23 100644 --- a/electron/main.ts +++ b/electron/main.ts @@ -597,14 +597,18 @@ ipcMain.handle( /// IPC handlers begin here. -ipcMain.handle('checkGameUpdates', async (event, libraries) => { - const legendaryUpdates = libraries.includes('epic') - ? await LegendaryLibrary.get().listUpdateableGames() - : [] - const gogUpdates = libraries.includes('gog') - ? await GOGLibrary.get().listUpdateableGames() - : [] - return [...legendaryUpdates, ...gogUpdates] +ipcMain.handle('checkGameUpdates', async (event, library?: Runner) => { + switch (library) { + case 'legendary': + return LegendaryLibrary.get().listUpdateableGames() + case 'gog': + return GOGLibrary.get().listUpdateableGames() + default: + return [ + ...(await LegendaryLibrary.get().listUpdateableGames()), + ...(await GOGLibrary.get().listUpdateableGames()) + ] + } }) ipcMain.handle('getEpicGamesStatus', async () => isEpicServiceOffline()) @@ -758,12 +762,21 @@ if (existsSync(installed)) { }) } -ipcMain.handle('refreshLibrary', async (e, fullRefresh, libraries) => { - await Promise.allSettled([ - libraries.includes('epic') && - LegendaryLibrary.get().getGames('info', fullRefresh), - libraries.includes('gog') && GOGLibrary.get().sync() - ]) +ipcMain.handle('refreshLibrary', async (e, fullRefresh, library?: Runner) => { + switch (library) { + case 'legendary': + await LegendaryLibrary.get().getGames('info', fullRefresh) + break + case 'gog': + await GOGLibrary.get().sync() + break + default: + await Promise.allSettled([ + LegendaryLibrary.get().getGames('info', fullRefresh), + GOGLibrary.get().sync() + ]) + break + } }) ipcMain.on('logError', (e, err) => logError(`${err}`, LogPrefix.Frontend)) diff --git a/src/components/UI/ActionIcons/index.tsx b/src/components/UI/ActionIcons/index.tsx index a1817818b0..327782c190 100644 --- a/src/components/UI/ActionIcons/index.tsx +++ b/src/components/UI/ActionIcons/index.tsx @@ -15,21 +15,22 @@ import { useTranslation } from 'react-i18next' import ContextProvider from 'src/state/ContextProvider' import FormControl from '../FormControl' import './index.css' +import { Runner } from 'src/types' interface Props { sortDescending: boolean sortInstalled: boolean toggleSortDescending: () => void toggleSortinstalled: () => void - category: string + library: Runner } export default function ActionIcons({ + library, sortDescending, toggleSortDescending, sortInstalled, - toggleSortinstalled, - category + toggleSortinstalled }: Props) { const { t } = useTranslation() const { refreshLibrary, handleLayout, layout } = useContext(ContextProvider) @@ -88,7 +89,7 @@ export default function ActionIcons({ checkForUpdates: true, fullRefresh: true, runInBackground: false, - libraries: [category] + library }) } > diff --git a/src/screens/Game/GameSubMenu/index.tsx b/src/screens/Game/GameSubMenu/index.tsx index ca284889a5..e4eeb43364 100644 --- a/src/screens/Game/GameSubMenu/index.tsx +++ b/src/screens/Game/GameSubMenu/index.tsx @@ -79,7 +79,7 @@ export default function GamesSubmenu({ }) if (path) { await renderer.invoke('changeInstallPath', [appName, path, runner]) - await refresh([runner]) + await refresh(runner) } return } diff --git a/src/screens/Library/index.tsx b/src/screens/Library/index.tsx index 43c5498a4e..869559de42 100644 --- a/src/screens/Library/index.tsx +++ b/src/screens/Library/index.tsx @@ -105,7 +105,7 @@ export default function Library(): JSX.Element { sortDescending={sortDescending} toggleSortDescending={() => handleSortDescending()} sortInstalled={sortInstalled} - category={category} + library={category === 'epic' ? 'legendary' : 'gog'} toggleSortinstalled={() => handleSortInstalled()} /> diff --git a/src/state/GlobalState.tsx b/src/state/GlobalState.tsx index a90f99e9ec..5703580bd7 100644 --- a/src/state/GlobalState.tsx +++ b/src/state/GlobalState.tsx @@ -8,6 +8,7 @@ import { InstalledInfo, LibraryTopSectionOptions, RefreshOptions, + Runner, WineVersionInfo } from 'src/types' import { TFunction, withTranslation } from 'react-i18next' @@ -232,13 +233,13 @@ export class GlobalState extends PureComponent { this.setState({ libraryTopSection: value }) } - handleSuccessfulLogin = (runner: 'epic' | 'gog') => { + handleSuccessfulLogin = (runner: Runner) => { this.handleFilter('all') this.handleCategory(runner) this.refreshLibrary({ fullRefresh: true, runInBackground: false, - libraries: [runner] + library: runner }) } @@ -254,7 +255,7 @@ export class GlobalState extends PureComponent { } }) - this.handleSuccessfulLogin('epic') + this.handleSuccessfulLogin('legendery' as Runner) } return response.status @@ -290,10 +291,7 @@ export class GlobalState extends PureComponent { window.location.reload() } - refresh = async ( - libraries: string[], - checkUpdates?: boolean - ): Promise => { + refresh = async (library?: Runner, checkUpdates?: boolean): Promise => { console.log('refreshing') let updates = this.state.gameUpdates @@ -313,7 +311,7 @@ export class GlobalState extends PureComponent { try { updates = checkUpdates - ? await ipcRenderer.invoke('checkGameUpdates', libraries) + ? await ipcRenderer.invoke('checkGameUpdates', library) : this.state.gameUpdates } catch (error) { ipcRenderer.send('logError', error) @@ -343,7 +341,7 @@ export class GlobalState extends PureComponent { checkForUpdates, fullRefresh, runInBackground = true, - libraries = ['gog', 'epic'] + library = undefined }: RefreshOptions): Promise => { if (this.state.refreshing) return @@ -353,11 +351,11 @@ export class GlobalState extends PureComponent { }) ipcRenderer.send('logInfo', 'Refreshing Library') try { - await ipcRenderer.invoke('refreshLibrary', fullRefresh, libraries) + await ipcRenderer.invoke('refreshLibrary', fullRefresh, library) } catch (error) { ipcRenderer.send('logError', error) } - this.refresh(libraries, checkForUpdates) + this.refresh(library, checkForUpdates) } refreshWineVersionInfo = async (fetch: boolean): Promise => { @@ -412,7 +410,8 @@ export class GlobalState extends PureComponent { appName, status, folder, - progress + progress, + runner }: GameStatus) => { const { libraryStatus, gameUpdates } = this.state const currentApp = libraryStatus.filter( @@ -445,7 +444,8 @@ export class GlobalState extends PureComponent { // This avoids calling legendary again before the previous process is killed when canceling this.refreshLibrary({ checkForUpdates: true, - runInBackground: true + runInBackground: true, + library: runner }) storage.setItem( @@ -459,7 +459,7 @@ export class GlobalState extends PureComponent { }) } - this.refreshLibrary({ runInBackground: true }) + this.refreshLibrary({ runInBackground: true, library: runner }) this.setState({ libraryStatus: newLibraryStatus }) } } @@ -536,16 +536,10 @@ export class GlobalState extends PureComponent { recentGames }) - const libraries: string[] = [ - gogUser && 'gog', - legendaryUser && 'epic' - ].filter(Boolean) as string[] - if (legendaryUser || gogUser) { this.refreshLibrary({ checkForUpdates: true, fullRefresh: true, - libraries, runInBackground: Boolean(epic.library.length) }) } diff --git a/src/types.ts b/src/types.ts index f869ff11c4..45bb88e813 100644 --- a/src/types.ts +++ b/src/types.ts @@ -70,7 +70,7 @@ export interface ContextType { libraryTopSection: string handleLibraryTopSection: (value: LibraryTopSectionOptions) => void platform: NodeJS.Platform | string - refresh: (libraries: string[], checkUpdates?: boolean) => Promise + refresh: (library: Runner, checkUpdates?: boolean) => Promise refreshLibrary: (options: RefreshOptions) => Promise refreshWineVersionInfo: (fetch: boolean) => void refreshing: boolean @@ -267,7 +267,7 @@ export interface Path { export type RefreshOptions = { checkForUpdates?: boolean fullRefresh?: boolean - libraries?: string[] + library?: Runner runInBackground?: boolean }