Change routing to always use OSM IDs except for custom places

Also implements a short term cache for OSM place data, so we can load it
multiple times without multiplying network requests where needed
This commit is contained in:
2026-04-22 11:01:09 +04:00
parent 670128cbda
commit 4fed8c05c5
2 changed files with 56 additions and 9 deletions

View File

@@ -9,25 +9,47 @@ export default class PlaceRoute extends Route {
async model(params) { async model(params) {
const id = params.place_id; const id = params.place_id;
let type, osmId;
let isExplicitOsm = false;
if ( if (
id.startsWith('osm:node:') || id.startsWith('osm:node:') ||
id.startsWith('osm:way:') || id.startsWith('osm:way:') ||
id.startsWith('osm:relation:') id.startsWith('osm:relation:')
) { ) {
const [, type, osmId] = id.split(':'); isExplicitOsm = true;
[, type, osmId] = id.split(':');
console.debug(`Fetching explicit OSM ${type}:`, osmId); console.debug(`Fetching explicit OSM ${type}:`, osmId);
return this.loadOsmPlace(osmId, type); }
let backgroundFetchPromise = null;
if (isExplicitOsm) {
backgroundFetchPromise = this.loadOsmPlace(osmId, type);
} }
await this.waitForSync(); await this.waitForSync();
let bookmark = this.storage.findPlaceById(id); let lookupId = isExplicitOsm ? osmId : id;
let bookmark = this.storage.findPlaceById(lookupId);
// Ensure type matches if we are looking up by osmId
if (bookmark && isExplicitOsm && bookmark.osmType !== type) {
bookmark = null; // Type mismatch, not the same OSM object
}
if (bookmark) { if (bookmark) {
console.debug('Found in bookmarks:', bookmark.title); console.debug('Found in bookmarks:', bookmark.title);
return bookmark; return bookmark;
} }
if (isExplicitOsm) {
console.debug(
`Not in bookmarks, using explicitly fetched OSM ${type}:`,
osmId
);
return await backgroundFetchPromise;
}
console.warn('Not in bookmarks:', id); console.warn('Not in bookmarks:', id);
return null; return null;
} }
@@ -119,14 +141,14 @@ export default class PlaceRoute extends Route {
} }
serialize(model) { serialize(model) {
// If the model is a saved bookmark, use its ID // If it's an OSM POI, use the explicit format first
if (model.id) {
return { place_id: model.id };
}
// If it's an OSM POI, use the explicit format
if (model.osmId && model.osmType) { if (model.osmId && model.osmType) {
return { place_id: `osm:${model.osmType}:${model.osmId}` }; return { place_id: `osm:${model.osmType}:${model.osmId}` };
} }
// If the model is a saved bookmark (and not OSM, e.g. custom place), use its ID
if (model.id) {
return { place_id: model.id };
}
// Fallback // Fallback
return { place_id: model.osmId }; return { place_id: model.osmId };
} }

View File

@@ -8,6 +8,7 @@ export default class OsmService extends Service {
controller = null; controller = null;
cachedResults = null; cachedResults = null;
lastQueryKey = null; lastQueryKey = null;
cachedPlaces = new Map();
cancelAll() { cancelAll() {
if (this.controller) { if (this.controller) {
@@ -232,6 +233,13 @@ out center;
async fetchOsmObject(osmId, osmType) { async fetchOsmObject(osmId, osmType) {
if (!osmId || !osmType) return null; if (!osmId || !osmType) return null;
const cacheKey = `${osmType}:${osmId}`;
const cached = this.cachedPlaces.get(cacheKey);
if (cached && Date.now() - cached.timestamp < 10000) {
console.debug(`Using in-memory cached OSM object for ${cacheKey}`);
return cached.data;
}
let url; let url;
if (osmType === 'node') { if (osmType === 'node') {
url = `https://www.openstreetmap.org/api/0.6/node/${osmId}.json`; url = `https://www.openstreetmap.org/api/0.6/node/${osmId}.json`;
@@ -253,8 +261,25 @@ out center;
} }
throw new Error(`OSM API request failed: ${res.status}`); throw new Error(`OSM API request failed: ${res.status}`);
} }
const data = await res.json(); const data = await res.json();
return this.normalizeOsmApiData(data.elements, osmId, osmType); const normalizedData = this.normalizeOsmApiData(
data.elements,
osmId,
osmType
);
this.cachedPlaces.set(cacheKey, {
data: normalizedData,
timestamp: Date.now(),
});
// Cleanup cache entry automatically after 10 seconds
setTimeout(() => {
this.cachedPlaces.delete(cacheKey);
}, 10000);
return normalizedData;
} catch (e) { } catch (e) {
console.error('Failed to fetch OSM object:', e); console.error('Failed to fetch OSM object:', e);
return null; return null;