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 appUpdater = new AppUpdater()
const notificationService = new NotificationService() const notificationService = new NotificationService()
// Initialize Python service with main window
pythonService.setMainWindow(mainWindow)
const checkMainWindow = () => { const checkMainWindow = () => {
if (!mainWindow || mainWindow.isDestroyed()) { if (!mainWindow || mainWindow.isDestroyed()) {
throw new Error('Main window does not exist or has been destroyed') throw new Error('Main window does not exist or has been destroyed')

View File

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