From a2b81d6bd0a4d0b2a9004baaa8a2854115abf99b Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Tue, 20 May 2014 10:05:47 -0600 Subject: [PATCH] Disallow adding the same feature twice --- src/ol/source/vectorsource.js | 9 ++++++--- test/spec/ol/source/vectorsource.test.js | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/ol/source/vectorsource.js b/src/ol/source/vectorsource.js index 50a7eca76b..03b4ffb3b6 100644 --- a/src/ol/source/vectorsource.js +++ b/src/ol/source/vectorsource.js @@ -128,15 +128,18 @@ ol.source.Vector.prototype.addFeatureInternal = function(feature) { var extent = geometry.getExtent(); this.rBush_.insert(extent, feature); } else { - this.nullGeometryFeatures_[goog.getUid(feature).toString()] = feature; + this.nullGeometryFeatures_[featureKey] = feature; } var id = feature.getId(); if (goog.isDef(id)) { var sid = id.toString(); - goog.asserts.assert(!(goog.isDef(this.idIndex_[sid]))); + goog.asserts.assert(!(sid in this.idIndex_), + 'Feature with same id already added to the source: ' + id); this.idIndex_[sid] = feature; } else { - this.undefIdIndex_[goog.getUid(feature).toString()] = feature; + goog.asserts.assert(!(featureKey in this.undefIdIndex_), + 'Feature already added to the source'); + this.undefIdIndex_[featureKey] = feature; } this.dispatchEvent( new ol.source.VectorEvent(ol.source.VectorEventType.ADDFEATURE, feature)); diff --git a/test/spec/ol/source/vectorsource.test.js b/test/spec/ol/source/vectorsource.test.js index 3777c1fd83..8957fd2305 100644 --- a/test/spec/ol/source/vectorsource.test.js +++ b/test/spec/ol/source/vectorsource.test.js @@ -363,6 +363,21 @@ describe('ol.source.Vector', function() { }); + describe('the undefined feature id index', function() { + var source; + beforeEach(function() { + source = new ol.source.Vector(); + }); + + it('disallows adding the same feature twice', function() { + var feature = new ol.Feature(); + source.addFeature(feature); + expect(function() { + source.addFeature(feature); + }).to.throwException(); + }); + }); + });