Skip to content

Commit 13c98ef

Browse files
committed
Migrate Wine Manager status state to Zustand
State logic in general was sort-of handled by the Backend and sort-of handled by the Frontend. This was changed to now only be the Backend's job
1 parent e891f2a commit 13c98ef

File tree

12 files changed

+129
-162
lines changed

12 files changed

+129
-162
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@
189189
"steam-shortcut-editor": "3.1.3",
190190
"tslib": "2.5.0",
191191
"xvfb-maybe": "^0.2.1",
192-
"zod": "3.22.3"
192+
"zod": "3.22.3",
193+
"zustand": "^4.4.7"
193194
},
194195
"scripts": {
195196
"start": "vite",

src/backend/api/wine.ts

+8-13
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import {
33
RuntimeName,
44
ToolArgs,
55
WineVersionInfo,
6-
ProgressInfo,
7-
State,
8-
Runner
6+
Runner,
7+
type WineManagerStatus
98
} from 'common/types'
109

1110
export const toggleDXVK = async (args: ToolArgs) =>
@@ -27,11 +26,10 @@ export const showItemInFolder = (installDir: string) =>
2726
ipcRenderer.send('showItemInFolder', installDir)
2827
export const installWineVersion = async (
2928
release: WineVersionInfo
30-
): Promise<'error' | 'abort' | 'success'> =>
31-
ipcRenderer.invoke('installWineVersion', release)
29+
): Promise<void> => ipcRenderer.invoke('installWineVersion', release)
3230
export const removeWineVersion = async (
3331
release: WineVersionInfo
34-
): Promise<boolean> => ipcRenderer.invoke('removeWineVersion', release)
32+
): Promise<void> => ipcRenderer.invoke('removeWineVersion', release)
3533
export const refreshWineVersionInfo = async (fetch?: boolean): Promise<void> =>
3634
ipcRenderer.invoke('refreshWineVersionInfo', fetch)
3735

@@ -48,18 +46,15 @@ export const handleProgressOfWinetricks = (
4846
}
4947

5048
export const handleProgressOfWineManager = (
51-
version: string,
5249
callback: (
5350
e: Electron.IpcRendererEvent,
54-
progress: {
55-
state: State
56-
progress: ProgressInfo
57-
}
51+
version: string,
52+
progress: WineManagerStatus
5853
) => void
5954
): (() => void) => {
60-
ipcRenderer.on('progressOfWineManager' + version, callback)
55+
ipcRenderer.on('progressOfWineManager', callback)
6156
return () => {
62-
ipcRenderer.removeListener('progressOfWineManager' + version, callback)
57+
ipcRenderer.removeListener('progressOfWineManager', callback)
6358
}
6459
}
6560

src/backend/utils.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import {
77
Release,
88
GameInfo,
99
GameSettings,
10-
State,
11-
ProgressInfo,
1210
GameStatus
1311
} from 'common/types'
1412
import axios from 'axios'
@@ -83,6 +81,7 @@ import EasyDl from 'easydl'
8381
import decompress from '@xhmikosr/decompress'
8482
import decompressTargz from '@xhmikosr/decompress-targz'
8583
import decompressTarxz from '@felipecrs/decompress-tarxz'
84+
import type { WineManagerStatus } from 'common/types'
8685

8786
const execAsync = promisify(exec)
8887

@@ -870,11 +869,8 @@ export async function downloadDefaultWine() {
870869
}
871870

