Back to Pursuit PIT
SOURCE / PINNED RELEASE

Made of little things.

Pursuit PIT

Release
565f4981bdc3…
Author-recorded commit
197afdbed44b…
License
LICENSE
Author’s source reference
nostr://npub12lm3rxsva8kfempyjkxupt09e02n3fezurkyadyqfejdc5e0r2kscqty6r/wss%3A%2F%2Fgit.napplet.soy%2F/n-951414e7fd7

Archive hash verified: 3b0485589942fd5c…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

src/napplet-settings.ts
import { config } from '@napplet/sdk';
import { runtimeHasDomain } from './domain-availability.js';
import settingsSchema from '../config.schema.json';

export interface Settings {
  units: 'mph' | 'kmh';
  mirror: boolean;
  traffic: 'light' | 'normal' | 'heavy';
  volume: number; // 0..100
  radio: boolean;
}

// Defaults match config.schema.json; they are used as-is when the host has no config domain.
export const settings: Settings = { units: 'mph', mirror: true, traffic: 'normal', volume: 70, radio: true };

export function watchSettings(onChange: () => void): void {
  if (!runtimeHasDomain('config')) return;
  void (async () => {
    // Some hosts consume the embedded schema; others need standard registration.
    // Do not replace a schema that the host already supplied.
    if (!config.schema) {
      await config.registerSchema(settingsSchema as Parameters<typeof config.registerSchema>[0]);
    }
    const subscription = config.subscribe((values) => {
      settings.units = values.units === 'kmh' ? 'kmh' : 'mph';
      settings.mirror = values.mirror !== false;
      settings.traffic = values.traffic === 'light' || values.traffic === 'heavy' ? values.traffic : 'normal';
      settings.volume = typeof values.volume === 'number' ? Math.max(0, Math.min(100, values.volume)) : 70;
      settings.radio = values.radio !== false;
      onChange();
    });
    window.addEventListener('pagehide', () => subscription.close(), { once: true });
  })().catch(() => {
    // Optional operations may be unavailable even when a host exposes the domain; keep defaults.
  });
}