Add optional fast parameter for clearing vector source

This commit is contained in:
Guillaume Beraudo
2014-12-05 16:38:00 +01:00
parent 17e56d8357
commit e3947fb09a
2 changed files with 42 additions and 12 deletions

View File

@@ -37,14 +37,15 @@ ol.source.VectorEventType = {
CHANGEFEATURE: 'changefeature', 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 * @event ol.source.VectorEvent#clear
* @api * @api
*/ */
CLEAR: 'clear', 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 * @event ol.source.VectorEvent#removefeature
* @api stable * @api stable
*/ */
@@ -234,9 +235,11 @@ ol.source.Vector.prototype.addFeaturesInternal = function(features) {
/** /**
* Remove all features from the source. * Remove all features from the source.
* @param {boolean=} opt_fast Skip dispatching of {@link removefeature} events.
* @api stable * @api stable
*/ */
ol.source.Vector.prototype.clear = function() { ol.source.Vector.prototype.clear = function(opt_fast) {
if (opt_fast) {
for (var featureId in this.featureChangeKeys_) { for (var featureId in this.featureChangeKeys_) {
var keys = this.featureChangeKeys_[featureId]; var keys = this.featureChangeKeys_[featureId];
goog.array.forEach(keys, goog.events.unlistenByKey); goog.array.forEach(keys, goog.events.unlistenByKey);
@@ -244,6 +247,15 @@ ol.source.Vector.prototype.clear = function() {
this.featureChangeKeys_ = {}; this.featureChangeKeys_ = {};
this.idIndex_ = {}; this.idIndex_ = {};
this.undefIdIndex_ = {}; 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.rBush_.clear(); this.rBush_.clear();
this.nullGeometryFeatures_ = {}; this.nullGeometryFeatures_ = {};

View File

@@ -85,7 +85,25 @@ describe('ol.source.Vector', function() {
describe('#clear', 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(); var changeSpy = sinon.spy();
goog.events.listen(vectorSource, 'change', changeSpy); goog.events.listen(vectorSource, 'change', changeSpy);
var removeFeatureSpy = sinon.spy(); var removeFeatureSpy = sinon.spy();
@@ -97,8 +115,8 @@ describe('ol.source.Vector', function() {
expect(vectorSource.isEmpty()).to.be(true); expect(vectorSource.isEmpty()).to.be(true);
expect(changeSpy).to.be.called(); expect(changeSpy).to.be.called();
expect(changeSpy.callCount).to.be(1); expect(changeSpy.callCount).to.be(1);
expect(removeFeatureSpy).not.to.be.called(); expect(removeFeatureSpy).to.be.called();
expect(removeFeatureSpy.callCount).to.be(0); expect(removeFeatureSpy.callCount).to.be(features.length);
expect(clearSourceSpy).to.be.called(); expect(clearSourceSpy).to.be.called();
expect(clearSourceSpy.callCount).to.be(1); expect(clearSourceSpy.callCount).to.be(1);
}); });