Commit dc1d8ce0 authored by Christian Paul's avatar Christian Paul 🦊

Implement a bundler middleware for identical API requests

parent 115d335b
Pipeline #63186791 failed with stage
in 1 minute and 5 seconds
'use strict';
function createBundler(func) {
const pendingPromises = {
node: {},
way: {},
relation: {},
};
return (type, id) => {
if (pendingPromises[type][id]) {
return pendingPromises[type][id];
}
const promise = func(type, id);
pendingPromises[type][id] = promise;
return promise.finally(() => {
pendingPromises[type][id] = undefined;
});
}
}
module.exports = createBundler;
......@@ -3,8 +3,9 @@ const express = require('express');
const router = express.Router();
const geoTz = require('geo-tz');
const askNominatimById = require('../apis/nominatim.js').getElementById;
const askOverpassById = require('../apis/overpass.js').getElementById;
const bundlerMiddleware = require('./bundler-middleware.js');
const askNominatimById = bundlerMiddleware(require('../apis/nominatim.js').getElementById);
const askOverpassById = bundlerMiddleware(require('../apis/overpass.js').getElementById);
const { loadFromFile, getFromCache, saveToCache } = require('./cache.js');
loadFromFile();
......@@ -20,7 +21,7 @@ async function getOsmElement(type, id) {
id: nominatimObject.osm_id,
name: osmTags.name,
opening_hours: osmTags.opening_hours,
timezone: geoTz(osmObject.lat, osmObject.lon)[0],
timezone: geoTz(nominatimObject.lat, nominatimObject.lon)[0],
nominatimObject,
osmObject,
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment