From 484814459f711218fcf5fb41cd73bb62812d1a4a Mon Sep 17 00:00:00 2001 From: geekgeekrun Date: Fri, 23 Feb 2024 10:38:07 +0800 Subject: [PATCH] try to prevent pipe write error , mainWindow null pointer error --- .../flow/CHECK_AND_DOWNLOAD_DEPENDENCIES/index.ts | 7 +++++-- .../src/main/flow/GEEK_AUTO_START_CHAT_WITH_BOSS.ts | 10 +++++++--- packages/ui/src/main/flow/utils/pipe.ts | 12 ++++++++++++ packages/ui/src/main/window/mainWindow.ts | 6 +++--- 4 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 packages/ui/src/main/flow/utils/pipe.ts diff --git a/packages/ui/src/main/flow/CHECK_AND_DOWNLOAD_DEPENDENCIES/index.ts b/packages/ui/src/main/flow/CHECK_AND_DOWNLOAD_DEPENDENCIES/index.ts index ad78f5d..390391a 100644 --- a/packages/ui/src/main/flow/CHECK_AND_DOWNLOAD_DEPENDENCIES/index.ts +++ b/packages/ui/src/main/flow/CHECK_AND_DOWNLOAD_DEPENDENCIES/index.ts @@ -1,6 +1,7 @@ import { app } from 'electron' import checkAndDownloadPuppeteer from './check-and-download-puppeteer' import * as net from 'net' +import { pipeWriteRegardlessError } from '../utils/pipe' export enum DOWNLOAD_ERROR_EXIT_CODE { NO_ERROR = 0, @@ -16,7 +17,8 @@ export const checkAndDownloadDependenciesForInit = async () => { console.warn('pipe is not available') } - pipe?.write( + pipeWriteRegardlessError( + pipe, JSON.stringify({ type: 'NEED_RESETUP_DEPENDENCIES' }) + '\r\n' @@ -35,7 +37,8 @@ export const checkAndDownloadDependenciesForInit = async () => { }, 30 * 1000) } console.log(downloadedBytes / totalBytes) - pipe?.write( + pipeWriteRegardlessError( + pipe, JSON.stringify({ type: 'PUPPETEER_DOWNLOAD_PROGRESS', totalBytes, diff --git a/packages/ui/src/main/flow/GEEK_AUTO_START_CHAT_WITH_BOSS.ts b/packages/ui/src/main/flow/GEEK_AUTO_START_CHAT_WITH_BOSS.ts index b084acc..05b70c8 100644 --- a/packages/ui/src/main/flow/GEEK_AUTO_START_CHAT_WITH_BOSS.ts +++ b/packages/ui/src/main/flow/GEEK_AUTO_START_CHAT_WITH_BOSS.ts @@ -6,6 +6,7 @@ import * as net from 'net' import { checkPuppeteerExecutable, } from './CHECK_AND_DOWNLOAD_DEPENDENCIES/check-and-download-puppeteer' +import { pipeWriteRegardlessError } from './utils/pipe' const { groupRobotAccessToken: dingTalkAccessToken } = readConfigFile('dingtalk.json') @@ -31,14 +32,16 @@ export const runAutoChat = async () => { } catch { console.warn('pipe is not available') } - pipe?.write( + pipeWriteRegardlessError( + pipe, JSON.stringify({ type: 'INITIALIZE_PUPPETEER' }) + '\r\n' ) try { await initPuppeteer() - pipe?.write( + pipeWriteRegardlessError( + pipe, JSON.stringify({ type: 'PUPPETEER_INITIALIZE_SUCCESSFULLY' }) + '\r\n' @@ -65,7 +68,8 @@ export const runAutoChat = async () => { errorEncounter: new SyncHook(['errorInfo']) } initPlugins(hooks) - pipe?.write( + pipeWriteRegardlessError( + pipe, JSON.stringify({ type: 'GEEK_AUTO_START_CHAT_WITH_BOSS_STARTED' //geek-auto-start-chat-with-boss-started }) + '\r\n' diff --git a/packages/ui/src/main/flow/utils/pipe.ts b/packages/ui/src/main/flow/utils/pipe.ts new file mode 100644 index 0000000..69961e8 --- /dev/null +++ b/packages/ui/src/main/flow/utils/pipe.ts @@ -0,0 +1,12 @@ +import * as net from 'net' + +export const pipeWriteRegardlessError = ( + pipe: net.Socket | null, + ...writeArgs: Parameters +) => { + try { + pipe?.write(...writeArgs) + } catch (error) { + console.log('pipe.write Error', error) + } +} diff --git a/packages/ui/src/main/window/mainWindow.ts b/packages/ui/src/main/window/mainWindow.ts index 87b460d..3c12609 100644 --- a/packages/ui/src/main/window/mainWindow.ts +++ b/packages/ui/src/main/window/mainWindow.ts @@ -122,7 +122,7 @@ export function createMainWindow(): void { // means cannot find downloaded puppeteer reject('NEED_TO_CHECK_RUNTIME_DEPENDENCIES') } else { - mainWindow.webContents.send('geek-auto-start-chat-with-boss-stopped') + mainWindow?.webContents.send('geek-auto-start-chat-with-boss-stopped') } }) }) @@ -162,7 +162,7 @@ export function createMainWindow(): void { switch (data.type) { case 'NEED_RESETUP_DEPENDENCIES': case 'PUPPETEER_DOWNLOAD_PROGRESS': { - mainWindow.webContents.send(data.type, data) + mainWindow?.webContents.send(data.type, data) break } default: { @@ -188,7 +188,7 @@ export function createMainWindow(): void { }) ipcMain.handle('stop-geek-auto-start-chat-with-boss', async () => { - mainWindow.webContents.send('geek-auto-start-chat-with-boss-stopping') + mainWindow?.webContents.send('geek-auto-start-chat-with-boss-stopping') subProcessOfPuppeteer?.kill('SIGINT') }) ipcMain.on('open-project-homepage-on-github', () => {