🐛 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 { electronApp, optimizer, is } from '@electron-toolkit/utils'
import { registerIpcHandlers, setOnProjectChanged } from './ipcHandlers'
import { flushTelemetry } from './telemetry'
import { getDraftRoot, getRecentProjects, writeGlobalConfig } from './globalConfig'
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', () => {
if (process.platform !== 'darwin') {
app.quit()

View File

@@ -24,8 +24,8 @@ async function persistSession(session: CompletedSession): Promise<void> {
await writeTelemetry(data)
}
function endSession(): void {
if (!active) return
function endSession(): Promise<void> {
if (!active) return Promise.resolve()
const endedAt = Date.now()
const files: CompletedSession['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 }
active = null
persistSession(session).catch(console.error)
return persistSession(session)
}
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)
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)
endSession()
return endSession()
}

File diff suppressed because one or more lines are too long