Skip to content

Commit

Permalink
LRUCache on style.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ftoromanoff committed Jan 20, 2025
1 parent 71f8cb6 commit cecc413
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/Core/Style.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FEATURE_TYPES } from 'Core/Feature';
import Cache from 'Core/Scheduler/Cache';
import { LRUCache } from 'lru-cache';
import Fetcher from 'Provider/Fetcher';
import * as maplibre from '@maplibre/maplibre-gl-style-spec';
import { Color } from 'three';
Expand All @@ -8,7 +8,7 @@ import Coordinates from 'Core/Geographic/Coordinates';

import itowns_stroke_single_before from './StyleChunk/itowns_stroke_single_before.css';

const cacheStyle = new Cache();
const cachedImg = new LRUCache({ max: 500 });

const matrix = document.createElementNS('http://www.w3.org/2000/svg', 'svg').createSVGMatrix();
const canvas = document.createElement('canvas');
Expand Down Expand Up @@ -79,11 +79,11 @@ function readVectorProperty(property, options) {
}

async function loadImage(url) {
const source = url.split('?')[0];
let promise = cacheStyle.get(source);
const imgUrl = url.split('?')[0];
let promise = cachedImg.get(imgUrl);
if (!promise) {
promise = Fetcher.texture(url, { crossOrigin: 'anonymous' });
cacheStyle.set(promise, source);
cachedImg.set(promise, imgUrl);
}
return (await promise).image;
}
Expand All @@ -106,7 +106,7 @@ function replaceWhitePxl(imgd, color, id) {
if (!color) {
return imgd;
}
const imgdColored = cacheStyle.get(id, color);
const imgdColored = cachedImg.get(`${id}_${color}`);
if (!imgdColored) {
const pix = imgd.data;
const newColor = new Color(color);
Expand All @@ -117,7 +117,7 @@ function replaceWhitePxl(imgd, color, id) {
pix[i + 1] = (pix[i + 1] * d + newColor.g * 255 * (1 - d));
pix[i + 2] = (pix[i + 2] * d + newColor.b * 255 * (1 - d));
}
cacheStyle.set(imgd, id, color);
cachedImg.set(imgd, `${id}_${color}`);
return imgd;
}
return imgdColored;
Expand Down

0 comments on commit cecc413

Please sign in to comment.