|
| 1 | +import { dirname, join } from "node:path"; |
| 2 | +import { fileURLToPath } from "node:url"; |
| 3 | +import type { Action, AppState, EmailId } from "@mail-client-demo/model"; |
| 4 | +import { |
| 5 | + generateEmails, |
| 6 | + Inbox, |
| 7 | + makeInitialState, |
| 8 | + openDatabase, |
| 9 | + reduce, |
| 10 | + syncStateToDisk, |
| 11 | +} from "@mail-client-demo/model"; |
| 12 | +import { app, BrowserWindow, ipcMain } from "electron"; |
| 13 | + |
| 14 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 15 | + |
| 16 | +// Big enough that the naive implementations FEEL slow: |
| 17 | +// buildThreads (O(n²)) ~1.5–2 s |
| 18 | +// sync (full JSON snapshot of 50k emails) ~150–300 ms PER ACTION |
| 19 | +// bulkArchive of 500 ids ~150–250 ms |
| 20 | +// Every IPC mutation runs through store.dispatch -> reduce -> syncStateToDisk, |
| 21 | +// so the sync cost is paid on every keystroke / button click and every UI |
| 22 | +// interaction feels the lag. Optimization will collapse all of it. |
| 23 | +const EMAIL_COUNT = 50_000; |
| 24 | + |
| 25 | +let state: AppState | null = null; |
| 26 | +let db: ReturnType<typeof openDatabase> | null = null; |
| 27 | + |
| 28 | +interface Timed<T> { |
| 29 | + result: T; |
| 30 | + durationMs: number; |
| 31 | +} |
| 32 | + |
| 33 | +function timed<T>(fn: () => T): Timed<T> { |
| 34 | + const t0 = performance.now(); |
| 35 | + const result = fn(); |
| 36 | + return { result, durationMs: performance.now() - t0 }; |
| 37 | +} |
| 38 | + |
| 39 | +function dispatch(action: Action): { durationMs: number; syncMs: number } { |
| 40 | + const t0 = performance.now(); |
| 41 | + if (!state || !db) return { durationMs: 0, syncMs: 0 }; |
| 42 | + state = reduce(state, action); |
| 43 | + const sync = syncStateToDisk(state, db); |
| 44 | + return { |
| 45 | + durationMs: performance.now() - t0, |
| 46 | + syncMs: sync.durationMs, |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Sync the current state to disk. Called from every read-only IPC handler so |
| 52 | + * the audit-trail file stays current with each user interaction (and so the |
| 53 | + * naive full-snapshot cost is felt on every UI op, not just mutations). |
| 54 | + */ |
| 55 | +function syncCurrentState(): number { |
| 56 | + if (!state || !db) return 0; |
| 57 | + return syncStateToDisk(state, db).durationMs; |
| 58 | +} |
| 59 | + |
| 60 | +function getInbox(): Inbox { |
| 61 | + return new Inbox(state ? state.emails : []); |
| 62 | +} |
| 63 | + |
| 64 | +function createWindow() { |
| 65 | + const win = new BrowserWindow({ |
| 66 | + width: 1200, |
| 67 | + height: 800, |
| 68 | + title: "Inbox", |
| 69 | + backgroundColor: "#0f1419", |
| 70 | + webPreferences: { |
| 71 | + preload: join(__dirname, "../preload/index.mjs"), |
| 72 | + contextIsolation: true, |
| 73 | + sandbox: false, |
| 74 | + }, |
| 75 | + }); |
| 76 | + |
| 77 | + if (process.env.ELECTRON_RENDERER_URL) { |
| 78 | + win.loadURL(process.env.ELECTRON_RENDERER_URL); |
| 79 | + } else { |
| 80 | + win.loadFile(join(__dirname, "../renderer/index.html")); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +app.whenReady().then(() => { |
| 85 | + const seedStart = performance.now(); |
| 86 | + const emails = generateEmails({ count: EMAIL_COUNT }); |
| 87 | + state = makeInitialState(emails); |
| 88 | + const dbPath = join(app.getPath("userData"), "state.json"); |
| 89 | + db = openDatabase(dbPath); |
| 90 | + console.log( |
| 91 | + `[main] seeded ${EMAIL_COUNT} emails in ${(performance.now() - seedStart).toFixed(1)} ms`, |
| 92 | + ); |
| 93 | + console.log(`[main] persistence path: ${dbPath}`); |
| 94 | + |
| 95 | + // Initial sync so disk reflects the seeded state. |
| 96 | + const initialSync = syncStateToDisk(state, db); |
| 97 | + console.log(`[main] initial sync: ${initialSync.durationMs.toFixed(1)} ms`); |
| 98 | + |
| 99 | + ipcMain.handle("inbox:list", () => { |
| 100 | + const t = timed(() => getInbox().sortByDate(getInbox().visible()).slice(0, 200)); |
| 101 | + const syncMs = syncCurrentState(); |
| 102 | + return { ...t, syncMs }; |
| 103 | + }); |
| 104 | + |
| 105 | + ipcMain.handle("inbox:search", (_evt, query: string) => { |
| 106 | + const t = timed(() => getInbox().sortByDate(getInbox().search(query)).slice(0, 50)); |
| 107 | + const syncMs = syncCurrentState(); |
| 108 | + return { ...t, syncMs }; |
| 109 | + }); |
| 110 | + |
| 111 | + ipcMain.handle("inbox:threads", () => { |
| 112 | + const t = timed(() => { |
| 113 | + const threads = getInbox().buildThreads(); |
| 114 | + threads.sort((a, b) => b.lastReceivedAt - a.lastReceivedAt); |
| 115 | + return threads.slice(0, 50); |
| 116 | + }); |
| 117 | + const syncMs = syncCurrentState(); |
| 118 | + return { ...t, syncMs }; |
| 119 | + }); |
| 120 | + |
| 121 | + ipcMain.handle("inbox:archive", (_evt, ids: EmailId[]) => { |
| 122 | + const { durationMs, syncMs } = dispatch({ type: "ARCHIVE", ids, at: Date.now() }); |
| 123 | + return { result: ids.length, durationMs, syncMs }; |
| 124 | + }); |
| 125 | + |
| 126 | + ipcMain.handle("inbox:stats", () => { |
| 127 | + if (!state) return { total: 0, visible: 0, pendingOps: 0 }; |
| 128 | + const inbox = getInbox(); |
| 129 | + return { |
| 130 | + total: state.emails.length, |
| 131 | + visible: inbox.visible().length, |
| 132 | + pendingOps: state.pendingOps.length, |
| 133 | + }; |
| 134 | + }); |
| 135 | + |
| 136 | + createWindow(); |
| 137 | +}); |
| 138 | + |
| 139 | +app.on("window-all-closed", () => { |
| 140 | + if (process.platform !== "darwin") app.quit(); |
| 141 | +}); |
| 142 | + |
| 143 | +app.on("activate", () => { |
| 144 | + if (BrowserWindow.getAllWindows().length === 0) createWindow(); |
| 145 | +}); |
0 commit comments