Open source personal finance application
perf(boot): prime stores from cache in the powersync plugin
Ilya Komichev3407d0c61bb6
diff --git a/app/app/components/app/useInitApp.ts b/app/app/components/app/useInitApp.tsindex 918a0453..b85a331e 100644--- a/app/app/components/app/useInitApp.ts+++ b/app/app/components/app/useInitApp.ts@@ -30,6 +30,9 @@ const firstSyncComplete = ref(false)const syncError = ref(false)// Shared cookie ref so every caller reads/writes the same reactive value (per-call useCookie would not).let hintRef: Ref<boolean> | null = null+// Memoized so the early plugin call and the later startLocalData call share one blob read.+// Reset on sign-out (clearLocalData) so the next user re-primes from their own snapshot.+let primePromise: Promise<void> | null = nullexport function useInitApp() {const userStore = useUserStore()@@ -108,20 +111,26 @@ export function useInitApp() {// Instant first paint: seed the stores from the last session's per-user snapshot while// PowerSync's db.init + first query scan are still running. The watches then reconcile to truth.- async function primeStoresFromCache() {- // Cold-start instrumentation: blob-cache read+prime time vs the SQLite watch path- // (`ps:watch:*` measures) - feeds the blob-cache architecture decision.- performance.mark('cache:prime:start')- const snap = await readStoreCache()- if (snap) {- trnsStore.primeFromCache((snap.trns as Trns) ?? null)- categoriesStore.primeFromCache((snap.categories as Categories) ?? null)- walletsStore.primeFromCache((snap.wallets as Wallets) ?? null)- userStore.primeFromCache((snap.user as UserSettingsCache) ?? null)- currenciesStore.primeFromCache((snap.rates as Rates) ?? null)- }- performance.mark('cache:prime:end')- performance.measure('cache:prime', 'cache:prime:start', 'cache:prime:end')+ // Memoized: the powersync plugin kicks this off at boot (parallel with db.init) so the data is+ // in the stores before the first render, and startLocalData's await reuses the same read.+ // primeFromCache only fills `items` (never `isLoaded`), so isHydrated/bootState stay watch-gated.+ function primeStoresFromCache(): Promise<void> {+ primePromise ??= (async () => {+ // Cold-start instrumentation: blob-cache read+prime time vs the SQLite watch path+ // (`ps:watch:*` measures) - feeds the blob-cache architecture decision.+ performance.mark('cache:prime:start')+ const snap = await readStoreCache()+ if (snap) {+ trnsStore.primeFromCache((snap.trns as Trns) ?? null)+ categoriesStore.primeFromCache((snap.categories as Categories) ?? null)+ walletsStore.primeFromCache((snap.wallets as Wallets) ?? null)+ userStore.primeFromCache((snap.user as UserSettingsCache) ?? null)+ currenciesStore.primeFromCache((snap.rates as Rates) ?? null)+ }+ performance.mark('cache:prime:end')+ performance.measure('cache:prime', 'cache:prime:start', 'cache:prime:end')+ })()+ return primePromise}// Demo mode keeps its own localforage-backed cache (no backend).@@ -245,6 +254,7 @@ export function useInitApp() {function clearLocalData() {blockPersist()+ primePromise = nullcategoriesStore.setCategories(null)trnsStore.setTrns(null)@@ -264,5 +274,6 @@ export function useInitApp() {isHydrated,isOnboarded,isOnboardedHint,+ primeStoresFromCache,}}diff --git a/app/app/plugins/powersync.client.ts b/app/app/plugins/powersync.client.tsindex 725f545a..13ac1317 100644--- a/app/app/plugins/powersync.client.ts+++ b/app/app/plugins/powersync.client.ts@@ -3,6 +3,7 @@ import { deleteRow, deleteTrnsReferencing } from '~~/services/powersync/mutationimport { setUploadErrorHandler } from '~~/services/powersync/uploadErrorHandler'import { planDivergence } from '~~/services/powersync/uploadReconcile'+import { useInitApp } from '~/components/app/useInitApp'import { useDemo } from '~/components/demo/useDemo'import { hasPersistedSession } from '~/composables/useAuthSession'import { showActionToast, showErrorToast } from '~/composables/useStoreSync'@@ -23,8 +24,12 @@ export default defineNuxtPlugin(() => {// Open local SQLite immediately for an already-logged-in user so the cold-start DB// init overlaps app boot instead of waiting for the async session to resolve.- if (!isDemo.value && hasPersistedSession())+ if (!isDemo.value && hasPersistedSession()) {getPowerSyncDb().then(db => db.init()).catch(e => logger.error('eager db init failed', e))+ // Seed the stores from the last session's snapshot now (parallel with db.init), before the+ // layout's useAsyncData('app') runs, so the dashboard paints with data on the first render.+ void useInitApp().primeStoresFromCache()+ }// Reconcile local rows after a write was fatally rejected by the server and discarded.setUploadErrorHandler((_error, divergedOps) => {