From e3947fb09a95feda36543d0dd3666f73a23402d8 Mon Sep 17 00:00:00 2001 From: Guillaume Beraudo Date: Fri, 5 Dec 2014 16:38:00 +0100 Subject: [PATCH] Add optional fast parameter for clearing vector source --- src/ol/source/vectorsource.js | 30 +++++++++++++++++------- test/spec/ol/source/vectorsource.test.js | 24 ++++++++++++++++--- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/src/ol/source/vectorsource.js b/src/ol/source/vectorsource.js index 7551565165..de1783e130 100644 --- a/src/ol/source/vectorsource.js +++ b/src/ol/source/vectorsource.js @@ -37,14 +37,15 @@ ol.source.VectorEventType = { CHANGEFEATURE: 'changefeature', /** - * Triggered when a clear is called on the source. + * Triggered when the clear method is called on the source. * @event ol.source.VectorEvent#clear * @api */ CLEAR: 'clear', /** - * Triggered when a single feature is removed from the source. + * Triggered when a feature is removed from the source. + * See {@link ol.source.Vector#clear source.clear()} for exceptions. * @event ol.source.VectorEvent#removefeature * @api stable */ @@ -234,16 +235,27 @@ ol.source.Vector.prototype.addFeaturesInternal = function(features) { /** * Remove all features from the source. + * @param {boolean=} opt_fast Skip dispatching of {@link removefeature} events. * @api stable */ -ol.source.Vector.prototype.clear = function() { - for (var featureId in this.featureChangeKeys_) { - var keys = this.featureChangeKeys_[featureId]; - goog.array.forEach(keys, goog.events.unlistenByKey); +ol.source.Vector.prototype.clear = function(opt_fast) { + if (opt_fast) { + for (var featureId in this.featureChangeKeys_) { + var keys = this.featureChangeKeys_[featureId]; + goog.array.forEach(keys, goog.events.unlistenByKey); + } + this.featureChangeKeys_ = {}; + this.idIndex_ = {}; + this.undefIdIndex_ = {}; + } else { + var rmFeatureInternal = this.removeFeatureInternal; + this.rBush_.forEach(rmFeatureInternal, this); + goog.object.forEach(this.nullGeometryFeatures_, rmFeatureInternal, this); + goog.asserts.assert(goog.object.isEmpty(this.featureChangeKeys_)); + goog.asserts.assert(goog.object.isEmpty(this.idIndex_)); + goog.asserts.assert(goog.object.isEmpty(this.undefIdIndex_)); } - this.featureChangeKeys_ = {}; - this.idIndex_ = {}; - this.undefIdIndex_ = {}; + this.rBush_.clear(); this.nullGeometryFeatures_ = {}; diff --git a/test/spec/ol/source/vectorsource.test.js b/test/spec/ol/source/vectorsource.test.js index e632efae11..558cd725e8 100644 --- a/test/spec/ol/source/vectorsource.test.js +++ b/test/spec/ol/source/vectorsource.test.js @@ -85,7 +85,25 @@ describe('ol.source.Vector', function() { describe('#clear', function() { - it('removes all features', function() { + it('removes all features using fast path', function() { + var changeSpy = sinon.spy(); + goog.events.listen(vectorSource, 'change', changeSpy); + var removeFeatureSpy = sinon.spy(); + goog.events.listen(vectorSource, 'removefeature', removeFeatureSpy); + var clearSourceSpy = sinon.spy(); + goog.events.listen(vectorSource, 'clear', clearSourceSpy); + vectorSource.clear(true); + expect(vectorSource.getFeatures()).to.eql([]); + expect(vectorSource.isEmpty()).to.be(true); + expect(changeSpy).to.be.called(); + expect(changeSpy.callCount).to.be(1); + expect(removeFeatureSpy).not.to.be.called(); + expect(removeFeatureSpy.callCount).to.be(0); + expect(clearSourceSpy).to.be.called(); + expect(clearSourceSpy.callCount).to.be(1); + }); + + it('removes all features using slow path', function() { var changeSpy = sinon.spy(); goog.events.listen(vectorSource, 'change', changeSpy); var removeFeatureSpy = sinon.spy(); @@ -97,8 +115,8 @@ describe('ol.source.Vector', function() { expect(vectorSource.isEmpty()).to.be(true); expect(changeSpy).to.be.called(); expect(changeSpy.callCount).to.be(1); - expect(removeFeatureSpy).not.to.be.called(); - expect(removeFeatureSpy.callCount).to.be(0); + expect(removeFeatureSpy).to.be.called(); + expect(removeFeatureSpy.callCount).to.be(features.length); expect(clearSourceSpy).to.be.called(); expect(clearSourceSpy.callCount).to.be(1); });