Skip to content

Commit

Permalink
Cache gmap tile fetches
Browse files Browse the repository at this point in the history
Don't want to go broke
  • Loading branch information
mayfield committed Apr 22, 2024
1 parent 298d30b commit 38ba946
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/stats.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ export class StatsProcessor extends events.EventEmitter {
console.error("Custom power zones are invalid:", e);
this._customPowerZones = null;
}
this._gmapTileCache = new Map();
rpc.register(this.getPowerZones, {scope: this});
rpc.register(this.updateAthlete, {scope: this});
rpc.register(this.startLap, {scope: this});
Expand Down Expand Up @@ -736,6 +737,10 @@ export class StatsProcessor extends events.EventEmitter {
}

async getIRLMapTile(x, y, z) {
const sig = [x,y,z].join();
if (this._gmapTileCache.has(sig)) {
return this._gmapTileCache.get(sig);
}
const key = this._googleMapTileKey;
if (!key) {
throw new Error("google map tile key required");
Expand Down Expand Up @@ -773,11 +778,13 @@ export class StatsProcessor extends events.EventEmitter {
console.error("Failed to get google map tile:", resp.status, await resp.text());
throw new Error("Map Tile Error");
}
return {
const entry = {
contentType: resp.headers.get('content-type'),
encoding: 'base64',
data: Buffer.from(await resp.arrayBuffer()).toString('base64'),
};
this._gmapTileCache.set(sig, entry);
return entry;
}

getPowerZones(ftp) {
Expand Down

0 comments on commit 38ba946

Please sign in to comment.