diff --git a/src/state/cache.ts b/src/state/cache.ts index d75d27e..7c66ef7 100644 --- a/src/state/cache.ts +++ b/src/state/cache.ts @@ -121,52 +121,49 @@ export const cache = { lastFetchTime === null || currentTime - lastFetchTime > maxAgeMilliseconds ); - //TODO cache.deleteFeatures(tileKey); }, - // Function to fetch a URL only if it hasn't been fetched for a certain duration - fetch: function(url: string, tileKey: string): Promise { + updateLastFetch: function(url: string): Promise { return new Promise(async (resolve, reject) => { - if (!await cache.shouldFetch(url)) { - resolve(null); - return; - } - if (!cache.db) { cache.db = await openDatabase(); } - // Fetch the URL since it's not in the cache or has expired - try { - const response = await fetch(url); - const data = await response.json(); - - const newTransaction = cache.db.transaction(['urls'], 'readwrite'); - const newObjectStore = newTransaction.objectStore('urls'); - - // Update or add the URL in the cache with the current timestamp - const putRequest = newObjectStore.put({ - url: url, - lastFetchTime: new Date().getTime(), - // No need to keep the data (the individual features should be - // loaded into their own object store on first fetch) - //cachedData: data, - }); - - putRequest.onsuccess = () => { - console.log("Fetched: ", url); - resolve(data); - }; - - putRequest.onerror = (event: Event) => { - reject(`Error updating cache: ${(event.target as IDBEventTargetWithResult).error}`); - }; - } catch (error) { - reject(`Error fetching URL: ${error}`); - } + const newTransaction = cache.db.transaction(['urls'], 'readwrite'); + const newObjectStore = newTransaction.objectStore('urls'); + + // Update or add the URL in the cache with the current timestamp + const putRequest = newObjectStore.put({ + url: url, + lastFetchTime: new Date().getTime(), + }); + + putRequest.onsuccess = () => { + console.log("Fetched: ", url); + resolve(true); + }; + + putRequest.onerror = (event: Event) => { + reject(`Error updating cache: ${(event.target as IDBEventTargetWithResult).error}`); + }; }); }, + // Function to fetch a URL only if it hasn't been fetched for a certain duration + fetch: async function(url: string, tileKey: string): Promise { + if (!await cache.shouldFetch(url)) { + return null; + } + + // Delete any stale features + cache.deleteFeatures(tileKey); + + // Fetch the URL since it's not in the cache or has expired + const response = await fetch(url); + cache.updateLastFetch(url); + return await response.json(); + }, + // Function to add GeoJSON feature to the cache addFeature: function(feature: SoundscapeFeature, tile: string): Promise { return new Promise(async (resolve, reject) => {