From baf424f99c7dd240f81d75d7555e97f9f4cfe577 Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Wed, 14 Jan 2015 10:40:56 +0100 Subject: [PATCH] Only update the rbush item if the extent has changed --- src/ol/structs/rbush.js | 14 ++++++++++++-- test/spec/ol/structs/rbush.test.js | 8 ++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/ol/structs/rbush.js b/src/ol/structs/rbush.js index ec2ac67355..7efa537782 100644 --- a/src/ol/structs/rbush.js +++ b/src/ol/structs/rbush.js @@ -4,6 +4,7 @@ goog.require('goog.array'); goog.require('goog.asserts'); goog.require('goog.object'); goog.require('ol.ext.rbush'); +goog.require('ol.extent'); @@ -123,8 +124,17 @@ ol.structs.RBush.prototype.remove = function(value) { * @param {T} value Value. */ ol.structs.RBush.prototype.update = function(extent, value) { - this.remove(value); - this.insert(extent, value); + var uid = goog.getUid(value); + goog.asserts.assert(goog.object.containsKey(this.items_, uid)); + + var item = this.items_[uid]; + if (!ol.extent.equals(item.slice(0, 4), extent)) { + if (goog.DEBUG && this.readers_) { + throw new Error('Can not update extent while reading'); + } + this.remove(value); + this.insert(extent, value); + } }; diff --git a/test/spec/ol/structs/rbush.test.js b/test/spec/ol/structs/rbush.test.js index c7653295c3..44117fb360 100644 --- a/test/spec/ol/structs/rbush.test.js +++ b/test/spec/ol/structs/rbush.test.js @@ -44,6 +44,14 @@ describe('ol.structs.RBush', function() { expect(rBush.getInExtent([2, 2, 3, 3])).to.eql([obj]); }); + it('don\'t throws an exception if the extent is not modified', function() { + expect(function() { + rBush.forEach(function(value) { + rBush.update([0, 0, 1, 1], obj); + }); + }).not.to.throwException(); + }); + }); describe('with a few objects', function() {