🐛 fix session tracking

This commit is contained in:
TC
2026-06-11 19:59:43 +10:00
parent 171490cae3
commit e309845289
3 changed files with 13 additions and 7 deletions

View File

@@ -2,6 +2,7 @@ import { app, BrowserWindow, shell, nativeImage, Menu, dialog } from 'electron'
import { join } from 'path' import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils' import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import { registerIpcHandlers, setOnProjectChanged } from './ipcHandlers' import { registerIpcHandlers, setOnProjectChanged } from './ipcHandlers'
import { flushTelemetry } from './telemetry'
import { getDraftRoot, getRecentProjects, writeGlobalConfig } from './globalConfig' import { getDraftRoot, getRecentProjects, writeGlobalConfig } from './globalConfig'
app.setName('Hohoff') app.setName('Hohoff')
@@ -290,6 +291,11 @@ app.whenReady().then(() => {
}) })
}) })
app.on('before-quit', (event) => {
event.preventDefault()
flushTelemetry().then(() => app.exit(0)).catch(() => app.exit(0))
})
app.on('window-all-closed', () => { app.on('window-all-closed', () => {
if (process.platform !== 'darwin') { if (process.platform !== 'darwin') {
app.quit() app.quit()

View File

@@ -24,8 +24,8 @@ async function persistSession(session: CompletedSession): Promise<void> {
await writeTelemetry(data) await writeTelemetry(data)
} }
function endSession(): void { function endSession(): Promise<void> {
if (!active) return if (!active) return Promise.resolve()
const endedAt = Date.now() const endedAt = Date.now()
const files: CompletedSession['files'] = {} const files: CompletedSession['files'] = {}
for (const [path, snap] of Object.entries(active.files)) { for (const [path, snap] of Object.entries(active.files)) {
@@ -38,7 +38,7 @@ function endSession(): void {
} }
const session: CompletedSession = { id: active.id, startedAt: active.startedAt, endedAt, files } const session: CompletedSession = { id: active.id, startedAt: active.startedAt, endedAt, files }
active = null active = null
persistSession(session).catch(console.error) return persistSession(session)
} }
export function onWordSnapshot(filePath: string, wordCount: number): void { export function onWordSnapshot(filePath: string, wordCount: number): void {
@@ -58,10 +58,10 @@ export function onWordSnapshot(filePath: string, wordCount: number): void {
} }
if (active.idleTimer) clearTimeout(active.idleTimer) if (active.idleTimer) clearTimeout(active.idleTimer)
active.idleTimer = setTimeout(endSession, IDLE_MS) active.idleTimer = setTimeout(() => { endSession().catch(console.error) }, IDLE_MS)
} }
export function flushTelemetry(): void { export function flushTelemetry(): Promise<void> {
if (active?.idleTimer) clearTimeout(active.idleTimer) if (active?.idleTimer) clearTimeout(active.idleTimer)
endSession() return endSession()
} }

File diff suppressed because one or more lines are too long