mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-12 23:31:02 +00:00
aa24a9eff7
With "Show whole trip" on, every leg of every day went to the router one at a time, a second apart, and the route cache lived in memory only, so each visit started over: a trip of a month took 30 to 45 seconds before its route was drawn, on every open. Routes the router answered are now also kept on the device (Dexie v14, routeCache by RouteCalculator's cache key, a month at most, the oldest give way past 3000). A leg found there needs no request and no pause, so a trip whose legs did not change opens with its roads at once; only new or changed legs are routed. Plugin routes stay in memory only, since they depend on trip and day. The cache moves out of RouteCalculator into routeStore.ts. Closes #2794
42 lines
1.6 KiB
TypeScript
42 lines
1.6 KiB
TypeScript
import { routeCacheRepo } from '../../repo/routeCacheRepo';
|
|
import type { RouteWithLegs } from '../../types';
|
|
|
|
/**
|
|
* Route answers by RouteCalculator's cache key (profile, unit, waypoints,
|
|
* avoided roads). Routes are stable, so this keeps the router from being asked
|
|
* again on every day switch or reorder: in memory for this visit, and on the
|
|
* device for the next one, so a trip that shows its whole route opens drawn
|
|
* instead of routing every leg again, one a second (#2794).
|
|
*/
|
|
const inMemory = new Map<string, RouteWithLegs>();
|
|
const MEMORY_MAX = 200;
|
|
|
|
/** Keeps a route for this visit only, dropping the oldest past the limit. */
|
|
export function rememberRoute(key: string, route: RouteWithLegs): void {
|
|
inMemory.set(key, route);
|
|
if (inMemory.size > MEMORY_MAX) {
|
|
const oldest = inMemory.keys().next().value;
|
|
if (oldest !== undefined) inMemory.delete(oldest);
|
|
}
|
|
}
|
|
|
|
/** The route this visit already holds, without asking the device. */
|
|
export function routeInMemory(key: string): RouteWithLegs | undefined {
|
|
return inMemory.get(key);
|
|
}
|
|
|
|
/** Keeps a route the router just answered, in memory and on the device. */
|
|
export function cacheRoute(key: string, route: RouteWithLegs): void {
|
|
rememberRoute(key, route);
|
|
void routeCacheRepo.put(key, route);
|
|
}
|
|
|
|
/** The route kept in memory, else the one kept on the device, which then stays in memory too. */
|
|
export async function storedRoute(key: string): Promise<RouteWithLegs | null> {
|
|
const held = inMemory.get(key);
|
|
if (held) return held;
|
|
const kept = await routeCacheRepo.get<RouteWithLegs>(key);
|
|
if (kept) rememberRoute(key, kept);
|
|
return kept;
|
|
}
|