fix: prevent UI freeze after saving signed log entries
Cache plaintext list metadata on entry save so the journal list avoids full decrypt per row, and batch sync pull writes with main-thread yields. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { buildEntryListCache, entryListItemFromLocal } from './entryListCache.js'
|
||||
import type { LocalEntry } from '../services/db.js'
|
||||
|
||||
describe('entryListCache', () => {
|
||||
it('builds cache fields from decrypted entry', async () => {
|
||||
const cache = await buildEntryListCache({
|
||||
date: '2026-06-02',
|
||||
dayOfTravel: '3',
|
||||
departure: 'Kiel',
|
||||
destination: 'Laboe',
|
||||
signSkipper: 'Max'
|
||||
})
|
||||
expect(cache).toEqual({
|
||||
date: '2026-06-02',
|
||||
dayOfTravel: '3',
|
||||
departure: 'Kiel',
|
||||
destination: 'Laboe',
|
||||
skipperSignStatus: 'valid'
|
||||
})
|
||||
})
|
||||
|
||||
it('maps cached local entry to list item', () => {
|
||||
const entry: LocalEntry = {
|
||||
payloadId: 'e1',
|
||||
logbookId: 'lb1',
|
||||
encryptedData: 'x',
|
||||
iv: 'i',
|
||||
tag: 't',
|
||||
updatedAt: '2026-06-02T12:00:00.000Z',
|
||||
listCache: {
|
||||
date: '2026-06-02',
|
||||
dayOfTravel: '1',
|
||||
departure: 'A',
|
||||
destination: 'B',
|
||||
skipperSignStatus: 'none'
|
||||
}
|
||||
}
|
||||
expect(entryListItemFromLocal(entry)).toEqual({
|
||||
id: 'e1',
|
||||
date: '2026-06-02',
|
||||
dayOfTravel: '1',
|
||||
departure: 'A',
|
||||
destination: 'B',
|
||||
updatedAt: '2026-06-02T12:00:00.000Z',
|
||||
skipperSignStatus: 'none'
|
||||
})
|
||||
})
|
||||
|
||||
it('returns null when cache is missing', () => {
|
||||
const entry: LocalEntry = {
|
||||
payloadId: 'e1',
|
||||
logbookId: 'lb1',
|
||||
encryptedData: 'x',
|
||||
iv: 'i',
|
||||
tag: 't',
|
||||
updatedAt: '2026-06-02T12:00:00.000Z'
|
||||
}
|
||||
expect(entryListItemFromLocal(entry)).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,64 @@
|
||||
import { db, type EntryListCache, type LocalEntry } from '../services/db.js'
|
||||
import { getSkipperSignStatus, type SkipperSignStatus } from './signatures.js'
|
||||
|
||||
export type { EntryListCache }
|
||||
|
||||
export interface EntryListItem {
|
||||
id: string
|
||||
date: string
|
||||
dayOfTravel: string
|
||||
departure: string
|
||||
destination: string
|
||||
updatedAt: string
|
||||
skipperSignStatus: SkipperSignStatus
|
||||
}
|
||||
|
||||
export async function buildEntryListCache(decrypted: Record<string, unknown>): Promise<EntryListCache> {
|
||||
return {
|
||||
date: String(decrypted.date || ''),
|
||||
dayOfTravel: String(decrypted.dayOfTravel || ''),
|
||||
departure: String(decrypted.departure || ''),
|
||||
destination: String(decrypted.destination || ''),
|
||||
skipperSignStatus: await getSkipperSignStatus(decrypted)
|
||||
}
|
||||
}
|
||||
|
||||
export function entryListItemFromLocal(entry: LocalEntry): EntryListItem | null {
|
||||
if (!entry.listCache) return null
|
||||
return {
|
||||
id: entry.payloadId,
|
||||
date: entry.listCache.date,
|
||||
dayOfTravel: entry.listCache.dayOfTravel,
|
||||
departure: entry.listCache.departure,
|
||||
destination: entry.listCache.destination,
|
||||
updatedAt: entry.updatedAt,
|
||||
skipperSignStatus: entry.listCache.skipperSignStatus
|
||||
}
|
||||
}
|
||||
|
||||
export type LocalEntryPut = Omit<LocalEntry, 'listCache'> & { listCache?: EntryListCache }
|
||||
|
||||
/** Persist entry ciphertext and optional plaintext list cache for fast journal list loads. */
|
||||
export async function putEntryRecord(
|
||||
record: LocalEntryPut,
|
||||
decryptedForCache?: Record<string, unknown>
|
||||
): Promise<void> {
|
||||
const listCache =
|
||||
record.listCache ??
|
||||
(decryptedForCache ? await buildEntryListCache(decryptedForCache) : undefined)
|
||||
|
||||
await db.entries.put({
|
||||
...record,
|
||||
...(listCache ? { listCache } : {})
|
||||
})
|
||||
}
|
||||
|
||||
/** Backfill list cache after a legacy decrypt — fire-and-forget is fine. */
|
||||
export function persistEntryListCache(
|
||||
payloadId: string,
|
||||
decrypted: Record<string, unknown>
|
||||
): void {
|
||||
void buildEntryListCache(decrypted)
|
||||
.then((listCache) => db.entries.update(payloadId, { listCache }))
|
||||
.catch((err) => console.warn('Failed to persist entry list cache:', err))
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/** Yield so long tasks can interleave with paint and input handling. */
|
||||
export function yieldToMain(): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(resolve, 0)
|
||||
})
|
||||
}
|
||||
|
||||
/** Run an async handler over items in batches, yielding between batches. */
|
||||
export async function forEachInBatches<T>(
|
||||
items: T[],
|
||||
batchSize: number,
|
||||
handler: (item: T) => Promise<void>
|
||||
): Promise<void> {
|
||||
if (items.length === 0) return
|
||||
const size = Math.max(1, batchSize)
|
||||
|
||||
for (let i = 0; i < items.length; i += size) {
|
||||
if (i > 0) await yieldToMain()
|
||||
const batch = items.slice(i, i + size)
|
||||
for (const item of batch) {
|
||||
await handler(item)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user