Skip to content

perf(desktop): debounce electron store persistence#37587

Open
Hona wants to merge 1 commit into
anomalyco:devfrom
Hona:debounced-desktop-store
Open

perf(desktop): debounce electron store persistence#37587
Hona wants to merge 1 commit into
anomalyco:devfrom
Hona:debounced-desktop-store

Conversation

@Hona

@Hona Hona commented Jul 18, 2026

Copy link
Copy Markdown
Member

Summary

Applies an in-memory debounced write buffer (\BufferedStore) for \�lectron-store\ instances in the desktop main process.

Details

  • Batches and debounces \store-set, \store-delete, and \store-clear\ file flushes to avoid synchronous main-thread disk I/O stalls during frequent state updates.
  • Instantly returns pending in-memory mutations for subsequent reads (\store-get) prior to disk flush.
  • Registers an explicit \ lushAllStores()\ call on app shutdown (\�efore-quit\ / \will-quit).

@Hona
Hona requested a review from Brendonovich as a code owner July 18, 2026 02:00
Copilot AI review requested due to automatic review settings July 18, 2026 02:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a buffered/debounced wrapper around electron-store in the Desktop main process to reduce synchronous disk I/O frequency by batching mutations in memory and flushing them later, with an explicit flush on shutdown.

Changes:

  • Added BufferedStore to buffer set/delete/clear operations and provide read-your-writes semantics before persistence.
  • Updated store lifecycle utilities to flush pending writes before deleting/cleaning store files.
  • Added a shutdown hook in the Electron main process to flush all buffered stores before quitting, plus unit tests for core buffering behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.

File Description
packages/desktop/src/main/store.ts Adds BufferedStore, switches the store cache to buffered instances, and introduces flushAllStores() plus pre-delete flushes.
packages/desktop/src/main/store.test.ts Adds tests for buffered read-before-flush, explicit flush persistence, and buffered deletes.
packages/desktop/src/main/index.ts Flushes buffered stores on before-quit and will-quit shutdown events.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +92 to +95
private scheduleFlush() {
if (this.timer) return
this.timer = setTimeout(() => this.flush(), this.delay)
}
@@ -0,0 +1,58 @@
import { describe, expect, test, beforeEach, afterEach, vi } from "bun:test"
Comment on lines +19 to +21
const underlying = new MemoryStore()
const store = new BufferedStore(underlying as any, 100)

Comment on lines +32 to +34
const underlying = new MemoryStore()
const store = new BufferedStore(underlying as any, 100)

Comment on lines +44 to +48
const underlying = new MemoryStore()
underlying.set("key1", "value1")

const store = new BufferedStore(underlying as any, 100)
store.delete("key1")
Comment on lines 223 to 227
app.on("before-quit", () => {
setAppQuitting()
flushAllStores()
void stopSidecars()
})
Comment on lines +4 to +15
class MemoryStore {
public data = new Map<string, unknown>()
get(key: string) {
return this.data.get(key)
}
set(key: string, value: unknown) {
this.data.set(key, value)
}
delete(key: string) {
this.data.delete(key)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants