diff --git a/modules/services/osm.js b/modules/services/osm.js index 77976531e..edcaa78db 100644 --- a/modules/services/osm.js +++ b/modules/services/osm.js @@ -148,7 +148,8 @@ var parsers = { }; -function parse(xml, callback) { +function parse(xml, callback, options) { + options = _.extend({ cache: true }, options); if (!xml || !xml.childNodes) return; var root = xml.childNodes[0], @@ -158,12 +159,13 @@ function parse(xml, callback) { var parser = parsers[child.nodeName]; if (parser) { var uid = osmEntity.id.fromOSM(child.nodeName, child.attributes.id.value); - if (entityCache[uid]) { + if (options.cache && entityCache[uid]) { return null; } return parser(child, uid); } } + utilIdleWorker(children, parseChild, callback); } @@ -216,7 +218,8 @@ export default { }, - loadFromAPI: function(path, callback) { + loadFromAPI: function(path, callback, options) { + options = _.extend({ cache: true }, options); var that = this; function done(err, xml) { @@ -242,11 +245,13 @@ export default { if (callback) { if (err) return callback(err, null); parse(xml, function (entities) { - for (var i in entities) { - entityCache[entities[i].id] = true; + if (options.cache) { + for (var i in entities) { + entityCache[entities[i].id] = true; + } } callback(null, entities); - }); + }, options); } } } @@ -275,13 +280,15 @@ export default { loadEntityVersion: function(id, version, callback) { var type = osmEntity.id.type(id), - osmID = osmEntity.id.toOSM(id); + osmID = osmEntity.id.toOSM(id), + options = { cache: false }; this.loadFromAPI( '/api/0.6/' + type + '/' + osmID + '/' + version, function(err, entities) { if (callback) callback(err, { data: entities }); - } + }, + options ); }, diff --git a/modules/util/idle_worker.js b/modules/util/idle_worker.js index f1ff47451..2a5cc5cfb 100644 --- a/modules/util/idle_worker.js +++ b/modules/util/idle_worker.js @@ -6,7 +6,7 @@ export function utilIdleWorker(tasks, processor, callback) { function worker(deadline) { while (deadline.timeRemaining() > 0 && currentPos < totalTasks) { var result = processor(tasks[currentPos]); - + // if falsy dont add to the processed list if (result) processed.push(result); currentPos++; @@ -15,7 +15,7 @@ export function utilIdleWorker(tasks, processor, callback) { // more tasks are left, we might need more idleCallbacks if (currentPos < totalTasks) { return window.requestIdleCallback(function(deadline) {worker(deadline);}); - } + } // tasks are completed return callback(processed); diff --git a/test/spec/services/osm.js b/test/spec/services/osm.js index f23c1d635..0ba3f5387 100644 --- a/test/spec/services/osm.js +++ b/test/spec/services/osm.js @@ -320,6 +320,23 @@ describe('iD.serviceOsm', function () { [200, { 'Content-Type': 'text/xml' }, wayXML]); server.respond(); }); + + it('ignores repeat requests using entityCache', function(done) { + var id = 'n1'; + connection.loadEntity(id, function(err, result) { + var entity = _.find(result.data, function(e) { return e.id === id; }); + expect(entity).to.be.an.instanceOf(iD.Node); + connection.loadEntity(id, function(err, result) { + expect(result.data).to.eql([]); + done(); + }); + server.respond(); + }); + + server.respondWith('GET', 'http://www.openstreetmap.org/api/0.6/node/1', + [200, { 'Content-Type': 'text/xml' }, nodeXML]); + server.respond(); + }); }); describe('#loadEntityVersion', function () { @@ -363,6 +380,24 @@ describe('iD.serviceOsm', function () { [200, { 'Content-Type': 'text/xml' }, wayXML]); server.respond(); }); + + it('does not ignore repeat requests', function(done) { + var id = 'n1'; + connection.loadEntityVersion(id, 1, function(err1, result1) { + var entity1 = _.find(result1.data, function(e1) { return e1.id === id; }); + expect(entity1).to.be.an.instanceOf(iD.Node); + connection.loadEntityVersion(id, 1, function(err2, result2) { + var entity2 = _.find(result2.data, function(e2) { return e2.id === id; }); + expect(entity2).to.be.an.instanceOf(iD.Node); + done(); + }); + server.respond(); + }); + + server.respondWith('GET', 'http://www.openstreetmap.org/api/0.6/node/1/1', + [200, { 'Content-Type': 'text/xml' }, nodeXML]); + server.respond(); + }); }); describe('#loadMultiple', function () {