# all files / ol/style/ iconimagecache.js

100% Statements 28/28
100% Branches 10/10
100% Functions 6/6
100% Lines 28/28

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    941×

    1×

    1×

    1×

    1×

    1×

    1×
    77×
    77×

    1×
    2×
    2×

    1×
    504×
    2×
    2×
    2×
    10×
    10×
    3×
    3×

    1×
    52×
    52×

    1×
    25×
    25×
    25×

     goog.provide('ol.style.IconImageCache');

    goog.require('ol.color');

    /**
     * @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;
    };

    /**
     * @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) {
      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_;
    };