-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.ts
56 lines (44 loc) · 1.21 KB
/
cache.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
49
50
51
52
53
54
55
56
import { StationMeta, StationResponse, Train, TrainResponse } from "./types/amtraker";
import * as fs from "fs";
export default class cache {
trains: TrainResponse;
stations: StationResponse;
constructor() {
this.trains = {};
this.stations = {};
return;
}
getTrains() {
return this.trains;
}
getStation(code: string) {
return this.stations[code];
}
getStations() {
return this.stations;
}
setTrains(data: TrainResponse) {
console.log('setting trains')
//fs.writeFileSync('cache.json', JSON.stringify(data, null, 2));
Object.keys(data).forEach((key) => {
data[key].forEach((train) => {
train.stations.forEach((station) => {
const stationData = this.getStation(station.code);
//console.log(stationData)
if (stationData && !stationData.trains.includes(train.trainID)) {
stationData.trains.push(train.trainID);
}
this.setStation(station.code, stationData);
})
})
})
this.trains = data;
}
setStation(code: string, data: StationMeta) {
//console.log('setting', code)
this.stations[code] = data;
}
setStations(data: StationResponse) {
this.stations = data;
}
}