-
Notifications
You must be signed in to change notification settings - Fork 0
/
from-station-sync.js
84 lines (76 loc) · 2.46 KB
/
from-station-sync.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
var GeoUtil = require('./util/geo-util.js')
var OVERRIDE_MAX_DISTANCE = 1000
var LOCK_RELEASE_MARGIN = 100
var FROM_LOCKS = {
none: 0,
nearby: 1,
manual: 2
}
function FromStationSync (nearbyStations, overrides, tripsSearch) {
this.overrides = overrides
this.nearbyStations = nearbyStations
this.tripsSearch = tripsSearch
this.fromLock = FROM_LOCKS.none
this.fromlockDistance = 0
this.fromChanged = this.fromChanged.bind(this)
this.nearbyChanged = this.nearbyChanged.bind(this)
}
module.exports = FromStationSync
FromStationSync.prototype.start = function () {
if (this.nearbyStations) {
this.nearbyStations.bind('change:stations', this.nearbyChanged)
}
if (this.tripsSearch) {
this.tripsSearch.bind('change:from', this.fromChanged)
}
}
FromStationSync.prototype.stop = function () {
if (this.nearbyStations) {
this.nearbyStations.unbind('change:stations', this.nearbyChanged)
}
if (this.tripsSearch) {
this.tripsSearch.unbind('change:from', this.fromChanged)
}
}
FromStationSync.prototype.nearbyChanged = function (stations) {
if (stations && stations.length > 0 && this.shouldChangeNearby()) {
this.tripsSearch.from = (this.overrides)
? this.overrides.get(stations[0])
: stations[0]
}
}
FromStationSync.prototype.shouldChangeNearby = function () {
if (!this.tripsSearch.from ||
this.fromLock === FROM_LOCKS.none) { return true }
if (this.fromLock === FROM_LOCKS.manual) { return false }
return (
GeoUtil.distance(
this.nearbyStations.lastSearchLocation,
this.tripsSearch.from.location
) > this.fromlockDistance + LOCK_RELEASE_MARGIN
)
}
FromStationSync.prototype.fromChanged = function (station) {
var nearbyStations = this.nearbyStations.stations
if (!(station && nearbyStations && nearbyStations[0])) { return }
var closestStation = nearbyStations[0]
if (!closestStation.equals(station)) {
var location = this.nearbyStations.lastSearchLocation
var distance = GeoUtil.distance(location, station.location)
var isNearby = this.nearbyStations.isAmongNearby(station)
if (isNearby || distance <= OVERRIDE_MAX_DISTANCE) {
this.fromLock = FROM_LOCKS.nearby
this.fromlockDistance = distance
if (this.overrides) {
this.overrides.set(closestStation, station)
}
} else {
this.fromLock = FROM_LOCKS.manual
}
} else {
this.fromLock = FROM_LOCKS.none
if (this.overrides) {
this.overrides.set(closestStation, null)
}
}
}