Deduplicate nearby station requests
This commit is contained in:
parent
0afe31fba3
commit
77a13639c6
2 changed files with 46 additions and 34 deletions
|
@ -183,25 +183,31 @@ function rebuildSuggestions() {
|
||||||
if (nearbyStatus === 'notRequested') {
|
if (nearbyStatus === 'notRequested') {
|
||||||
suggestionsArea.appendChild(a('', 'Load nearby stations').event$('click', function (event) {
|
suggestionsArea.appendChild(a('', 'Load nearby stations').event$('click', function (event) {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
|
var latitude = 0
|
||||||
|
var longitude = 0
|
||||||
var watchId = navigator.geolocation.watchPosition(
|
var watchId = navigator.geolocation.watchPosition(
|
||||||
function (data) {
|
function (data) {
|
||||||
var geoUrl = new URL('https://v6.db.transport.rest/locations/nearby')
|
if (data.coords.latitude !== latitude || data.coords.longitude !== longitude) {
|
||||||
geoUrl.searchParams.append('latitude', data.coords.latitude.toString())
|
latitude = data.coords.latitude
|
||||||
geoUrl.searchParams.append('longitude', data.coords.longitude.toString())
|
longitude = data.coords.longitude
|
||||||
geoUrl.searchParams.append('results', '10')
|
var geoUrl = new URL('https://v6.db.transport.rest/locations/nearby')
|
||||||
fetch(geoUrl)
|
geoUrl.searchParams.append('latitude', data.coords.latitude.toString())
|
||||||
.then(function (response) {
|
geoUrl.searchParams.append('longitude', data.coords.longitude.toString())
|
||||||
return response.json()
|
geoUrl.searchParams.append('results', '10')
|
||||||
})
|
fetch(geoUrl)
|
||||||
.then(function (data) {
|
.then(function (response) {
|
||||||
nearbyStatus = 'gotData'
|
return response.json()
|
||||||
nearbyStations = data
|
})
|
||||||
rebuildSuggestions()
|
.then(function (data) {
|
||||||
})
|
nearbyStatus = 'gotData'
|
||||||
.catch(function () {
|
nearbyStations = data
|
||||||
nearbyStatus = 'unavailable'
|
rebuildSuggestions()
|
||||||
rebuildSuggestions()
|
})
|
||||||
})
|
.catch(function () {
|
||||||
|
nearbyStatus = 'unavailable'
|
||||||
|
rebuildSuggestions()
|
||||||
|
})
|
||||||
|
}
|
||||||
},
|
},
|
||||||
function (error) {
|
function (error) {
|
||||||
if (nearbyStations.length === 0) {
|
if (nearbyStations.length === 0) {
|
||||||
|
|
40
station.js
40
station.js
|
@ -145,25 +145,31 @@ function rebuildSuggestions() {
|
||||||
if (nearbyStatus === 'notRequested') {
|
if (nearbyStatus === 'notRequested') {
|
||||||
suggestionsArea.appendChild(a('', 'Load nearby stations').event$('click', function (event) {
|
suggestionsArea.appendChild(a('', 'Load nearby stations').event$('click', function (event) {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
|
var latitude = 0
|
||||||
|
var longitude = 0
|
||||||
var watchId = navigator.geolocation.watchPosition(
|
var watchId = navigator.geolocation.watchPosition(
|
||||||
function (data) {
|
function (data) {
|
||||||
var geoUrl = new URL('https://v6.db.transport.rest/locations/nearby')
|
if (data.coords.latitude !== latitude || data.coords.longitude !== longitude) {
|
||||||
geoUrl.searchParams.append('latitude', data.coords.latitude.toString())
|
latitude = data.coords.latitude
|
||||||
geoUrl.searchParams.append('longitude', data.coords.longitude.toString())
|
longitude = data.coords.longitude
|
||||||
geoUrl.searchParams.append('results', '10')
|
var geoUrl = new URL('https://v6.db.transport.rest/locations/nearby')
|
||||||
fetch(geoUrl)
|
geoUrl.searchParams.append('latitude', data.coords.latitude.toString())
|
||||||
.then(function (response) {
|
geoUrl.searchParams.append('longitude', data.coords.longitude.toString())
|
||||||
return response.json()
|
geoUrl.searchParams.append('results', '10')
|
||||||
})
|
fetch(geoUrl)
|
||||||
.then(function (data) {
|
.then(function (response) {
|
||||||
nearbyStatus = 'gotData'
|
return response.json()
|
||||||
nearbyStations = data
|
})
|
||||||
rebuildSuggestions()
|
.then(function (data) {
|
||||||
})
|
nearbyStatus = 'gotData'
|
||||||
.catch(function () {
|
nearbyStations = data
|
||||||
nearbyStatus = 'unavailable'
|
rebuildSuggestions()
|
||||||
rebuildSuggestions()
|
})
|
||||||
})
|
.catch(function () {
|
||||||
|
nearbyStatus = 'unavailable'
|
||||||
|
rebuildSuggestions()
|
||||||
|
})
|
||||||
|
}
|
||||||
},
|
},
|
||||||
function (error) {
|
function (error) {
|
||||||
if (nearbyStations.length === 0) {
|
if (nearbyStations.length === 0) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue