Skip to content

Commit

Permalink
Move tile logic outside calloutAnnouncer
Browse files Browse the repository at this point in the history
  • Loading branch information
steinbro committed Dec 28, 2023
1 parent 899ae43 commit 596b6b0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
11 changes: 1 addition & 10 deletions app/callout.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
import { enumerateTilesAround } from './tile.js'

export function createCalloutAnnouncer(audioQueue, proximityThresholdMeters) {
// Avoid a flood of network requests, by maintaining a list of tiles already requested
// (handles the case where the network request is already open)
const seenTiles = new Set();

// Avoid repeating myself, by maintaining a list of the most recent POIs announced
const spokenRecently = {
keys: new Set(), // for quick lookups
Expand Down Expand Up @@ -85,12 +81,7 @@ export function createCalloutAnnouncer(audioQueue, proximityThresholdMeters) {
audioQueue.updateLocation(myLocation, heading);

for (const tile of tiles) {
//FIXME move tile logic outside of calloutAnnouncer
if (!seenTiles.has(tile.key)) {
seenTiles.add(tile.key);
tile.load();
}

tile.load();
tile.getFeatures()
.then(features => {
features.forEach(feature => {
Expand Down
13 changes: 12 additions & 1 deletion app/tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@ import { createBoundingBox, enumerateTilesInBoundingBox } from './geospatial.js'
const maxAge = 604800000; // 1 week, in ms
export const zoomLevel = 16;

// Track tiles that don't need to be re-requested at the moment
const tilesInProgressOrDone = new Set();

function createTile(x, y, z) {
var tile = {
x: x,
y: y,
z: z,
key: `${z}/${x}/${y}`,

load: function() {
load: async function() {
if (tilesInProgressOrDone.has(tile.key)) {
// no need to request again
return;
}
tilesInProgressOrDone.add(tile.key);

const urlToFetch = `${config.tileServer}/${tile.key}.json`;
fetchUrlIfNotCached(urlToFetch, maxAge)
.then((data) => {
Expand All @@ -26,6 +35,8 @@ function createTile(x, y, z) {
})
.catch((error) => {
console.error(error);
// should be retried when next needed
tilesInProgressOrDone.delete(tile.key);
});
},

Expand Down

0 comments on commit 596b6b0

Please sign in to comment.