Skip to content

Commit 496d907

Browse files
committed
Add caching of the tiles
1 parent e51a3b9 commit 496d907

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/georaster-layer-for-leaflet.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,12 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
5757
updateWhenZooming: false,
5858
keepBuffer: 25,
5959
resolution: 2 ** 5,
60-
debugLevel: 0
60+
debugLevel: 0,
61+
caching: true
6162
},
6263

64+
cache: {},
65+
6366
initialize: function (options: GeoRasterLayerOptions) {
6467
try {
6568
if (options.georasters) {
@@ -321,7 +324,23 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
321324
// note that we aren't setting the tile height or width here
322325
// drawTile dynamically sets the width and padding based on
323326
// how much the georaster takes up the tile area
324-
this.drawTile({ tile, coords, context, done });
327+
const coordsKey = this._tileCoordsToKey(coords);
328+
const key = `${coordsKey}:${this.options.resolution}`;
329+
const doneCb = (error?: Error, tile?: HTMLElement): void => {
330+
done(error, tile);
331+
332+
// caching the rendered tile, to skip the calculation for the next time
333+
if (!error && this.options.caching) {
334+
this.cache[key] = tile;
335+
}
336+
};
337+
338+
if (this.options.caching && this.cache[key]) {
339+
done(undefined, this.cache[key]);
340+
return this.cache[key];
341+
} else {
342+
this.drawTile({ tile, coords, context, done: doneCb });
343+
}
325344

326345
return tile;
327346
},
@@ -1060,6 +1079,10 @@ const GeoRasterLayer: (new (options: GeoRasterLayerOptions) => any) & typeof L.C
10601079

10611080
same(array: GeoRaster[], key: GeoRasterKeys) {
10621081
return new Set(array.map(item => item[key])).size === 1;
1082+
},
1083+
1084+
clearCache() {
1085+
this.cache = {};
10631086
}
10641087
});
10651088

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ interface GeoRasterLayerOptions_CommonOptions extends GridLayerOptions {
3030
updateWhenIdle?: boolean; // inherited from LeafletJS
3131
updateWhenZooming?: boolean; // inherited from LeafletJS
3232
keepBuffer?: number; // inherited from LeafletJS
33+
caching?: boolean;
3334
}
3435

3536
// Ensures at least one of the georaster[s] options is defined while being ok the other is not

0 commit comments

Comments
 (0)