Skip to content

Commit

Permalink
feat: Allow 0s for maxCacheSize and maxCacheByteSize (#9156)
Browse files Browse the repository at this point in the history
  • Loading branch information
Raimund Schnürer authored and felixpalmer committed Sep 23, 2024
1 parent 15542db commit aec90ee
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/api-reference/geo-layers/tile-layer.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ If not supplied, the `maxCacheSize` is calculated as `5` times the number of til

The maximum memory used for caching tiles. If this limit is supplied, `getTileData` must return an object that contains a `byteLength` field.

If not supplied, the `maxCacheByteSize` is set to `Infinity`.

- Default: `null`


Expand Down
10 changes: 5 additions & 5 deletions modules/geo-layers/src/tileset-2d/tileset-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class Tileset2D {

this.onTileLoad = tile => {
this.opts.onTileLoad?.(tile);
if (this.opts.maxCacheByteSize) {
if (this.opts.maxCacheByteSize != null) {
this._cacheByteSize += tile.byteLength;
this._resizeCache();
}
Expand Down Expand Up @@ -469,18 +469,18 @@ export class Tileset2D {
const {_cache, opts} = this;

const maxCacheSize =
opts.maxCacheSize ||
opts.maxCacheSize ??
// @ts-expect-error called only when selectedTiles is initialized
(opts.maxCacheByteSize ? Infinity : DEFAULT_CACHE_SCALE * this.selectedTiles.length);
const maxCacheByteSize = opts.maxCacheByteSize || Infinity;
(opts.maxCacheByteSize != null ? Infinity : DEFAULT_CACHE_SCALE * this.selectedTiles.length);
const maxCacheByteSize = opts.maxCacheByteSize ?? Infinity;

const overflown = _cache.size > maxCacheSize || this._cacheByteSize > maxCacheByteSize;

if (overflown) {
for (const [id, tile] of _cache) {
if (!tile.isVisible && !tile.isSelected) {
// delete tile
this._cacheByteSize -= opts.maxCacheByteSize ? tile.byteLength : 0;
this._cacheByteSize -= opts.maxCacheByteSize != null ? tile.byteLength : 0;
_cache.delete(id);
this.opts.onTileUnload?.(tile);
}
Expand Down

0 comments on commit aec90ee

Please sign in to comment.