-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (104 loc) · 2.85 KB
/
index.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict'
const observeHafasClient = (hafas, emitter, watch) => {
const onJourney = (journey) => {
emitter.emit('journey', journey)
for (const leg of journey.legs) {
emitter.emit('leg', leg)
if (!Array.isArray(leg.stopovers)) continue
for (const stopover of leg.stopovers) {
emitter.emit('stopover', {
...stopover,
tripId: leg.id,
line: leg.line
})
}
}
}
const wrapper = Object.create(hafas)
if (watch.departures) {
const observedDepartures = (station, opt = {}) => {
return hafas.departures(station, opt)
.then((departures) => {
for (const dep of departures) emitter.emit('departure', dep)
return departures
})
}
wrapper.departures = observedDepartures
}
if (watch.arrivals) {
const observedArrivals = (station, opt = {}) => {
return hafas.arrivals(station, opt)
.then((arrivals) => {
for (const arr of arrivals) emitter.emit('arrival', arr)
return arrivals
})
}
wrapper.arrivals = observedArrivals
}
const spyOnJourneys = watch.journeys || watch.legs || watch.stopovers
if (spyOnJourneys && hafas.journeys) {
const observedJourneys = (from, to, opt = {}) => {
return hafas.journeys(from, to, opt)
.then((journeys) => {
for (const journey of journeys) onJourney(journey)
return journeys
})
}
wrapper.journeys = observedJourneys
}
const spyOnRefreshJourney = watch.journeys || watch.legs || watch.stopovers
if (spyOnRefreshJourney && hafas.refreshJourney) {
const observedRefreshJourney = (refreshToken, opt = {}) => {
return hafas.refreshJourney(refreshToken, opt)
.then((journey) => {
onJourney(journey)
return journey
})
}
wrapper.refreshJourney = observedRefreshJourney
}
const spyOnTrip = watch.trips || watch.stopovers
if (spyOnTrip && hafas.trip) {
const observedTrip = (id, lineName, opt = {}) => {
return hafas.trip(id, lineName, opt)
.then((trip) => {
emitter.emit('trip', trip)
if (Array.isArray(trip.stopovers)) {
for (const stopover of trip.stopovers) {
emitter.emit('stopover', {
...stopover,
tripId: trip.id,
line: trip.line
})
}
}
return trip
})
}
wrapper.trip = observedTrip
}
const spyOnRadar = watch.movements || watch.stopovers
if (spyOnRadar && hafas.radar) {
const observedRadar = (bbox, opt = {}) => {
return hafas.radar(bbox, opt)
.then((movements) => {
for (const movement of movements) {
emitter.emit('movement', movement)
for (const stopover of movement.nextStops) {
emitter.emit('stopover', {
...stopover,
line: movement.line,
tripId: movement.trip
})
}
}
return movements
})
}
wrapper.radar = observedRadar
}
// todo: watch.stops, watch.pois, watch.stations, watch.locations
// todo: locations(), station(), reachableFrom()
return wrapper
}
module.exports = observeHafasClient