Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions src/hooks.server.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,72 @@
import oba from '$lib/obaSdk.js';

let routesCache = null;
let cacheTimestamp = null;
const CACHE_EXPIRATION_MS = 5 * 60 * 1000; // Cache expires every 5 minutes

/**
* Fetches routes data from the OneBusAway (OBA) SDK.
* Retrieves all agencies and their corresponding routes.
* Caches the results to optimize performance.
*/
async function fetchRoutesData() {
try {
const agenciesResponse = await oba.agenciesWithCoverage.list();
const agencies = agenciesResponse.data.list;

// Fetch routes for each agency concurrently
const routesPromises = agencies.map(async (agency) => {
const routesResponse = await oba.routesForAgency.list(agency.agencyId);
const routes = routesResponse.data.list;
const references = routesResponse.data.references;

// Create a map of agency references for quick lookups
const agencyReferenceMap = new Map(references.agencies.map((agency) => [agency.id, agency]));

// Attach agency info to each route
routes.forEach((route) => {
route.agencyInfo = agencyReferenceMap.get(route.agencyId);
});

return routes;
});

const routes = await Promise.all(routesPromises);
return routes.flat();
// Wait for all routes to be fetched and flatten the results
const routes = (await Promise.all(routesPromises)).flat();

// Update cache and timestamp
routesCache = routes;
cacheTimestamp = Date.now();

return routes;
} catch (error) {
console.error('Error fetching routes:', error);
return null;
}
}

/**
* Ensures the routes data is loaded and cached.
* Refreshes the cache if it is expired.
*/
async function preloadRoutesData() {
if (!routesCache) {
routesCache = await fetchRoutesData();
const isCacheExpired = !cacheTimestamp || (Date.now() - cacheTimestamp > CACHE_EXPIRATION_MS);
if (!routesCache || isCacheExpired) {
await fetchRoutesData();
}
}

preloadRoutesData();

/**
* Middleware hook that ensures routes data is available before handling the request.
*/
export async function handle({ event, resolve }) {
await preloadRoutesData();
return resolve(event);
}

/**
* Returns the cached routes data.
*/
export function getRoutesCache() {
return routesCache;
}