From 343a4085a01af60d846dad7b61e7f1da8447be9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bobo=20H=C3=A4ggstr=C3=B6m?= Date: Thu, 19 Oct 2017 09:43:54 +0200 Subject: [PATCH 1/2] Add method to set max cache size in ol.style.IconImageCache --- src/ol/style/iconimagecache.js | 11 ++++++++++- test/spec/ol/style/iconimagecache.test.js | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/ol/style/iconimagecache.js b/src/ol/style/iconimagecache.js index 58b1baac69..64f621ec11 100644 --- a/src/ol/style/iconimagecache.js +++ b/src/ol/style/iconimagecache.js @@ -21,7 +21,6 @@ ol.style.IconImageCache = function() { this.cacheSize_ = 0; /** - * @const * @type {number} * @private */ @@ -91,3 +90,13 @@ ol.style.IconImageCache.prototype.set = function(src, crossOrigin, color, iconIm this.cache_[key] = iconImage; ++this.cacheSize_; }; + + +/** + * Set cache max size. + * @param {number} maxCacheSize Cache max size. + */ +ol.style.IconImageCache.prototype.setMaxCacheSize = function(maxCacheSize) { + this.maxCacheSize_ = maxCacheSize; + this.expire(); +}; diff --git a/test/spec/ol/style/iconimagecache.test.js b/test/spec/ol/style/iconimagecache.test.js index ef47345275..1c023470c3 100644 --- a/test/spec/ol/style/iconimagecache.test.js +++ b/test/spec/ol/style/iconimagecache.test.js @@ -66,4 +66,25 @@ describe('ol.style.IconImageCache', function() { expect(cache.get('4', null, null)).to.not.be(null); }); }); + + describe('#setMaxCacheSize', function() { + it('sets max cache size and expires cache', function() { + var cache = ol.style.iconImageCache; + + var i, src, iconImage; + + for (i = 0; i < 3; ++i) { + src = i + ''; + iconImage = new ol.style.IconImage(null, src); + cache.set(src, null, null, iconImage); + } + + expect(cache.cacheSize_).to.eql(3); + + cache.setMaxCacheSize(2); + + expect(cache.maxCacheSize_).to.eql(2); + expect(cache.cacheSize_).to.eql(2); + }); + }); }); From 8a9f32fa09a2852b56496c2eae8cb2c564120044 Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Fri, 1 Dec 2017 17:01:26 +0100 Subject: [PATCH 2/2] Rename method and make it appear in the API docs --- src/ol/style.js | 4 ++++ src/ol/style/iconimagecache.js | 8 ++++++-- test/spec/ol/style/iconimagecache.test.js | 4 ++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/ol/style.js b/src/ol/style.js index d3efbe46e4..8639a72651 100644 --- a/src/ol/style.js +++ b/src/ol/style.js @@ -2,4 +2,8 @@ goog.provide('ol.style'); goog.require('ol.style.IconImageCache'); +/** + * The {@link ol.style.IconImageCache} for {@link ol.style.Icon} images. + * @api + */ ol.style.iconImageCache = new ol.style.IconImageCache(); diff --git a/src/ol/style/iconimagecache.js b/src/ol/style/iconimagecache.js index 64f621ec11..7966e7bdc6 100644 --- a/src/ol/style/iconimagecache.js +++ b/src/ol/style/iconimagecache.js @@ -4,6 +4,7 @@ goog.require('ol.color'); /** + * Singleton class. Available through {@link ol.style.iconImageCache}. * @constructor */ ol.style.IconImageCache = function() { @@ -93,10 +94,13 @@ ol.style.IconImageCache.prototype.set = function(src, crossOrigin, color, iconIm /** - * Set cache max size. + * Set the cache size of the icon cache. Default is `32`. Change this value when + * your map uses more than 32 different icon images and you are not caching icon + * styles on the application level. * @param {number} maxCacheSize Cache max size. + * @api */ -ol.style.IconImageCache.prototype.setMaxCacheSize = function(maxCacheSize) { +ol.style.IconImageCache.prototype.setSize = function(maxCacheSize) { this.maxCacheSize_ = maxCacheSize; this.expire(); }; diff --git a/test/spec/ol/style/iconimagecache.test.js b/test/spec/ol/style/iconimagecache.test.js index 1c023470c3..bb3daa3321 100644 --- a/test/spec/ol/style/iconimagecache.test.js +++ b/test/spec/ol/style/iconimagecache.test.js @@ -67,7 +67,7 @@ describe('ol.style.IconImageCache', function() { }); }); - describe('#setMaxCacheSize', function() { + describe('#setSize', function() { it('sets max cache size and expires cache', function() { var cache = ol.style.iconImageCache; @@ -81,7 +81,7 @@ describe('ol.style.IconImageCache', function() { expect(cache.cacheSize_).to.eql(3); - cache.setMaxCacheSize(2); + cache.setSize(2); expect(cache.maxCacheSize_).to.eql(2); expect(cache.cacheSize_).to.eql(2);