Give ol.style.IconImageCache its own file
This commit is contained in:
@@ -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');
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -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.<string, ol.style.IconImage>}
|
||||
* @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_();
|
||||
|
||||
97
src/ol/style/iconimagecache.js
Normal file
97
src/ol/style/iconimagecache.js
Normal file
@@ -0,0 +1,97 @@
|
||||
goog.provide('ol.style.IconImageCache');
|
||||
|
||||
goog.require('ol.color');
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @private
|
||||
*/
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @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_;
|
||||
};
|
||||
5
src/ol/style/index.js
Normal file
5
src/ol/style/index.js
Normal file
@@ -0,0 +1,5 @@
|
||||
goog.provide('ol.style');
|
||||
|
||||
goog.require('ol.style.IconImageCache');
|
||||
|
||||
ol.style.iconImageCache = new ol.style.IconImageCache();
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user