try to prevent pipe write error , mainWindow null pointer error

This commit is contained in:
geekgeekrun
2024-02-23 10:38:07 +08:00
parent c97ca5f585
commit 484814459f
4 changed files with 27 additions and 8 deletions

View File

@@ -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,

View File

@@ -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'

View File

@@ -0,0 +1,12 @@
import * as net from 'net'
export const pipeWriteRegardlessError = (
pipe: net.Socket | null,
...writeArgs: Parameters<net.Socket['write']>
) => {
try {
pipe?.write(...writeArgs)
} catch (error) {
console.log('pipe.write Error', error)
}
}

View File

@@ -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', () => {