Introduce an icon image cache

This commit is contained in:
Éric Lemoine
2014-02-03 14:21:59 +01:00
parent bf8520096e
commit 065663b242

View File

@@ -2,6 +2,7 @@
goog.provide('ol.style.Icon');
goog.provide('ol.style.IconAnchorUnits');
goog.provide('ol.style.IconImageCache');
goog.require('goog.array');
goog.require('goog.asserts');
@@ -395,3 +396,83 @@ ol.style.IconImage_.prototype.unlistenImage_ = function() {
goog.array.forEach(this.imageListenerKeys_, goog.events.unlistenByKey);
this.imageListenerKeys_ = null;
};
/**
* @constructor
*/
ol.style.IconImageCache = function() {
/**
* @type {Object.<string, ol.style.IconImage_>}
* @private
*/
this.cache_ = {};
/**
* @type {number}
* @private
*/
this.cacheSize_ = 0;
/**
* @const
* @type {number}
* @private
*/
this.maxCacheSize_ = 32;
};
goog.addSingletonGetter(ol.style.IconImageCache);
/**
* @param {string} src Src.
* @param {?string} crossOrigin Cross origin.
* @return {string} Cache key.
*/
ol.style.IconImageCache.getKey = function(src, crossOrigin) {
goog.asserts.assert(goog.isDef(crossOrigin));
return crossOrigin + ':' + src;
};
/**
* FIXME empty description for jsdoc
*/
ol.style.IconImageCache.prototype.expire = function() {
if (this.cacheSize_ > this.maxCacheSize_) {
var i = 0;
var key, iconImage;
for (key in this.cache_) {
iconImage = this.cache_[key];
if ((i++ & 3) === 0 && !goog.events.hasListener(iconImage)) {
delete this.cache_[key];
--this.cacheSize_;
}
}
}
};
/**
* @param {string} src Src.
* @param {?string} crossOrigin Cross origin.
* @return {ol.style.IconImage_} Icon image.
*/
ol.style.IconImageCache.prototype.get = function(src, crossOrigin) {
var key = ol.style.IconImageCache.getKey(src, crossOrigin);
return key in this.cache_ ? this.cache_[key] : null;
};
/**
* @param {string} src Src.
* @param {?string} crossOrigin Cross origin.
* @param {ol.style.IconImage_} iconImage Icon image.
*/
ol.style.IconImageCache.prototype.set = function(src, crossOrigin, iconImage) {
var key = ol.style.IconImageCache.getKey(src, crossOrigin);
this.cache_[key] = iconImage;
++this.cacheSize_;
};