codeburn/app/renderer/lib/ipc.ts
iamtoruk e22e5511ba feat(app): electron shell, 30s timer, and typed IPC bridge
- main.ts: 1200x820 BrowserWindow (contextIsolation:true,
  nodeIntegration:false, sandbox:true), loads the Vite dev URL or the
  built index.html, registers one ipcMain.handle per CodeburnBridge
  channel, and starts a 30s tick that emits `codeburn:refresh`.
  createBridgeHandlers() is a pure, injectable channel→argv map so the
  spawn→IPC wiring is unit-testable without a GUI (main.test.ts).
- preload.ts: exposes window.codeburn (CodeburnBridge, exactly) and
  window.codeburnEvents.onRefresh via contextBridge. Handlers return an
  {ok,value|error} envelope so the structured CliError kind survives the
  world boundary.
- types.ts: mirrors MenubarPayload/DailyHistoryEntry/DailyModelBreakdown
  and the other CLI payload types verbatim, plus the exact CodeburnBridge
  interface and Period union.
- ipc.ts: typed 1:1 wrapper + normalizeCliError.
2026-07-10 15:17:03 -07:00

32 lines
1.2 KiB
TypeScript

import type { CliError, CodeburnBridge } from './types'
// The preload script (app/electron/preload.ts) exposes `window.codeburn` as the
// typed CodeburnBridge (methods resolve to CLI JSON or reject with a plain
// CliError), and `window.codeburnEvents` for main→renderer push events.
declare global {
interface Window {
codeburn: CodeburnBridge
codeburnEvents: {
/** Subscribe to the main-process 30s refresh tick. Returns an unsubscribe fn. */
onRefresh(cb: () => void): () => void
}
}
}
/** The typed bridge. Import this instead of touching `window` directly. */
export const codeburn: CodeburnBridge = window.codeburn
/** Subscribe to the main-process refresh tick. */
export function onRefresh(cb: () => void): () => void {
return window.codeburnEvents.onRefresh(cb)
}
/** Coerce anything thrown across the IPC boundary into a CliError shape. */
export function normalizeCliError(err: unknown): CliError {
if (err && typeof err === 'object' && 'kind' in err && typeof (err as CliError).kind === 'string') {
const e = err as CliError
return { kind: e.kind, message: e.message ?? 'codeburn CLI error' }
}
const message = err instanceof Error ? err.message : String(err)
return { kind: 'nonzero', message }
}