# all files / ol/ tilequeue.js

100% Statements 48/48
94.12% Branches 16/17
100% Functions 7/7
100% Lines 48/48

    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
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    1321×
    1×

    1×
    1×
    1×
    1×
    1×
    1×

    1×

    1×

    175×

    46×

    225×

    175×

    175×

    175×

    1×

    1×
    44×
    44×
    44×
    44×

    44×

    1×
    9×

    1×
    87×
    87×
    87×

    44×

    44×
    44×
    23×
    23×

    44×

    87×

    1×
    15×
    15×
    15×

    43×
    43×
    43×
    23×
    23×
    23×
    23×

    43×

     goog.provide('ol.TilePriorityFunction');
    goog.provide('ol.TileQueue');

    goog.require('goog.asserts');
    goog.require('ol.events');
    goog.require('ol.events.EventType');
    goog.require('ol.Coordinate');
    goog.require('ol.TileState');
    goog.require('ol.structs.PriorityQueue');

    /**
     * @typedef {function(ol.Tile, string, ol.Coordinate, number): number}
     */
    ol.TilePriorityFunction;

    /**
     * @constructor
     * @extends {ol.structs.PriorityQueue.<Array>}
     * @param {ol.TilePriorityFunction} tilePriorityFunction
     *     Tile priority function.
     * @param {function(): ?} tileChangeCallback
     *     Function called on each tile change event.
     * @struct
     */
    ol.TileQueue = function(tilePriorityFunction, tileChangeCallback) {

      goog.base(
          this,
          /**
           * @param {Array} element Element.
           * @return {number} Priority.
           */
          function(element) {
            return tilePriorityFunction.apply(null, element);
          },
          /**
           * @param {Array} element Element.
           * @return {string} Key.
           */
          function(element) {
            return /** @type {ol.Tile} */ (element[0]).getKey();
          });

      /**
       * @private
       * @type {function(): ?}
       */
      this.tileChangeCallback_ = tileChangeCallback;

      /**
       * @private
       * @type {number}
       */
      this.tilesLoading_ = 0;

      /**
       * @private
       * @type {!Object.<string,boolean>}
       */
      this.tilesLoadingKeys_ = {};

    };
    goog.inherits(ol.TileQueue, ol.structs.PriorityQueue);

    /**
     * @inheritDoc
     */
    ol.TileQueue.prototype.enqueue = function(element) {
      var added = goog.base(this, 'enqueue', element);
      Eif (added) {
        var tile = element[0];
        ol.events.listen(tile, ol.events.EventType.CHANGE,
            this.handleTileChange, this);
      }
      return added;
    };

    /**
     * @return {number} Number of tiles loading.
     */
    ol.TileQueue.prototype.getTilesLoading = function() {
      return this.tilesLoading_;
    };

    /**
     * @param {ol.events.Event} event Event.
     * @protected
     */
    ol.TileQueue.prototype.handleTileChange = function(event) {
      var tile = /** @type {ol.Tile} */ (event.target);
      var state = tile.getState();
      if (state === ol.TileState.LOADED || state === ol.TileState.ERROR ||
          state === ol.TileState.EMPTY || state === ol.TileState.ABORT) {
        ol.events.unlisten(tile, ol.events.EventType.CHANGE,
            this.handleTileChange, this);
        var tileKey = tile.getKey();
        if (tileKey in this.tilesLoadingKeys_) {
          delete this.tilesLoadingKeys_[tileKey];
          --this.tilesLoading_;
        }
        this.tileChangeCallback_();
      }
      goog.asserts.assert(Object.keys(this.tilesLoadingKeys_).length === this.tilesLoading_);
    };

    /**
     * @param {number} maxTotalLoading Maximum number tiles to load simultaneously.
     * @param {number} maxNewLoads Maximum number of new tiles to load.
     */
    ol.TileQueue.prototype.loadMoreTiles = function(maxTotalLoading, maxNewLoads) {
      var newLoads = 0;
      var tile, tileKey;
      while (this.tilesLoading_ < maxTotalLoading && newLoads < maxNewLoads &&
             this.getCount() > 0) {
        tile = /** @type {ol.Tile} */ (this.dequeue()[0]);
        tileKey = tile.getKey();
        if (tile.getState() === ol.TileState.IDLE && !(tileKey in this.tilesLoadingKeys_)) {
          this.tilesLoadingKeys_[tileKey] = true;
          ++this.tilesLoading_;
          ++newLoads;
          tile.load();
        }
        goog.asserts.assert(Object.keys(this.tilesLoadingKeys_).length === this.tilesLoading_);
      }
    };