From 5abb9ae18b1b1c0f5e7fade996884976a920884d Mon Sep 17 00:00:00 2001 From: Chandler Swift Date: Tue, 17 Mar 2026 18:11:52 -0500 Subject: [PATCH] Don't crash page if chandler layer can't load --- layers/chandler/layer.js | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/layers/chandler/layer.js b/layers/chandler/layer.js index 85ac3f1..6848f36 100644 --- a/layers/chandler/layer.js +++ b/layers/chandler/layer.js @@ -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;