Skip to content

Open source personal finance application

fix(stores): only the first empty emission keeps primed cache

Ilya Komicheva7aac72c663d
diff --git a/app/app/components/categories/useCategoriesStore.test.ts b/app/app/components/categories/useCategoriesStore.test.ts
index 26841d4e..ad50f30e 100644
--- a/app/app/components/categories/useCategoriesStore.test.ts
+++ b/app/app/components/categories/useCategoriesStore.test.ts
@@ -75,12 +75,21 @@ describe('useCategoriesStore', () => {
expect(store.items.c1).toMatchObject({ icon: 'mdi:cat' })
expect(store.items.adjustment).toBeDefined() // synthetic re-added
- // TEMP: cache-first guard - an empty emission must not wipe primed/cached items.
+ // An empty emission after the first one is a genuine wipe and must apply.
h.watchCallbacks[0]!([])
- expect(store.hasItems).toBe(true)
+ expect(store.hasItems).toBe(false)
expect(store.items.transfer).toBeDefined()
})
+ it('keeps primed items on an empty FIRST emission (sync not landed yet)', () => {
+ const store = useCategoriesStore()
+ store.initCategories()
+
+ store.setCategories({ c1: { color: '', icon: 'mdi:cat', name: 'C1', parentId: 0, showInLastUsed: false, showInQuickSelector: false } } as never)
+ h.watchCallbacks[0]!([])
+ expect(store.hasItems).toBe(true)
+ })
+
it('flips isLoaded on the first emission and resets it on re-subscribe', () => {
const store = useCategoriesStore()
expect(store.isLoaded).toBe(false)
diff --git a/app/app/components/categories/useCategoriesStore.ts b/app/app/components/categories/useCategoriesStore.ts
index 987b78ec..20f720af 100644
--- a/app/app/components/categories/useCategoriesStore.ts
+++ b/app/app/components/categories/useCategoriesStore.ts
@@ -187,11 +187,15 @@ export const useCategoriesStore = defineStore('categories', (): CategoriesStore
return
watchController?.abort()
isLoaded.value = false // re-subscribe (e.g. different user) waits for a fresh emission
+ let isFirstEmit = true
watchController = watchTable<Row>('SELECT * FROM categories', [], (rows) => {
+ const isFirst = isFirstEmit
+ isFirstEmit = false
isLoaded.value = true
if (!rows.length) {
- // TEMP: keep cached data instead of wiping it with an empty first emission.
- if (hasItems.value)
+ // An empty FIRST emission means local SQLite hasn't received the first sync yet
+ // (fresh device) - keep the primed cache. Later empty emissions are genuine wipes.
+ if (isFirst && hasItems.value)
return
setCategories(null)
return
diff --git a/app/app/components/trns/useTrnsStore.test.ts b/app/app/components/trns/useTrnsStore.test.ts
index 7b2ff44e..1e852c02 100644
--- a/app/app/components/trns/useTrnsStore.test.ts
+++ b/app/app/components/trns/useTrnsStore.test.ts
@@ -68,8 +68,17 @@ describe('useTrnsStore', () => {
expect(store.items?.a).toMatchObject({ amount: 100, categoryId: 'food', type: 0 })
})
- // TEMP: cache-first guard - an empty emission must not wipe primed/cached items.
- it('keeps existing items on an empty emission', () => {
+ it('keeps primed items on an empty FIRST emission (sync not landed yet)', () => {
+ const store = useTrnsStore()
+ store.initTrns()
+ const emit = h.watchCallbacks[0]!
+
+ store.setTrns({ a: { amount: 100, categoryId: 'food', date: 1, type: 0 } } as never)
+ emit([])
+ expect(store.items?.a).toBeDefined()
+ })
+
+ it('applies an empty later emission (genuine wipe)', () => {
const store = useTrnsStore()
store.initTrns()
const emit = h.watchCallbacks[0]!
@@ -78,7 +87,7 @@ describe('useTrnsStore', () => {
expect(store.items?.a).toBeDefined()
emit([])
- expect(store.items?.a).toBeDefined()
+ expect(store.items).toBeNull()
})
it('does not subscribe in demo mode', () => {
diff --git a/app/app/components/trns/useTrnsStore.ts b/app/app/components/trns/useTrnsStore.ts
index 16ec0782..b5d7afc5 100644
--- a/app/app/components/trns/useTrnsStore.ts
+++ b/app/app/components/trns/useTrnsStore.ts
@@ -125,19 +125,20 @@ export const useTrnsStore = defineStore('trns', () => {
isLoaded.value = false // re-subscribe (e.g. different user) waits for a fresh emission
let isFirstEmit = true
watchController = watchTable<Row>('SELECT * FROM trns', [], (rows) => {
+ const isFirst = isFirstEmit
+ isFirstEmit = false
isLoaded.value = true
- // TEMP: keep cached data instead of wiping it with an empty first emission.
- if (!rows.length && hasItems.value)
+ // An empty FIRST emission means local SQLite hasn't received the first sync yet
+ // (fresh device) - keep the primed cache. Later empty emissions are genuine wipes.
+ if (isFirst && !rows.length && hasItems.value)
return
const prev = items.value
// Cold-start instrumentation: per-row materialize+transform cost vs the SQLite scan
// (`ps:watch:trns` measure) - feeds the blob-cache architecture decision.
- const transformStart = isFirstEmit ? performance.now() : 0
+ const transformStart = isFirst ? performance.now() : 0
const next = prev ? reconcileTrns(prev, rows) : rowsToTrns(rows)
- if (isFirstEmit) {
- isFirstEmit = false
+ if (isFirst)
performance.measure('trns:first-transform', { duration: performance.now() - transformStart, start: transformStart })
- }
// reconcileTrns returns `prev` (same ref) when nothing changed - skip the rebuild
// (this is the echo of our own optimistic write).
if (next !== prev)
diff --git a/app/app/components/wallets/useWalletsStore.test.ts b/app/app/components/wallets/useWalletsStore.test.ts
index b25f0adb..9ddfb0b3 100644
--- a/app/app/components/wallets/useWalletsStore.test.ts
+++ b/app/app/components/wallets/useWalletsStore.test.ts
@@ -68,9 +68,18 @@ describe('useWalletsStore', () => {
expect(Object.keys(store.items ?? {})).toEqual(['a', 'b'])
expect(store.items?.a).toMatchObject({ currency: 'USD', type: 'cash' })
- // TEMP: cache-first guard - an empty emission must not wipe primed/cached items.
+ // An empty emission after the first one is a genuine wipe and must apply.
h.watchCallbacks[0]!([])
- expect(Object.keys(store.items ?? {})).toEqual(['a', 'b'])
+ expect(store.items).toBeNull()
+ })
+
+ it('keeps primed items on an empty FIRST emission (sync not landed yet)', () => {
+ const store = useWalletsStore()
+ store.initWallets()
+
+ store.setWallets({ a: { currency: 'USD', name: 'A', order: 0, type: 'cash' } } as never)
+ h.watchCallbacks[0]!([])
+ expect(store.items?.a).toBeDefined()
})
it('flips isLoaded on the first emission and resets it on re-subscribe', () => {
diff --git a/app/app/components/wallets/useWalletsStore.ts b/app/app/components/wallets/useWalletsStore.ts
index 243ebfd6..e8bfab30 100644
--- a/app/app/components/wallets/useWalletsStore.ts
+++ b/app/app/components/wallets/useWalletsStore.ts
@@ -71,10 +71,14 @@ export const useWalletsStore = defineStore('wallets', () => {
return
watchController?.abort()
isLoaded.value = false // re-subscribe (e.g. different user) waits for a fresh emission
+ let isFirstEmit = true
watchController = watchTable<Row>('SELECT * FROM wallets', [], (rows) => {
+ const isFirst = isFirstEmit
+ isFirstEmit = false
isLoaded.value = true
- // TEMP: keep cached data instead of wiping it with an empty first SQLite emission.
- if (!rows.length && hasItems.value)
+ // An empty FIRST emission means local SQLite hasn't received the first sync yet
+ // (fresh device) - keep the primed cache. Later empty emissions are genuine wipes.
+ if (isFirst && !rows.length && hasItems.value)
return
setWallets(rowsToWallets(rows))
})