Просмотр исходного кода

fix: 修复pipe初始化连接问题

jiaxing.liao недель назад: 3
Родитель
Сommit
15e12281fc

+ 6 - 0
src/main/index.ts

@@ -7,6 +7,7 @@ import { handleFile, unlockProjectFolder } from './files'
 import { ensurePipeServerRunning, stopSpawnedPipeServer } from './pipe-server'
 
 let pipeClient: PipeClient | null = null
+let pipeStartupSettled = false
 
 function createWindow(): void {
   const mainWindow = new BrowserWindow({
@@ -60,6 +61,10 @@ app.whenReady().then(async () => {
 
   ipcMain.on('ping', () => console.log('pong'))
   ipcMain.on('connect-pipe', () => {
+    if (!pipeStartupSettled) {
+      return
+    }
+
     pipeClient?.connect()
   })
   ipcMain.on('disconnect-pipe', () => {
@@ -84,6 +89,7 @@ app.whenReady().then(async () => {
     console.error('[pipe] ensurePipeServerRunning failed:', error)
   }
 
+  pipeStartupSettled = true
   pipeClient?.connect()
 
   app.on('activate', () => {

+ 14 - 27
src/main/pipe-server.ts

@@ -1,7 +1,6 @@
 import { spawn, type ChildProcess } from 'node:child_process'
-import { existsSync } from 'node:fs'
+import { existsSync, readdirSync } from 'node:fs'
 import { join } from 'node:path'
-import { connect } from 'node:net'
 import { getPipeBundleDir } from './pipe-path'
 import { PIPE_PATH } from './client'
 
@@ -16,34 +15,22 @@ function getPipeServerExecutablePath(): string {
   return join(dir, 'pipe')
 }
 
-function probeNamedPipe(timeoutMs: number): Promise<boolean> {
-  return new Promise((resolve) => {
-    let finished = false
-    const socket = connect(PIPE_PATH)
-    const timer = setTimeout(() => {
-      if (finished) return
-      finished = true
-      socket.destroy()
-      resolve(false)
-    }, timeoutMs)
-
-    const done = (ok: boolean) => {
-      if (finished) return
-      finished = true
-      clearTimeout(timer)
-      socket.destroy()
-      resolve(ok)
-    }
+function getPipeName(pipePath: string): string {
+  return pipePath.replace(/^\\\\\.\\pipe\\/, '')
+}
 
-    socket.once('connect', () => done(true))
-    socket.once('error', () => done(false))
-  })
+function hasNamedPipe(pipePath: string): boolean {
+  try {
+    return readdirSync('\\\\.\\pipe\\').includes(getPipeName(pipePath))
+  } catch {
+    return false
+  }
 }
 
-async function waitUntilPipeAccepts(totalMs: number, intervalMs: number): Promise<void> {
+async function waitUntilPipeAppears(totalMs: number, intervalMs: number): Promise<void> {
   const deadline = Date.now() + totalMs
   while (Date.now() < deadline) {
-    if (await probeNamedPipe(500)) {
+    if (hasNamedPipe(PIPE_PATH)) {
       return
     }
     await new Promise((r) => setTimeout(r, intervalMs))
@@ -63,7 +50,7 @@ export async function ensurePipeServerRunning(): Promise<void> {
     return
   }
 
-  if (await probeNamedPipe(300)) {
+  if (hasNamedPipe(PIPE_PATH)) {
     console.log('[pipe] pipe server already running')
     return
   }
@@ -96,7 +83,7 @@ export async function ensurePipeServerRunning(): Promise<void> {
     }
   })
 
-  await waitUntilPipeAccepts(25_000, 200)
+  await waitUntilPipeAppears(25_000, 200)
 }
 
 export function stopSpawnedPipeServer(): void {

+ 1 - 1
src/renderer/src/lvgl-widgets/barcode/index.ts

@@ -2,7 +2,7 @@
 import icon from '../assets/icon/icon_41barcode.svg'
 import type { IComponentModelConfig } from '../type'
 import i18n from '@/locales'
-import { stateList, flagOptions, stateOptions, DEFAULT_THEME_KEY } from '@/constants'
+import { stateList, flagOptions, stateOptions } from '@/constants'
 import defaultStyle from './style.json'
 import { getCode128BUnitLength } from './code128'
 

+ 1 - 1
src/renderer/src/lvgl-widgets/canvas/index.tsx

@@ -1,6 +1,6 @@
 import Canvas from './Canvas.vue'
 import icon from '../assets/icon/icon_25canvas.svg'
-import { flagOptions, stateOptions, DEFAULT_THEME_KEY } from '@/constants'
+import { flagOptions, stateOptions } from '@/constants'
 import type { IComponentModelConfig } from '../type'
 import i18n from '@/locales'
 import Config from './Config.vue'

+ 1 - 1
src/renderer/src/lvgl-widgets/led/index.ts

@@ -1,6 +1,6 @@
 import Led from './Led.vue'
 import icon from '../assets/icon/icon_23Led.svg'
-import { flagOptions, stateOptions, DEFAULT_THEME_KEY } from '@/constants'
+import { flagOptions, stateOptions } from '@/constants'
 import type { IComponentModelConfig } from '../type'
 import i18n from '@/locales'
 

+ 1 - 1
src/renderer/src/lvgl-widgets/video/index.tsx

@@ -2,7 +2,7 @@
 import icon from '../assets/icon/icon_37video.svg'
 import type { IComponentModelConfig } from '../type'
 import i18n from '@/locales'
-import { flagOptions, stateOptions, stateList, DEFAULT_THEME_KEY } from '@/constants'
+import { flagOptions, stateOptions, stateList } from '@/constants'
 import defaultStyle from './style.json'
 
 export default {