-
Notifications
You must be signed in to change notification settings - Fork 10
/
ReachableStopsFinderBirdsEyeCached.ts
48 lines (40 loc) · 1.79 KB
/
ReachableStopsFinderBirdsEyeCached.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { inject, injectable } from "inversify";
import ReachableStopsFinderMode from "../../enums/ReachableStopsFinderMode";
import IStop from "../../fetcher/stops/IStop";
import IStopsProvider from "../../fetcher/stops/IStopsProvider";
import { DurationMs, SpeedKmH } from "../../interfaces/units";
import TYPES from "../../types";
import IReachableStopsFinder, { IReachableStop } from "./IReachableStopsFinder";
import ReachableStopsFinderBirdsEye from "./ReachableStopsFinderBirdsEye";
/**
* This [[IReachableStopsFinder]] forms a caching layer in front of a [[ReachableStopsFinderBirdsEye]] instance,
* so all the distances don't have to be calculated repeatedly
*/
@injectable()
export default class ReachableStopsFinderBirdsEyeCached implements IReachableStopsFinder {
private readonly reachableStopsFinder: ReachableStopsFinderBirdsEye;
private readonly reachableStopsCache: {[cacheKey: string]: IReachableStop[]};
constructor(
@inject(TYPES.StopsProvider) stopsProvider: IStopsProvider,
) {
this.reachableStopsFinder = new ReachableStopsFinderBirdsEye(stopsProvider);
this.reachableStopsCache = {};
}
public async findReachableStops(
sourceOrTargetStop: IStop,
mode: ReachableStopsFinderMode,
maximumDuration: DurationMs,
minimumSpeed: SpeedKmH,
): Promise<IReachableStop[]> {
// Mode can be ignored since birds eye view distance is identical
const cacheKey = `${sourceOrTargetStop.id} ${maximumDuration} ${minimumSpeed}`;
const cacheItem = this.reachableStopsCache[cacheKey];
if (cacheItem) {
return cacheItem;
}
const reachableStops = await this.reachableStopsFinder
.findReachableStops(sourceOrTargetStop, mode, maximumDuration, minimumSpeed);
this.reachableStopsCache[cacheKey] = reachableStops;
return reachableStops;
}
}