Don't crash page if chandler layer can't load

This commit is contained in:
Chandler Swift 2026-03-17 18:11:52 -05:00
parent 69e777033e
commit 5abb9ae18b
Signed by: chandlerswift
GPG key ID: A851D929D52FB93F

View file

@ -5,24 +5,15 @@ import {Style} from 'ol/style.js';
import Icon from 'ol/style/Icon.js';
import pinURL from '/layers/chandler/pin.svg?url'; // TODO: remove `?url`?
import { Feature } from 'ol';
import { Collection, Feature } from 'ol';
import { Point } from 'ol/geom';
import { fromLonLat } from 'ol/proj';
const res = await fetch("https://whereis.chandlerswift.com/api/0/last");
const locs = await res.json();
const loc = locs[0];
let feature = new Feature({
geometry: new Point(fromLonLat([loc.lon, loc.lat])),
...loc
});
let features = new Collection();
const vectorLayer = new VectorLayer({
source: new VectorSource({
features: [
feature,
]
features,
}),
style: new Style({
image: new Icon({
@ -32,12 +23,23 @@ const vectorLayer = new VectorLayer({
}),
});
setInterval(async function(){
async function refresh(){
const res = await fetch("https://whereis.chandlerswift.com/api/0/last");
const locs = await res.json();
const loc = locs[0];
feature.setProperties(loc); // TODO: this won't remove a property if it was in a previous response but not this one
feature.getGeometry().setCoordinates(fromLonLat([loc.lon, loc.lat]));
}, 10 * 1000);
// TODO: I could probably just `features[0] = …` but I'm not sure if that causes problems with re-rendering features
if (features.getLength() == 0) {
features.push(new Feature({
geometry: new Point(fromLonLat([loc.lon, loc.lat])),
...loc
}));
} else {
features.item(0).setProperties(loc); // TODO: this won't remove a property if it was in a previous response but not this one
features.item(0).getGeometry().setCoordinates(fromLonLat([loc.lon, loc.lat]));
}
}
refresh();
setInterval(refresh, 10 * 1000);
export default vectorLayer;