From da0e230c3345d641c91f503ef56cebb0343b8419 Mon Sep 17 00:00:00 2001 From: Landon Reed Date: Tue, 19 Feb 2019 11:32:33 -0500 Subject: [PATCH] refactor(gtfs+): simplify lookupMissingEntities function --- lib/gtfsplus/actions/gtfsplus.js | 50 +++++++++++++++----------------- lib/types/reducers.js | 5 +++- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/lib/gtfsplus/actions/gtfsplus.js b/lib/gtfsplus/actions/gtfsplus.js index cc56ff731..c65d2e490 100644 --- a/lib/gtfsplus/actions/gtfsplus.js +++ b/lib/gtfsplus/actions/gtfsplus.js @@ -213,7 +213,7 @@ export function loadGtfsEntities ( }) : rows const {namespace} = feedVersion - const {routesToLoad, stopsToLoad} = lookupMissingEntities(tableId, filteredRows, getState().gtfsplus.gtfsEntityLookup) + const {routesToLoad, stopsToLoad} = lookupMissingEntities(filteredRows, getState().gtfsplus.gtfsEntityLookup) if (routesToLoad.length === 0 && stopsToLoad.length === 0) { return } @@ -225,40 +225,38 @@ export function loadGtfsEntities ( } } -function lookupMissingEntities (tableId: string, rows: Array, currentLookup: Object) { - // lookup table for mapping tableId:fieldName keys to inputType values - const typeLookup = {} - const getDataType = function (tableId, fieldName) { - const lookupKey = tableId + ':' + fieldName - if (lookupKey in typeLookup) return typeLookup[lookupKey] - const specTable = getGtfsPlusSpec().find(t => t.id === tableId) - if (!specTable) { - console.warn(`unable to find spec table for tableId: ${tableId}`) - return null - } - const fieldInfo = specTable.fields.find(f => f.name === fieldName) - if (!fieldInfo) return null - typeLookup[lookupKey] = fieldInfo.inputType - return fieldInfo.inputType - } - - // Determine which routes, stops, etc. aren't currently in the +/** + * Given a set of GTFS+ rows, determine (from the current state's lookup) if the + * rows contain references to GTFS routes or stops that have not yet been fetched + * from the GraphQL GTFS API and return the list of unfetched/missing stops and + * routes. NOTE: only a single feed version is considered at once, so there is + * no need to scope the route/stop IDs by feed. + */ +function lookupMissingEntities ( + gtfsPlusRows: Array, + currentLookup: {[string]: GtfsRoute | GtfsStop} +) { + // Determine which routes and stops aren't currently in the // gtfsEntityLookup table and need to be loaded from the API. const routesToLoad: string[] = [] const stopsToLoad: string[] = [] - for (const rowData of rows) { + for (const rowData of gtfsPlusRows) { for (const fieldName in rowData) { - const dataType = getDataType(tableId, fieldName) - switch (dataType) { - case 'GTFS_ROUTE': - const routeId = rowData.route_id + // Currently, the only references to stops/routes in GTFS+ are fields named + // route_id or stop_id. If this changes, this logic should revert to + // commit 7d150d85e203b336147ba106eef7c3be90b39fb7 which contained a way + // to lookup based on inputType in gtfsplus.yml. + switch (fieldName) { + case 'route_id': + const {route_id: routeId} = rowData if (routeId && !(`route_${routeId}` in currentLookup)) routesToLoad.push(routeId) break - case 'GTFS_STOP': - const stopId = rowData.stop_id + case 'stop_id': + const {stop_id: stopId} = rowData if (stopId && !(`stop_${stopId}` in currentLookup)) stopsToLoad.push(stopId) break default: + // No entities to search for. break } } diff --git a/lib/types/reducers.js b/lib/types/reducers.js index 5d663b09e..195fb3623 100644 --- a/lib/types/reducers.js +++ b/lib/types/reducers.js @@ -9,6 +9,7 @@ import type { Alert, ControlPoint, FetchStatus, + GtfsRoute, GtfsStop, Organization, Project, @@ -443,7 +444,9 @@ export type GtfsPlusReducerState = { activeTableId: 'realtime_routes', currentPage: number, feedVersionId: null | string, - gtfsEntityLookup: Object, // TODO specify more exact type + gtfsEntityLookup: { + [string]: GtfsStop | GtfsRoute + }, pageCount: number, recordsPerPage: number, tableData: null | any, // TODO specify more exact type