Make _seen local to a single render and make it safer by using Set

This commit is contained in:
Christian Paul 2024-11-03 17:02:46 +01:00
parent 91855972b8
commit af464f3c9b

View File

@ -47,7 +47,6 @@ class Renderer {
private styler: Styler; private styler: Styler;
private tileSource: TileSource; private tileSource: TileSource;
private width: number; private width: number;
private _seen: Record<string, boolean>;
private terminal = { private terminal = {
CLEAR: '\x1B[2J', CLEAR: '\x1B[2J',
@ -78,7 +77,7 @@ class Renderer {
this.isDrawing = true; this.isDrawing = true;
this.labelBuffer.clear(); this.labelBuffer.clear();
this._seen = {}; const seen = new Set<string>();
let ref; let ref;
const color = ((ref = this.styler.styleById['background']) !== null ? const color = ((ref = this.styler.styleById['background']) !== null ?
@ -98,7 +97,7 @@ class Renderer {
await this._getTile(tile); await this._getTile(tile);
this._getTileFeatures(tile, zoom); this._getTileFeatures(tile, zoom);
})); }));
await this._renderTiles(tiles); await this._renderTiles(tiles, seen);
return this._getFrame(); return this._getFrame();
} catch (err) { } catch (err) {
console.error(err); console.error(err);
@ -176,7 +175,7 @@ class Renderer {
return tile; return tile;
} }
private _renderTiles(tiles) { private _renderTiles(tiles, seen: Set<string>) {
const labels: { const labels: {
tile: Tile, tile: Tile,
feature: Feature, feature: Feature,
@ -199,7 +198,7 @@ class Renderer {
scale: layer.scale, scale: layer.scale,
}); });
} else { } else {
this._drawFeature(tile, feature, layer.scale); this._drawFeature(tile, feature, layer.scale, seen);
} }
} }
} }
@ -210,7 +209,7 @@ class Renderer {
}); });
for (const label of labels) { for (const label of labels) {
this._drawFeature(label.tile, label.feature, label.scale); this._drawFeature(label.tile, label.feature, label.scale, seen);
} }
} }
@ -231,7 +230,7 @@ class Renderer {
// return this.labelBuffer.featuresAt(x, y); // return this.labelBuffer.featuresAt(x, y);
// } // }
private _drawFeature(tile: Tile, feature, scale: number): boolean { private _drawFeature(tile: Tile, feature, scale: number, seen: Set<string>): boolean {
if (!this.canvas) { if (!this.canvas) {
return false; return false;
} }
@ -266,7 +265,7 @@ class Renderer {
const genericSymbol = config.poiMarker; const genericSymbol = config.poiMarker;
const text = feature.label || config.poiMarker; const text = feature.label || config.poiMarker;
if (this._seen[text] && !genericSymbol) { if (seen.has(text) && !genericSymbol) {
return false; return false;
} }
@ -290,7 +289,7 @@ class Renderer {
} }
} }
if (placed) { if (placed) {
this._seen[text] = true; seen.add(text);
} }
break; break;
} }