Coffee journaling on ATProto (alpha) alpha.arabica.social
coffee
17
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: reduce automatic recache polling

authored by

Patrick Dewey and committed by tangled.org 32166a27 4204d27e

+6 -59
+6 -59
static/js/data-cache.js
··· 2 2 * Client-side data cache for Arabica 3 3 * Caches beans, roasters, grinders, and brewers in localStorage 4 4 * to reduce PDS round-trips on page loads. 5 + * 6 + * Data is fetched on page load (if stale) and after mutations. 7 + * No background polling — callers invalidate explicitly after CRUD ops. 5 8 */ 6 9 7 10 const CACHE_KEY = "arabica_data_cache"; 8 11 const CACHE_VERSION = 1; 9 - const CACHE_TTL_MS = 30 * 1000; // 30 seconds (shorter for multi-device sync) 10 - const REFRESH_INTERVAL_MS = 30 * 1000; // 30 seconds 12 + const CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes 11 13 12 14 // Module state 13 - let refreshTimer = null; 14 15 let isRefreshing = false; 15 16 let listeners = []; 16 17 ··· 242 243 } 243 244 244 245 /** 245 - * Start periodic background refresh 246 - */ 247 - function startPeriodicRefresh() { 248 - if (refreshTimer) return; 249 - 250 - refreshTimer = setInterval(async () => { 251 - try { 252 - await refreshCache(); 253 - } catch (e) { 254 - console.warn("Periodic refresh failed:", e); 255 - } 256 - }, REFRESH_INTERVAL_MS); 257 - } 258 - 259 - /** 260 - * Stop periodic background refresh 261 - */ 262 - function stopPeriodicRefresh() { 263 - if (refreshTimer) { 264 - clearInterval(refreshTimer); 265 - refreshTimer = null; 266 - } 267 - } 268 - 269 - /** 270 - * Initialize the cache - call on page load 271 - * Preloads data if not cached, starts periodic refresh 246 + * Initialize the cache - call on page load for pages that need entity data. 247 + * Fetches once if cache is stale. No background polling. 272 248 */ 273 249 async function init() { 274 - // Start periodic refresh 275 - startPeriodicRefresh(); 276 - 277 - // Preload if cache is empty or expired 278 250 if (!isCacheValid()) { 279 251 try { 280 252 await refreshCache(); ··· 282 254 console.warn("Initial cache load failed:", e); 283 255 } 284 256 } 285 - 286 - // Refresh when user returns to tab/app (handles multi-device sync) 287 - document.addEventListener("visibilitychange", () => { 288 - if (document.visibilityState === "visible" && !isCacheValid()) { 289 - refreshCache().catch((e) => 290 - console.warn("Visibility refresh failed:", e), 291 - ); 292 - } 293 - }); 294 - 295 - // For iOS PWA: refresh on focus 296 - window.addEventListener("focus", () => { 297 - if (!isCacheValid()) { 298 - refreshCache().catch((e) => console.warn("Focus refresh failed:", e)); 299 - } 300 - }); 301 - 302 - // Refresh on page show (back button, bfcache restore) 303 - window.addEventListener("pageshow", (event) => { 304 - if (event.persisted && !isCacheValid()) { 305 - refreshCache().catch((e) => console.warn("Pageshow refresh failed:", e)); 306 - } 307 - }); 308 257 } 309 258 310 259 /** ··· 323 272 invalidateAndRefresh, 324 273 addListener, 325 274 removeListener, 326 - startPeriodicRefresh, 327 - stopPeriodicRefresh, 328 275 init, 329 276 preload, 330 277 isCacheValid,