Skip to content

Commit

Permalink
feat(front): cache psap queries
Browse files Browse the repository at this point in the history
  • Loading branch information
stdavis committed Nov 1, 2023
1 parent 800a0fe commit 1d7028b
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/front/services/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ export function useFollowUser(mapViewRef) {
return { followUser, stopFollowUser, isFollowing: !!subscription };
}

const cache = {};

export async function getAssistancePrompt() {
const existingPermissions = await Location.getForegroundPermissionsAsync();

Expand All @@ -137,12 +139,22 @@ export async function getAssistancePrompt() {
return prompt;
}

let currentLocation = await Location.getLastKnownPositionAsync();
let currentLocation = await Location.getLastKnownPositionAsync({
maxAge: 1000 * 60 * 10, // minutes
});

if (!currentLocation) {
currentLocation = await getLocation();
}

const cacheKey = `${currentLocation.coords.latitude.toFixed(
2,
)},${currentLocation.coords.longitude.toFixed(2)}`;

if (cache[cacheKey]) {
return cache[cacheKey];
}

try {
const response = await myFetch(
`${config.URLS.PSAP_FEATURE_SERVICE}/query`,
Expand All @@ -168,6 +180,8 @@ export async function getAssistancePrompt() {
const attributes = response.features[0].attributes;

prompt = `If you encounter a live animal that needs assistance, please call ${attributes.DsplayName} at ${attributes.PHONE_NUMBER}.`;

cache[cacheKey] = prompt;
}
} catch (error) {
console.error(error);
Expand Down

0 comments on commit 1d7028b

Please sign in to comment.