From a70c6f916ba374ea87f9e52a7e9d73ea6b095ed0 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Tue, 9 Aug 2016 08:56:15 -0600 Subject: [PATCH] Give ol.style.IconImageCache its own file --- src/ol/renderer/map.js | 4 +- src/ol/style/icon.js | 98 ----------------------- src/ol/style/iconimagecache.js | 97 ++++++++++++++++++++++ src/ol/style/index.js | 5 ++ src/ol/{style.jsdoc => style/index.jsdoc} | 0 test/spec/ol/style/icon.test.js | 3 +- 6 files changed, 106 insertions(+), 101 deletions(-) create mode 100644 src/ol/style/iconimagecache.js create mode 100644 src/ol/style/index.js rename src/ol/{style.jsdoc => style/index.jsdoc} (100%) diff --git a/src/ol/renderer/map.js b/src/ol/renderer/map.js index 841d9b4b9a..e9291e5584 100644 --- a/src/ol/renderer/map.js +++ b/src/ol/renderer/map.js @@ -1,7 +1,6 @@ goog.provide('ol.renderer.Map'); goog.provide('ol.RendererType'); -goog.require('ol.transform'); goog.require('ol'); goog.require('ol.Disposable'); goog.require('ol.events'); @@ -9,7 +8,8 @@ goog.require('ol.events.EventType'); goog.require('ol.extent'); goog.require('ol.functions'); goog.require('ol.layer.Layer'); -goog.require('ol.style.iconImageCache'); +goog.require('ol.style'); +goog.require('ol.transform'); /** diff --git a/src/ol/style/icon.js b/src/ol/style/icon.js index f8f95ce751..184ed424e1 100644 --- a/src/ol/style/icon.js +++ b/src/ol/style/icon.js @@ -1,7 +1,6 @@ goog.provide('ol.style.Icon'); goog.provide('ol.style.IconAnchorUnits'); goog.provide('ol.style.IconOrigin'); -goog.provide('ol.style.iconImageCache'); goog.require('ol'); goog.require('ol.asserts'); @@ -363,100 +362,3 @@ ol.style.Icon.prototype.unlistenImageChange = function(listener, thisArg) { ol.events.unlisten(this.iconImage_, ol.events.EventType.CHANGE, listener, thisArg); }; - - -/** - * @constructor - * @private - */ -ol.style.IconImageCache_ = function() { - - /** - * @type {Object.} - * @private - */ - this.cache_ = {}; - - /** - * @type {number} - * @private - */ - this.cacheSize_ = 0; - - /** - * @const - * @type {number} - * @private - */ - this.maxCacheSize_ = 32; -}; - - -/** - * @param {string} src Src. - * @param {?string} crossOrigin Cross origin. - * @param {ol.Color} color Color. - * @return {string} Cache key. - */ -ol.style.IconImageCache_.getKey = function(src, crossOrigin, color) { - goog.DEBUG && console.assert(crossOrigin !== undefined, - 'argument crossOrigin must be defined'); - var colorString = color ? ol.color.asString(color) : 'null'; - return crossOrigin + ':' + src + ':' + colorString; -}; - - -/** - * FIXME empty description for jsdoc - */ -ol.style.IconImageCache_.prototype.clear = function() { - this.cache_ = {}; - this.cacheSize_ = 0; -}; - - -/** - * 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 && !iconImage.hasListener()) { - delete this.cache_[key]; - --this.cacheSize_; - } - } - } -}; - - -/** - * @param {string} src Src. - * @param {?string} crossOrigin Cross origin. - * @param {ol.Color} color Color. - * @return {ol.style.IconImage} Icon image. - */ -ol.style.IconImageCache_.prototype.get = function(src, crossOrigin, color) { - var key = ol.style.IconImageCache_.getKey(src, crossOrigin, color); - return key in this.cache_ ? this.cache_[key] : null; -}; - - -/** - * @param {string} src Src. - * @param {?string} crossOrigin Cross origin. - * @param {ol.Color} color Color. - * @param {ol.style.IconImage} iconImage Icon image. - */ -ol.style.IconImageCache_.prototype.set = function(src, crossOrigin, color, - iconImage) { - var key = ol.style.IconImageCache_.getKey(src, crossOrigin, color); - this.cache_[key] = iconImage; - ++this.cacheSize_; -}; - - -ol.style.iconImageCache = new ol.style.IconImageCache_(); diff --git a/src/ol/style/iconimagecache.js b/src/ol/style/iconimagecache.js new file mode 100644 index 0000000000..aab810c273 --- /dev/null +++ b/src/ol/style/iconimagecache.js @@ -0,0 +1,97 @@ +goog.provide('ol.style.IconImageCache'); + +goog.require('ol.color'); + + +/** + * @constructor + * @private + */ +ol.style.IconImageCache = function() { + + /** + * @type {Object.} + * @private + */ + this.cache_ = {}; + + /** + * @type {number} + * @private + */ + this.cacheSize_ = 0; + + /** + * @const + * @type {number} + * @private + */ + this.maxCacheSize_ = 32; +}; + + +/** + * @param {string} src Src. + * @param {?string} crossOrigin Cross origin. + * @param {ol.Color} color Color. + * @return {string} Cache key. + */ +ol.style.IconImageCache.getKey = function(src, crossOrigin, color) { + goog.DEBUG && console.assert(crossOrigin !== undefined, + 'argument crossOrigin must be defined'); + var colorString = color ? ol.color.asString(color) : 'null'; + return crossOrigin + ':' + src + ':' + colorString; +}; + + +/** + * FIXME empty description for jsdoc + */ +ol.style.IconImageCache.prototype.clear = function() { + this.cache_ = {}; + this.cacheSize_ = 0; +}; + + +/** + * 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 && !iconImage.hasListener()) { + delete this.cache_[key]; + --this.cacheSize_; + } + } + } +}; + + +/** + * @param {string} src Src. + * @param {?string} crossOrigin Cross origin. + * @param {ol.Color} color Color. + * @return {ol.style.IconImage} Icon image. + */ +ol.style.IconImageCache.prototype.get = function(src, crossOrigin, color) { + var key = ol.style.IconImageCache.getKey(src, crossOrigin, color); + return key in this.cache_ ? this.cache_[key] : null; +}; + + +/** + * @param {string} src Src. + * @param {?string} crossOrigin Cross origin. + * @param {ol.Color} color Color. + * @param {ol.style.IconImage} iconImage Icon image. + */ +ol.style.IconImageCache.prototype.set = function(src, crossOrigin, color, + iconImage) { + var key = ol.style.IconImageCache.getKey(src, crossOrigin, color); + this.cache_[key] = iconImage; + ++this.cacheSize_; +}; diff --git a/src/ol/style/index.js b/src/ol/style/index.js new file mode 100644 index 0000000000..d3efbe46e4 --- /dev/null +++ b/src/ol/style/index.js @@ -0,0 +1,5 @@ +goog.provide('ol.style'); + +goog.require('ol.style.IconImageCache'); + +ol.style.iconImageCache = new ol.style.IconImageCache(); diff --git a/src/ol/style.jsdoc b/src/ol/style/index.jsdoc similarity index 100% rename from src/ol/style.jsdoc rename to src/ol/style/index.jsdoc diff --git a/test/spec/ol/style/icon.test.js b/test/spec/ol/style/icon.test.js index b2ec97dcf5..c2adf7a564 100644 --- a/test/spec/ol/style/icon.test.js +++ b/test/spec/ol/style/icon.test.js @@ -3,8 +3,9 @@ goog.provide('ol.test.style.IconImageCache'); goog.require('ol'); goog.require('ol.events'); -goog.require('ol.style.IconImage'); +goog.require('ol.style'); goog.require('ol.style.Icon'); +goog.require('ol.style.IconImage'); describe('ol.style.Icon', function() {