From 1c1b317c112022552692a1a158fabf3ded5ec534 Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Wed, 3 Jun 2020 16:31:03 +0800 Subject: [PATCH] fix: fix fatal error --- src/renderer/components/Application.tsx | 13 ++++++++----- src/renderer/components/Timer/Timer.tsx | 6 ++---- src/renderer/monitor/UsageRecorder.ts | 17 +++++++++-------- src/renderer/monitor/activeWinMonitor.ts | 16 ++++++++++------ 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/renderer/components/Application.tsx b/src/renderer/components/Application.tsx index 7a02edb..30bc526 100644 --- a/src/renderer/components/Application.tsx +++ b/src/renderer/components/Application.tsx @@ -42,7 +42,7 @@ class Application extends React.Component { this.props.fetchSettings(); this.props.fetchKanban(); setTrayImageWithMadeIcon(undefined).then(); - window.addEventListener('error', this.restartApp); + window.addEventListener('error', this.onError); } onKeyDown = (keyname: string) => { @@ -63,16 +63,19 @@ class Application extends React.Component { }; componentWillUnmount() { - window.removeEventListener('error', this.restartApp); + window.removeEventListener('error', this.onError); } - restartApp = () => { - ipcRenderer.send('restart-app', 'error'); + onError = (event: ErrorEvent) => this.handleError(event.error); + handleError = (err: Error) => { + if (process.env.NODE_ENV === 'production') { + ipcRenderer.send('restart-app', 'error'); + } }; componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void { if (process.env.NODE_ENV === 'production') { - this.restartApp(); + this.handleError(error); } } diff --git a/src/renderer/components/Timer/Timer.tsx b/src/renderer/components/Timer/Timer.tsx index a4a7b4c..d64f10c 100644 --- a/src/renderer/components/Timer/Timer.tsx +++ b/src/renderer/components/Timer/Timer.tsx @@ -598,7 +598,7 @@ class Timer extends Component { private onMaskClick = () => { this.setState({ showMask: false }); - this.onSessionConfirmed().catch(console.error); + this.onSessionConfirmed(); }; private onMaskButtonClick = async () => { @@ -682,9 +682,7 @@ class Timer extends Component { }; componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void { - if (process.env.NODE_ENV === 'production') { - ipcRenderer.send('restart-app', 'error'); - } + message.error(error.toString()); } render() { diff --git a/src/renderer/monitor/UsageRecorder.ts b/src/renderer/monitor/UsageRecorder.ts index 843a4ed..2a4f795 100644 --- a/src/renderer/monitor/UsageRecorder.ts +++ b/src/renderer/monitor/UsageRecorder.ts @@ -28,7 +28,7 @@ export class UsageRecorder { apps: {}, spentTimeInHour: 0, switchTimes: 0, - startTime: 0 + startTime: 0, }; this.monitorListener = monitorListener; @@ -51,7 +51,7 @@ export class UsageRecorder { apps: {}, spentTimeInHour: 0, switchTimes: 0, - startTime: 0 + startTime: 0, }; this.maxIndex = 0; @@ -82,7 +82,7 @@ export class UsageRecorder { /** * - * @param result, undefined means the timer is stopped + * @param result, undefined means the timer is stopped, letting recorder to record the last app info * @param screenshot, the path to screenshot */ listener: ActiveWinListener = async (result?: BaseResult, screenshot?: string) => { @@ -97,7 +97,7 @@ export class UsageRecorder { this.record.screenshots.push({ time: new Date().getTime(), - path: screenshot + path: screenshot, }); } @@ -111,7 +111,8 @@ export class UsageRecorder { this.updateLastAppUsageInfo(this.lastUsingApp); } - if (!appName || !result) { + if (!appName || !result || !this.isRunning) { + // the app may have stopped, but the listener may still be invoked return; } @@ -120,7 +121,7 @@ export class UsageRecorder { this.record.apps[appName] = { appName, spentTimeInHour: 0, - titleSpentTime: {} + titleSpentTime: {}, }; } @@ -133,7 +134,7 @@ export class UsageRecorder { private updateLastAppUsageInfo = (lastAppName: string) => { const row = this.record.apps[lastAppName]; if (!row) { - throw new Error( + console.error( `App "${lastAppName}" does not exists in ${JSON.stringify(this.record.apps)}` ); } @@ -155,7 +156,7 @@ export class UsageRecorder { row.titleSpentTime[title] = { index: this.maxIndex, normalizedWeight: 0, - occurrence: 0 + occurrence: 0, }; this.maxIndex += 1; diff --git a/src/renderer/monitor/activeWinMonitor.ts b/src/renderer/monitor/activeWinMonitor.ts index 61b9c2e..3a6de00 100644 --- a/src/renderer/monitor/activeWinMonitor.ts +++ b/src/renderer/monitor/activeWinMonitor.ts @@ -52,12 +52,16 @@ export class Monitor { watch = async () => { const data = await activeWin(); if (data) { - if (this.shouldTakeScreenshot) { - this.shouldTakeScreenshot = false; - const screenshot = await getScreen(500); - this.listener(data, screenshot); - } else { - this.listener(data); + try { + if (this.shouldTakeScreenshot) { + this.shouldTakeScreenshot = false; + const screenshot = await getScreen(500); + this.listener(data, screenshot); + } else { + this.listener(data); + } + } catch (e) { + console.error(e); } } };