Skip to content

Commit

Permalink
add opt.shouldQueryDeparturesAt(stopId, hops) 📝; 5.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Jan 24, 2024
1 parent 1790804 commit d173313
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
5 changes: 4 additions & 1 deletion examples/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {createWalkAndDiscoverStations as createWalk} from '../index.js'
const hafas = createHafas(dbProfile, 'hafas-discover-stations example')
const walk = createWalk(hafas)
const berlinFriedrichstr = '8011306'
const stations = walk(berlinFriedrichstr, {concurrency: 8})
const stations = walk(berlinFriedrichstr, {
concurrency: 8,
shouldQueryDeparturesAt: (stopId, hops) => hops <= 1,
})

const DEV = process.env.NODE_ENV === 'dev'
stations.on('hafas-error', (err) => {
Expand Down
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const createWalkAndDiscoverStations = (hafas) => {
parseStationId: id => id,
subStops: true,
linesOfStops: false,
shouldQueryDeparturesAt: (stopId, hops) => true,
...opt,
}
if (!('when' in opt) || opt.when === null) {
Expand Down Expand Up @@ -91,8 +92,11 @@ const createWalkAndDiscoverStations = (hafas) => {
visitedStopsAndStations[sId] = true
nrOfStopsAndStations++
out.push(station)
queue.push(queryDepartures(sId, hops + 1))
stopsAndStationsPerSecond = stopsAndStationsSpeed(1)

if (opt.shouldQueryDeparturesAt(sId, hops + 1)) {
queue.push(queryDepartures(sId, hops + 1))
}
}

const onStop = (stop, hops) => {
Expand All @@ -104,8 +108,11 @@ const createWalkAndDiscoverStations = (hafas) => {

nrOfStopsAndStations++
out.push(stop)
queue.push(queryDepartures(sId, hops + 1))
stopsAndStationsPerSecond = stopsAndStationsSpeed(1)

if (opt.shouldQueryDeparturesAt(sId, hops + 1)) {
queue.push(queryDepartures(sId, hops + 1))
}
}

const onStopsAndStations = (stopsAndStations, hops) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hafas-discover-stations",
"description": "Pass in a HAFAS client, discover stations by querying departures.",
"version": "4.2.2",
"version": "5.0.0",
"type": "module",
"main": "index.js",
"files": [
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ walk(stationId, [opt])
- `timeout`: timeout for a single job in milliseconds – default: `10000`
- `parseStationId`: an optional function to process station IDs – default: `id => id`
- `stationLines`: Query lines of stops/stations? – default: `false`
- `shouldQueryDeparturesAt(stopId, nrOfHops)`: Should departures be queries from this `stopId`, effectively continuing the graph search? – default: `() => true`


## Contributing
Expand Down

0 comments on commit d173313

Please sign in to comment.