UNPKG

@deck.gl/experimental-layers

Version:

Experimental layers for deck.gl

208 lines (166 loc) 6.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _tile = _interopRequireDefault(require("./tile")); var _viewportUtil = require("./viewport-util"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } /** * Manages loading and purging of tiles data. This class caches recently visited tiles * and only create new tiles if they are present. */ var TileCache = /*#__PURE__*/ function () { /** * Takes in a function that returns tile data, a cache size, and a max and a min zoom level. * Cache size defaults to 5 * number of tiles in the current viewport */ function TileCache(_ref) { var getTileData = _ref.getTileData, maxSize = _ref.maxSize, maxZoom = _ref.maxZoom, minZoom = _ref.minZoom, onGetTileDataError = _ref.onGetTileDataError; _classCallCheck(this, TileCache); // TODO: Instead of hardcode size, we should calculate how much memory left this._getTileData = getTileData; this._maxSize = maxSize; this.onGetTileDataError = onGetTileDataError; // Maps tile id in string {z}-{x}-{y} to a Tile object this._cache = new Map(); if (maxZoom && parseInt(maxZoom, 10) === maxZoom) { this._maxZoom = maxZoom; } if (minZoom && parseInt(minZoom, 10) === minZoom) { this._minZoom = minZoom; } } /** * Clear the current cache */ _createClass(TileCache, [{ key: "finalize", value: function finalize() { this._cache.clear(); } /** * Update the cache with the given viewport and triggers callback onUpdate. * @param {*} viewport * @param {*} onUpdate */ }, { key: "update", value: function update(viewport, onUpdate) { var _cache = this._cache, _getTileData = this._getTileData, _maxSize = this._maxSize, _maxZoom = this._maxZoom, _minZoom = this._minZoom; this._markOldTiles(); var tileIndices = (0, _viewportUtil.getTileIndices)(viewport, _maxZoom, _minZoom); if (!tileIndices || tileIndices.length === 0) { onUpdate(tileIndices); return; } var viewportTiles = new Set(); _cache.forEach(function (cachedTile) { if (tileIndices.some(function (tile) { return cachedTile.isOverlapped(tile); })) { cachedTile.isVisible = true; viewportTiles.add(cachedTile); } }); for (var i = 0; i < tileIndices.length; i++) { var tileIndex = tileIndices[i]; var x = tileIndex.x, y = tileIndex.y, z = tileIndex.z; var tile = this._getTile(x, y, z); if (!tile) { tile = new _tile.default({ getTileData: _getTileData, x: x, y: y, z: z, onGetTileDataError: this.onGetTileDataError }); } var tileId = this._getTileId(x, y, z); _cache.set(tileId, tile); viewportTiles.add(tile); } // cache size is either the user defined maxSize or 5 * number of current tiles in the viewport. var commonZoomRange = 5; this._resizeCache(_maxSize || commonZoomRange * tileIndices.length); // sort by zoom level so parents tiles don't show up when children tiles are rendered var viewportTilesArray = Array.from(viewportTiles).sort(function (t1, t2) { return t1.z - t2.z; }); onUpdate(viewportTilesArray); } /** * Clear tiles that are not visible when the cache is full */ }, { key: "_resizeCache", value: function _resizeCache(maxSize) { var _cache = this._cache; if (_cache.size > maxSize) { var iterator = _cache[Symbol.iterator](); var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = iterator[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var cachedTile = _step.value; if (_cache.size <= maxSize) { break; } var tileId = cachedTile[0]; var tile = cachedTile[1]; if (!tile.isVisible) { _cache.delete(tileId); } } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } } } }, { key: "_markOldTiles", value: function _markOldTiles() { this._cache.forEach(function (cachedTile) { cachedTile.isVisible = false; }); } }, { key: "_getTile", value: function _getTile(x, y, z) { var tileId = this._getTileId(x, y, z); return this._cache.get(tileId); } }, { key: "_getTileId", value: function _getTileId(x, y, z) { return "".concat(z, "-").concat(x, "-").concat(y); } }]); return TileCache; }(); exports.default = TileCache; //# sourceMappingURL=tile-cache.js.map