perf: sprint C — отделить history от IPC state-broadcast
#9 AppState больше не содержит `history` (вынесено в PersistedState — internal store-shape). `broadcastState()` и IPC.getState шлют через IPC только exercises/settings/challenges/gamesEnabled. Раньше каждый markDone/snooze/toggle вызывал broadcastState() → весь state, включая до 10k history-записей (~500KB JSON), летел через IPC к каждому BrowserWindow и парсился в renderer'е. На долгом горизонте использования становилось заметным лагом UI. Renderer и раньше историю из state не читал (Dashboard вызывает IPC.getHistory отдельно), так что это чистый perf-win без behavioural change. Store-internal mutations продолжают работать с полным PersistedState через `getState()`; renderer-bound IPC использует новый `getStateForRenderer()`. Не сделано из спринта C: zustand setState refactor (#8) — текущая архитектура работает корректно (zustand bathes), `?? []` fallback'и в селекторах безопасны. Реальный gain был от #9, который и закрыт.
This commit is contained in:
@@ -17,6 +17,7 @@ import {
|
||||
GameId,
|
||||
HistoryAction,
|
||||
HistoryEntry,
|
||||
PersistedState,
|
||||
SAMPLE_EXERCISES,
|
||||
Settings
|
||||
} from '@shared/types'
|
||||
@@ -30,7 +31,7 @@ const HISTORY_MAX = 10_000
|
||||
const WRITE_DEBOUNCE_MS = 1500
|
||||
const WRITE_RETRY_DELAYS = [50, 200, 800] // ms backoff on transient EBUSY/EPERM
|
||||
|
||||
let cache: AppState | null = null
|
||||
let cache: PersistedState | null = null
|
||||
let storePath = ''
|
||||
let pendingWrite: NodeJS.Timeout | null = null
|
||||
|
||||
@@ -43,7 +44,7 @@ function getStorePath(): string {
|
||||
return storePath
|
||||
}
|
||||
|
||||
function makeInitial(): AppState {
|
||||
function makeInitial(): PersistedState {
|
||||
const now = Date.now()
|
||||
return {
|
||||
exercises: SAMPLE_EXERCISES.map((e) => ({
|
||||
@@ -144,8 +145,8 @@ function runMigrations(s: StoredState): StoredState {
|
||||
return cursor
|
||||
}
|
||||
|
||||
/** Coerce a (possibly partial) migrated state into a fully-formed AppState. */
|
||||
function coerce(s: StoredState): AppState {
|
||||
/** Coerce a (possibly partial) migrated state into a fully-formed PersistedState. */
|
||||
function coerce(s: StoredState): PersistedState {
|
||||
return {
|
||||
exercises: Array.isArray(s.exercises) ? (s.exercises as Exercise[]) : [],
|
||||
settings: {
|
||||
@@ -162,7 +163,7 @@ function coerce(s: StoredState): AppState {
|
||||
}
|
||||
}
|
||||
|
||||
function load(): AppState {
|
||||
function load(): PersistedState {
|
||||
const p = getStorePath()
|
||||
if (!existsSync(p)) {
|
||||
const initial = makeInitial()
|
||||
@@ -325,11 +326,34 @@ function scheduleWrite(): void {
|
||||
pendingWrite.unref?.()
|
||||
}
|
||||
|
||||
export function getState(): AppState {
|
||||
/**
|
||||
* Internal persisted state — единственный source of truth. Включает историю.
|
||||
* Mutate напрямую (mutations внутри store.ts), затем scheduleWrite().
|
||||
*/
|
||||
export function getState(): PersistedState {
|
||||
if (!cache) cache = load()
|
||||
return cache
|
||||
}
|
||||
|
||||
/**
|
||||
* State для отправки renderer'у. Копия БЕЗ `history` — историю renderer
|
||||
* запрашивает отдельным IPC.getHistory. Раньше каждый markDone/snooze
|
||||
* отправлял весь state через evtStateChanged, и при 10k entries в истории
|
||||
* это 500KB JSON × N IPC mutations подряд → заметный лаг.
|
||||
*
|
||||
* Возвращаемая копия безопасна для мутации (ipc.ts накладывает на settings
|
||||
* актуальное OS-значение startWithWindows) — мы НЕ мутируем cache.
|
||||
*/
|
||||
export function getStateForRenderer(): AppState {
|
||||
const p = getState()
|
||||
return {
|
||||
exercises: p.exercises,
|
||||
settings: p.settings,
|
||||
challenges: p.challenges,
|
||||
gamesEnabled: p.gamesEnabled
|
||||
}
|
||||
}
|
||||
|
||||
export function getSettings(): Settings {
|
||||
return getState().settings
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user