872871
// download the latest version
873-
const onProgress = (state: State, progress?: ProgressInfo) => {
874-
sendFrontendMessage('progressOfWineManager' + release.version, {
875-
state,
876-
progress
877-
})
872+
const onProgress = (state: WineManagerStatus) => {
873+
sendFrontendMessage('progressOfWineManager', release.version, state)
878874
}
879875
const result = await installWineVersion(release, onProgress)
880876

src/backend/wine/manager/downloader/main.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ import {
1818
WINECROSSOVER_URL,
1919
WINESTAGINGMACOS_URL
2020
} from './constants'
21-
import { VersionInfo, Repositorys, State, ProgressInfo } from 'common/types'
21+
import { VersionInfo, Repositorys } from 'common/types'
2222
import {
2323
fetchReleases,
2424
getFolderSize,
2525
unlinkFile,
2626
unzipFile
2727
} from './utilities'
2828
import { calculateEta, downloadFile } from 'backend/utils'
29+
import type { WineManagerStatus } from 'common/types'
2930

3031
interface getVersionsProps {
3132
repositorys?: Repositorys[]
@@ -151,7 +152,7 @@ interface installProps {
151152
versionInfo: VersionInfo
152153
installDir: string
153154
overwrite?: boolean
154-
onProgress?: (state: State, progress?: ProgressInfo) => void
155+
onProgress?: (state: WineManagerStatus) => void
155156
abortSignal?: AbortSignal
156157
}
157158

@@ -248,7 +249,8 @@ async function installVersion({
248249
versionInfo.downsize
249250
)
250251

251-
onProgress('downloading', {
252+
onProgress({
253+
status: 'downloading',
252254
percentage,
253255
eta: eta!,
254256
avgSpeed: downloadSpeed

src/backend/wine/manager/downloader/utilities.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as axios from 'axios'
33
import { existsSync, statSync, unlinkSync } from 'graceful-fs'
44
import { spawnSync } from 'child_process'
55

6-
import { ProgressInfo, State, VersionInfo, Type } from 'common/types'
6+
import { VersionInfo, Type, type WineManagerStatus } from 'common/types'
77
import { extractFiles } from 'backend/utils'
88

99
interface fetchProps {
@@ -117,7 +117,7 @@ interface unzipProps {
117117
filePath: string
118118
unzipDir: string
119119
overwrite?: boolean
120-
onProgress: (state: State, progress?: ProgressInfo) => void
120+
onProgress: (state: WineManagerStatus) => void
121121
abortSignal?: AbortSignal
122122
}
123123

@@ -152,15 +152,15 @@ async function unzipFile({
152152

153153
extractFiles({ path: filePath, destination: unzipDir, strip: 1 })
154154
.then(() => {
155-
onProgress('idle')
155+
onProgress({ status: 'idle' })
156156
resolve(`Succesfully unzip ${filePath} to ${unzipDir}.`)
157157
})
158158
.catch((error) => {
159-
onProgress('idle')
159+
onProgress({ status: 'idle' })
160160
reject(`Unzip of ${filePath} failed with:\n ${error}!`)
161161
})
162162

163-
onProgress('unzipping')
163+
onProgress({ status: 'unzipping' })
164164
})
165165
}
166166

+34-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,45 @@
11
import { ipcMain } from 'electron'
2-
import { ProgressInfo, State } from 'common/types'
32
import {
43
installWineVersion,
54
removeWineVersion,
65
updateWineVersionInfos
76
} from './utils'
87
import { logError, LogPrefix } from '../../logger/logger'
98
import { sendFrontendMessage } from '../../main_window'
9+
import type { WineManagerStatus } from 'common/types'
10+
import { notify } from '../../dialog/dialog'
11+
import { t } from 'i18next'
1012

1113
ipcMain.handle('installWineVersion', async (e, release) => {
12-
const onProgress = (state: State, progress?: ProgressInfo) => {
13-
sendFrontendMessage('progressOfWineManager' + release.version, {
14-
state,
15-
progress
16-
})
14+
const onProgress = (state: WineManagerStatus) => {
15+
sendFrontendMessage('progressOfWineManager', release.version, state)
1716
}
17+
18+
notify({ title: release.version, body: t('notify.install.startInstall') })
19+
onProgress({
20+
status: 'downloading',
21+
percentage: 0,
22+
avgSpeed: 0,
23+
eta: '00:00:00'
24+
})
25+
1826
const result = await installWineVersion(release, onProgress)
19-
return result
27+
28+
let notifyBody: string | null = null
29+
switch (result) {
30+
case 'error':
31+
notifyBody = t('notify.install.error')
32+
break
33+
case 'abort':
34+
notifyBody = t('notify.install.canceled')
35+
break
36+
case 'success':
37+
notifyBody = t('notify.install.finished')
38+
}
39+
if (notifyBody) notify({ title: release.version, body: notifyBody })
40+
onProgress({
41+
status: 'idle'
42+
})
2043
})
2144

2245
ipcMain.handle('refreshWineVersionInfo', async (e, fetch?) => {
@@ -29,6 +52,7 @@ ipcMain.handle('refreshWineVersionInfo', async (e, fetch?) => {
2952
}
3053
})
3154

32-
ipcMain.handle('removeWineVersion', async (e, release) =>
33-
removeWineVersion(release)
34-
)
55+
ipcMain.handle('removeWineVersion', async (e, release) => {
56+
const result = await removeWineVersion(release)
57+
if (result) notify({ title: release.version, body: t('notify.uninstalled') })
58+
})

src/backend/wine/manager/utils.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import { existsSync, mkdirSync, rmSync } from 'graceful-fs'
77
import { logError, logInfo, LogPrefix, logWarning } from '../../logger/logger'
88
import {
99
WineVersionInfo,
10-
ProgressInfo,
1110
Repositorys,
12-
State,
13-
VersionInfo
11+
VersionInfo,
12+
WineManagerStatus
1413
} from 'common/types'
1514

1615
import { getAvailableVersions, installVersion } from './downloader/main'
@@ -89,7 +88,7 @@ async function updateWineVersionInfos(
8988

9089
async function installWineVersion(
9190
release: WineVersionInfo,
92-
onProgress: (state: State, progress?: ProgressInfo) => void
91+
onProgress: (status: WineManagerStatus) => void
9392
) {
9493
let updatedInfo: WineVersionInfo
9594
const variant = release.hasUpdate ? 'update' : 'installation'

src/common/typedefs/ipcBridge.d.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,9 @@ interface AsyncIPCFunctions {
222222
}) => Promise<void>
223223
isNative: (args: { appName: string; runner: Runner }) => boolean
224224
getLogContent: (appNameOrRunner: string) => string
225-
installWineVersion: (
226-
release: WineVersionInfo
227-
) => Promise<'error' | 'abort' | 'success'>
225+
installWineVersion: (release: WineVersionInfo) => Promise<void>
228226
refreshWineVersionInfo: (fetch?: boolean) => Promise<void>
229-
removeWineVersion: (release: WineVersionInfo) => Promise<boolean>
227+
removeWineVersion: (release: WineVersionInfo) => Promise<void>
230228
shortcutsExists: (appName: string, runner: Runner) => boolean
231229
addToSteam: (appName: string, runner: Runner) => Promise<boolean>
232230
removeFromSteam: (appName: string, runner: Runner) => Promise<void>

src/common/types.ts

+3-13
Original file line numberDiff line numberDiff line change
@@ -711,19 +711,9 @@ export enum Repositorys {
711711
WINESTAGINGMACOS
712712
}
713713

714-
/**
715-
* Type for the progress callback state
716-
*/
717-
export type State = 'downloading' | 'unzipping' | 'idle'
718-
719-
/**
720-
* Interface for the information that progress callback returns
721-
*/
722-
export interface ProgressInfo {
723-
percentage: number
724-
avgSpeed: number
725-
eta: string
726-
}
714+
export type WineManagerStatus =
715+
| { status: 'idle' | 'unzipping' }
716+
| { status: 'downloading'; percentage: number; avgSpeed: number; eta: string }
727717

728718
export interface WineManagerUISettings {
729719
value: string

0 commit comments

Comments
 (0)