From a01893b27547da2c72e5203fa99cdec106cef669 Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Wed, 31 Mar 2021 18:17:04 +0200 Subject: [PATCH 1/2] VectorSource#getFeatures() consistently returns a new array --- src/ol/source/Vector.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ol/source/Vector.js b/src/ol/source/Vector.js index e686c8f335..6b68b481db 100644 --- a/src/ol/source/Vector.js +++ b/src/ol/source/Vector.js @@ -653,14 +653,15 @@ class VectorSource extends Source { } /** - * Get all features on the source in random order. + * Get all 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>} Features. * @api */ getFeatures() { let features; if (this.featuresCollection_) { - features = this.featuresCollection_.getArray(); + features = this.featuresCollection_.getArray().slice(0); } else if (this.featuresRtree_) { features = this.featuresRtree_.getAll(); if (!isEmpty(this.nullGeometryFeatures_)) { @@ -700,7 +701,7 @@ class VectorSource extends Source { if (this.featuresRtree_) { return this.featuresRtree_.getInExtent(extent); } else if (this.featuresCollection_) { - return this.featuresCollection_.getArray(); + return this.featuresCollection_.getArray().slice(0); } else { return []; } From 59d686d10492b21b06ef49a2782c0e2da21dbb5e Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Thu, 1 Apr 2021 08:38:29 +0200 Subject: [PATCH 2/2] Fix typo, add test --- src/ol/source/Vector.js | 2 +- test/spec/ol/source/vector.test.js | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/ol/source/Vector.js b/src/ol/source/Vector.js index 6b68b481db..ce4d8a2ebc 100644 --- a/src/ol/source/Vector.js +++ b/src/ol/source/Vector.js @@ -653,7 +653,7 @@ class VectorSource extends Source { } /** - * Get all snapshot of the features currently on the source in random order. The returned array + * 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>} Features. * @api diff --git a/test/spec/ol/source/vector.test.js b/test/spec/ol/source/vector.test.js index ad69fea674..6940d5fe6f 100644 --- a/test/spec/ol/source/vector.test.js +++ b/test/spec/ol/source/vector.test.js @@ -143,6 +143,18 @@ describe('ol.source.Vector', function () { 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 () {