removeFeature consistent with other remove methods

Make returning the removed feature a TODO
This commit is contained in:
mike-000
2021-10-24 20:33:48 +01:00
committed by GitHub
parent 658f55d03f
commit 855fc6f5bc
+22 -4
View File
@@ -544,7 +544,10 @@ class VectorSource extends Source {
} }
} else { } else {
if (this.featuresRtree_) { if (this.featuresRtree_) {
this.featuresRtree_.forEach(this.removeFeatureInternal.bind(this)); // use Array forEach to ignore return
this.featuresRtree_
.getAll()
.forEach(this.removeFeatureInternal.bind(this));
for (const id in this.nullGeometryFeatures_) { for (const id in this.nullGeometryFeatures_) {
this.removeFeatureInternal(this.nullGeometryFeatures_[id]); this.removeFeatureInternal(this.nullGeometryFeatures_[id]);
} }
@@ -1019,6 +1022,9 @@ class VectorSource extends Source {
* @api * @api
*/ */
removeFeature(feature) { removeFeature(feature) {
if (!feature) {
return;
}
const featureKey = getUid(feature); const featureKey = getUid(feature);
if (featureKey in this.nullGeometryFeatures_) { if (featureKey in this.nullGeometryFeatures_) {
delete this.nullGeometryFeatures_[featureKey]; delete this.nullGeometryFeatures_[featureKey];
@@ -1027,18 +1033,29 @@ class VectorSource extends Source {
this.featuresRtree_.remove(feature); this.featuresRtree_.remove(feature);
} }
} }
this.removeFeatureInternal(feature); const result = this.removeFeatureInternal(feature);
this.changed(); if (result) {
this.changed();
}
// TODO at full version for consistency with other remove methods
// (would be breaking change if used as callback in forEachFeatureAtPixel)
//return result;
} }
/** /**
* Remove feature without firing a `change` event. * Remove feature without firing a `change` event.
* @param {import("../Feature.js").default<Geometry>} feature Feature. * @param {import("../Feature.js").default<Geometry>} feature Feature.
* @return {import("../Feature.js").default<Geometry>|undefined} The removed feature
* (or undefined if the feature was not found).
* @protected * @protected
*/ */
removeFeatureInternal(feature) { removeFeatureInternal(feature) {
const featureKey = getUid(feature); const featureKey = getUid(feature);
this.featureChangeKeys_[featureKey].forEach(unlistenByKey); const featureChangeKeys = this.featureChangeKeys_[featureKey];
if (!featureChangeKeys) {
return;
}
featureChangeKeys.forEach(unlistenByKey);
delete this.featureChangeKeys_[featureKey]; delete this.featureChangeKeys_[featureKey];
const id = feature.getId(); const id = feature.getId();
if (id !== undefined) { if (id !== undefined) {
@@ -1048,6 +1065,7 @@ class VectorSource extends Source {
this.dispatchEvent( this.dispatchEvent(
new VectorSourceEvent(VectorEventType.REMOVEFEATURE, feature) new VectorSourceEvent(VectorEventType.REMOVEFEATURE, feature)
); );
return feature;
} }
/** /**