From 065663b2429a2f03f1fbb3311d62b717b6cbfa69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Mon, 3 Feb 2014 14:21:59 +0100 Subject: [PATCH] Introduce an icon image cache --- src/ol/style/iconstyle.js | 81 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/src/ol/style/iconstyle.js b/src/ol/style/iconstyle.js index 827ed4bd39..c04b9a9bd8 100644 --- a/src/ol/style/iconstyle.js +++ b/src/ol/style/iconstyle.js @@ -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.} + * @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_; +};