fix(rotator): the compass shows the heading it already knows

Opening Station Control left the compass blank for a second or two while the
amplifiers, relay boards and power supply filled at once.

Nothing was slow. The status bar keeps the shared heading loop running, so when
the compass mounts there is already a tick pending — up to the full three-second
idle interval away — and the new subscriber simply waited out the rest of it.
The heading was known the whole time and had nowhere to be read from.

The last heading is now kept and handed to whoever subscribes next. That matters
more on the Alpha SPID this was reported on than it would elsewhere: every poll
is an open, a read at 600 baud and a close, so even fetching immediately on
mount would not have been immediate.

In a microtask, so a subscriber is never called back before subscribeRotorHeading
has returned to it.
This commit is contained in:
2026-08-17 10:32:06 +02:00
parent 21a0d560de
commit e5c9ca1a7d
2 changed files with 19 additions and 2 deletions
+15
View File
@@ -36,6 +36,14 @@ let timer: number | undefined;
let inFlight = false;
let lastAz: number | null = null;
let lastMoveAt = 0;
// The last heading anyone received, replayed to whoever subscribes next.
//
// Without it, opening Station Control left the compass blank for one or two
// seconds while every other panel filled at once. Nothing was slow: the status
// bar already had the loop running with a tick pending, so a component mounting
// halfway through an idle interval simply waited out the rest of it. The
// heading was known the whole time — it just had nowhere to be read from.
let last: RotorHeading | null = null;
function schedule(delay: number) {
if (timer !== undefined) window.clearTimeout(timer);
@@ -54,6 +62,7 @@ async function tick() {
if (lastAz !== null && h.azimuth !== lastAz) lastMoveAt = Date.now();
lastAz = h.azimuth;
}
last = h;
subs.forEach((fn) => { try { fn(h); } catch { /* a subscriber must not stop the loop */ } });
} catch {
// Leave the last heading alone: a single failed poll on a shared serial port
@@ -69,6 +78,12 @@ async function tick() {
// unsubscribe. The loop stops when the last subscriber leaves.
export function subscribeRotorHeading(fn: (h: RotorHeading) => void): () => void {
subs.add(fn);
// Hand over what is already known, at once. An Alpha SPID poll is an open,
// a read at 600 baud and a close, so even an immediate one takes a moment —
// the cached heading is what makes the compass appear with the panel rather
// than after it. In a microtask, so a subscriber is never called back before
// subscribeRotorHeading has returned to it.
if (last) { const h = last; queueMicrotask(() => { if (subs.has(fn)) fn(h); }); }
if (timer === undefined && !inFlight) void tick();
return () => {
subs.delete(fn);