From 17e56d83573740be6fc78a251b8cff04452f78b7 Mon Sep 17 00:00:00 2001 From: Guillaume Beraudo Date: Fri, 5 Dec 2014 14:55:46 +0100 Subject: [PATCH] Introduce clear event on vector source Three seconds speed up when clearing 100'000 features. Clearing is now around 350ms. --- src/ol/source/vectorsource.js | 25 ++++++++++++++++++------ test/spec/ol/source/vectorsource.test.js | 8 ++++++-- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/ol/source/vectorsource.js b/src/ol/source/vectorsource.js index 6b223026a0..7551565165 100644 --- a/src/ol/source/vectorsource.js +++ b/src/ol/source/vectorsource.js @@ -37,7 +37,14 @@ ol.source.VectorEventType = { CHANGEFEATURE: 'changefeature', /** - * Triggered when a feature is removed from the source. + * Triggered when a clear is called on the source. + * @event ol.source.VectorEvent#clear + * @api + */ + CLEAR: 'clear', + + /** + * Triggered when a single feature is removed from the source. * @event ol.source.VectorEvent#removefeature * @api stable */ @@ -230,12 +237,18 @@ ol.source.Vector.prototype.addFeaturesInternal = function(features) { * @api stable */ ol.source.Vector.prototype.clear = function() { - this.rBush_.forEach(this.removeFeatureInternal, this); + for (var featureId in this.featureChangeKeys_) { + var keys = this.featureChangeKeys_[featureId]; + goog.array.forEach(keys, goog.events.unlistenByKey); + } + this.featureChangeKeys_ = {}; + this.idIndex_ = {}; + this.undefIdIndex_ = {}; this.rBush_.clear(); - goog.object.forEach( - this.nullGeometryFeatures_, this.removeFeatureInternal, this); - goog.object.clear(this.nullGeometryFeatures_); - goog.asserts.assert(goog.object.isEmpty(this.featureChangeKeys_)); + this.nullGeometryFeatures_ = {}; + + var clearEvent = new ol.source.VectorEvent(ol.source.VectorEventType.CLEAR); + this.dispatchEvent(clearEvent); this.changed(); }; diff --git a/test/spec/ol/source/vectorsource.test.js b/test/spec/ol/source/vectorsource.test.js index 1ec5803a2c..e632efae11 100644 --- a/test/spec/ol/source/vectorsource.test.js +++ b/test/spec/ol/source/vectorsource.test.js @@ -90,13 +90,17 @@ describe('ol.source.Vector', function() { 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(); expect(vectorSource.getFeatures()).to.eql([]); expect(vectorSource.isEmpty()).to.be(true); expect(changeSpy).to.be.called(); expect(changeSpy.callCount).to.be(1); - expect(removeFeatureSpy).to.be.called(); - expect(removeFeatureSpy.callCount).to.be(features.length); + expect(removeFeatureSpy).not.to.be.called(); + expect(removeFeatureSpy.callCount).to.be(0); + expect(clearSourceSpy).to.be.called(); + expect(clearSourceSpy.callCount).to.be(1); }); });