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

[Tech] Make IPC typing simpler & more expandable #3819

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Prev Previous commit
Make progressUpdate one channel
The Frontend already filters this to only registered appNames
Ideally this would be refactored to go in a Zustand store, but one thing at a
time
  • Loading branch information
CommandMC committed Jul 2, 2024
commit 4585d62b86a0e52444aafcd5652c38883e7f9cbf
11 changes: 1 addition & 10 deletions src/backend/api/library.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { GameStatus } from 'common/types'
import {
makeListenerCaller as lc,
makeHandlerInvoker as hi,
Expand All @@ -19,15 +18,7 @@ export const importGame = hi('importGame')

export const handleGameStatus = fls('gameStatusUpdate')

export const onProgressUpdate = (
appName: string,
onChange: (e: Electron.IpcRendererEvent, status: GameStatus) => void
) => {
ipcRenderer.on(`progressUpdate-${appName}`, onChange)
return () => {
ipcRenderer.removeListener(`progressUpdate-${appName}`, onChange)
}
}
export const onProgressUpdate = fls('progressUpdate')

export const handleLaunchGame = fls('launchGame')

Expand Down
6 changes: 3 additions & 3 deletions src/backend/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ export async function moveOnWindows(
const filenameMatch = data.match(/([\w.:\\]+)$/)?.[1]
if (filenameMatch) currentFile = filenameMatch

sendFrontendMessage(`progressUpdate-${gameInfo.app_name}`, {
sendFrontendMessage(`progressUpdate`, {
appName: gameInfo.app_name,
runner: gameInfo.runner,
status: 'moving',
Expand Down Expand Up @@ -1146,7 +1146,7 @@ export async function moveOnUnix(
}
}

sendFrontendMessage(`progressUpdate-${gameInfo.app_name}`, {
sendFrontendMessage(`progressUpdate`, {
appName: gameInfo.app_name,
runner: gameInfo.runner,
status: 'moving',
Expand Down Expand Up @@ -1250,7 +1250,7 @@ function sendGameStatusUpdate(payload: GameStatus) {
}

function sendProgressUpdate(payload: GameStatus) {
sendFrontendMessage(`progressUpdate-${payload.appName}`, payload)
sendFrontendMessage(`progressUpdate`, payload)
backendEvents.emit(`progressUpdate-${payload.appName}`, payload)
}

Expand Down
2 changes: 1 addition & 1 deletion src/common/ipc/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ export interface FrontendMessages {
progressOfWineManager: (version: string, progress: WineManagerStatus) => void
'installing-winetricks-component': (component: string) => void

[key: `progressUpdate${string}`]: (progress: GameStatus) => void
progressUpdate: (progress: GameStatus) => void

// Used inside tests, so we can be a bit lenient with the type checking here
message: (...params: unknown[]) => void
Expand Down
6 changes: 2 additions & 4 deletions src/frontend/hooks/hasProgress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ export const hasProgress = (appName: string) => {
})
}
}
const setGameStatusRemoveListener = window.api.onProgressUpdate(
appName,
handleProgressUpdate
)
const setGameStatusRemoveListener =
window.api.onProgressUpdate(handleProgressUpdate)

return () => {
setGameStatusRemoveListener()
Expand Down
Loading