refactor: remove main window dependency from PythonService and utilize WindowService for window management (#11116)

* refactor: remove main window dependency from PythonService and utilize WindowService for window management

* format code
This commit is contained in:
beyondkmp 2025-11-03 13:09:40 +08:00 committed by GitHub
parent 9f00f00546
commit 6eaa2b2461
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 12 deletions

View File

@ -115,9 +115,6 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) {
const appUpdater = new AppUpdater()
const notificationService = new NotificationService()
// Initialize Python service with main window
pythonService.setMainWindow(mainWindow)
const checkMainWindow = () => {
if (!mainWindow || mainWindow.isDestroyed()) {
throw new Error('Main window does not exist or has been destroyed')

View File

@ -1,8 +1,9 @@
import { randomUUID } from 'node:crypto'
import type { BrowserWindow } from 'electron'
import { ipcMain } from 'electron'
import { windowService } from './WindowService'
interface PythonExecutionRequest {
id: string
script: string
@ -21,7 +22,6 @@ interface PythonExecutionResponse {
*/
export class PythonService {
private static instance: PythonService | null = null
private mainWindow: BrowserWindow | null = null
private pendingRequests = new Map<string, { resolve: (value: string) => void; reject: (error: Error) => void }>()
private constructor() {
@ -51,10 +51,6 @@ export class PythonService {
})
}
public setMainWindow(mainWindow: BrowserWindow) {
this.mainWindow = mainWindow
}
/**
* Execute Python code by sending request to renderer PyodideService
*/
@ -63,8 +59,8 @@ export class PythonService {
context: Record<string, any> = {},
timeout: number = 60000
): Promise<string> {
if (!this.mainWindow) {
throw new Error('Main window not set in PythonService')
if (!windowService.getMainWindow()) {
throw new Error('Main window not found')
}
return new Promise((resolve, reject) => {
@ -95,7 +91,7 @@ export class PythonService {
// Send request to renderer
const request: PythonExecutionRequest = { id: requestId, script, context, timeout }
this.mainWindow?.webContents.send('python-execution-request', request)
windowService.getMainWindow()?.webContents.send('python-execution-request', request)
})
}
}