More cleanup for the tiled layer cache.

This commit is contained in:
Eric van der Vlist 2022-11-01 11:46:54 +01:00
parent a6f10b9ee0
commit 04d2aa1ac1
2 changed files with 12 additions and 5 deletions

View File

@ -49,7 +49,6 @@ export interface TileSetProperties {
* Idea stolen [on the web](https://dev.to/tiagof/react-re-mounting-vs-re-rendering-lnh) * Idea stolen [on the web](https://dev.to/tiagof/react-re-mounting-vs-re-rendering-lnh)
* *
* *
*
*/ */
export const TileSet: react.FC<TileSetProperties> = memo( export const TileSet: react.FC<TileSetProperties> = memo(
(props: TileSetProperties) => { (props: TileSetProperties) => {
@ -96,14 +95,21 @@ export const TileSet: react.FC<TileSetProperties> = memo(
}); });
} }
); );
if (tiles.size > tileSetConfig.cacheSize) { if (tiles.size > tileSetConfig.cacheSizePerLayer) {
const oldestTileKeys = [...tiles.keys()]; const oldestTileKeys = [...tiles.keys()];
oldestTileKeys.splice(-tileSetConfig.cacheSize); oldestTileKeys.splice(-tileSetConfig.cacheSizePerLayer);
oldestTileKeys.forEach((tileKey) => { oldestTileKeys.forEach((tileKey) => {
tiles.delete(tileKey); tiles.delete(tileKey);
}); });
} }
globalThis.cacheForTileSet.set(key, tiles); globalThis.cacheForTileSet.set(key, tiles);
if (globalThis.cacheForTileSet > tileSetConfig.numberOfCachedLayers) {
const oldestCachedLayerKeys = [...globalThis.cacheForTileSet.keys()];
oldestCachedLayerKeys.slice(-tileSetConfig.numberOfCachedLayers);
oldestCachedLayerKeys.forEach((cachedLayerKey) => {
globalThis.cacheForTileSet.delete(cachedLayerKey);
});
}
} }
return <>{Array.from(tiles.values())}</>; return <>{Array.from(tiles.values())}</>;

View File

@ -11,5 +11,6 @@ export const handlersConfig = {
}; };
export const tileSetConfig = { export const tileSetConfig = {
cacheSize: 1000, cacheSizePerLayer: 1000,
} numberOfCachedLayers: 20,
};