API
Usage and API reference
The alarm scheduler runs callbacks at a future time using a single timer and a priority queue, so hundreds of device timeouts cost one `setTimeout`. When a background tab becomes visible again, alarms that came due are fired and the timer is re-armed.
Copy-paste examples
Each snippet is a complete, working example you can paste into a Svelte 5 component.
Mark a device offline after its timeout
<script lang="ts">
import { useCwAlarm } from '@cropwatchdevelopment/cwui';
let { device } = $props();
let offline = $state(false);
const alarm = useCwAlarm();
$effect(() => {
offline = false;
const id = alarm.schedule({
id: `device-${device.id}`,
from: device.lastSeen,
alarmAfterMinutes: device.uploadIntervalMinutes * 2,
callback: () => (offline = true)
});
return () => alarm.cancel(id);
});
</script>