Merge pull request #12168 from ahocevar/getfeatures-reference

VectorSource#getFeatures() consistently returns a new array
This commit is contained in:
Andreas Hocevar
2021-04-01 09:00:53 +02:00
committed by GitHub
2 changed files with 16 additions and 3 deletions

View File

@@ -653,14 +653,15 @@ class VectorSource extends Source {
} }
/** /**
* Get all features on the source in random order. * Get a snapshot of the features currently on the source in random order. The returned array
* is a copy, the features are references to the features in the source.
* @return {Array<import("../Feature.js").default<Geometry>>} Features. * @return {Array<import("../Feature.js").default<Geometry>>} Features.
* @api * @api
*/ */
getFeatures() { getFeatures() {
let features; let features;
if (this.featuresCollection_) { if (this.featuresCollection_) {
features = this.featuresCollection_.getArray(); features = this.featuresCollection_.getArray().slice(0);
} else if (this.featuresRtree_) { } else if (this.featuresRtree_) {
features = this.featuresRtree_.getAll(); features = this.featuresRtree_.getAll();
if (!isEmpty(this.nullGeometryFeatures_)) { if (!isEmpty(this.nullGeometryFeatures_)) {
@@ -700,7 +701,7 @@ class VectorSource extends Source {
if (this.featuresRtree_) { if (this.featuresRtree_) {
return this.featuresRtree_.getInExtent(extent); return this.featuresRtree_.getInExtent(extent);
} else if (this.featuresCollection_) { } else if (this.featuresCollection_) {
return this.featuresCollection_.getArray(); return this.featuresCollection_.getArray().slice(0);
} else { } else {
return []; return [];
} }

View File

@@ -143,6 +143,18 @@ describe('ol.source.Vector', function () {
expect(feature).to.be(features[0]); expect(feature).to.be(features[0]);
}); });
}); });
describe('#getFeatures', function () {
it('does not return the internal array when useSpatialIndex is false', function () {
const noSpatialIndexSource = new VectorSource({
useSpatialIndex: false,
features: vectorSource.getFeatures(),
});
expect(noSpatialIndexSource.getFeatures()).to.not.be(
noSpatialIndexSource.getFeaturesCollection().getArray()
);
});
});
}); });
describe('clear and refresh', function () { describe('clear and refresh', function () {