feat: reactive watch API (watch* subscriptions) - #3
Merged
Conversation
Add a reactive watch API so consumers can subscribe to a kind of device information and receive a Tauri event whenever it changes, instead of polling a getter on a timer. - New `start_watching`/`stop_watching` commands (with matching permissions) backed by a reference-counted per-kind monitor registry. - Native, event-driven monitors on macOS: battery (IOKit), display (Core Graphics), and network (SystemConfiguration) — CPU stays idle between changes. Storage and device fall back to polling. - Change-detecting polling fallback with per-kind default/min intervals so expensive getters (e.g. `device` via system_profiler) are not polled aggressively; sleeps on a condvar and wakes immediately on stop. - macOS run-loop teardown is race-free (stop is signalled through the run loop itself) and stop() no longer holds the shared lock while joining; OS callbacks contain panics so they cannot unwind across the C ABI. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Expose the reactive watch API to JS: watchBattery, watchNetwork, watchStorage, watchDisplay, and watchDevice. Each listens on the per-kind change event, starts the backend monitor, and returns an async unwatch that stops it and removes the listener. If starting the backend fails, the event listener is removed so it does not leak. Add and export the WatchOptions (intervalMs) and UnwatchFn types. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Use watchBattery/watchNetwork/watchDisplay instead of a manual setInterval loop. The charging overlay now fires only on a real false → true transition (not on the initial value), and all watchers are unsubscribed on destroy. A failed start is surfaced as an error rather than left as an unhandled promise rejection. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Add a dedicated Reactive Watch API reference page (functions, options, native-vs-polling behavior, per-kind intervals, permissions) and wire it into the sidebar and API overview. Update the README, getting-started guide, examples, and home page to cover watch* alongside the getters, and replace the manual setInterval example with a watch-based one. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Minor bump for the backwards-compatible reactive watch API. Add a dev-dependency on tauri's test feature for the watcher lifecycle tests, and introduce CHANGELOG.md documenting the 1.1.0 release. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Fixes the `cargo fmt -- --check` CI failure (comment indentation in the reference-counting test). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The mock-runtime test binary crashed on load on windows-latest with STATUS_ENTRYPOINT_NOT_FOUND (0xc0000139) before any test ran. Enable Tauri's `test` feature only off Windows (target-gated dev-dependency) and cfg-gate the three mock-based watcher tests to match. The pure-logic tests still run on every platform. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a reactive watch API so consumers can subscribe to a kind of device
information and get a callback whenever the value changes, instead of polling a
getter on a timer. The current value is delivered immediately, then on every
change.
New functions:
watchBattery,watchNetwork,watchStorage,watchDisplay,watchDevice— each returns aPromise<UnwatchFn>. New exported types:WatchOptions(intervalMs) andUnwatchFn.How it works
The public API and event names are identical across platforms; the engine per
kind differs:
network (SystemConfiguration). CPU stays idle between changes; updates arrive
the instant the OS reports them.
(
storageanddeviceeverywhere; all kinds off macOS). Emits only on a realchange.
Monitors are reference-counted: one per kind, started on the first
subscriber and torn down on the last.
Performance
device60s (min 10s),storage10s (min 1s), others 2s (min 250ms).deviceshells out tosystem_profileron macOS and rarely changes, so it'sno longer read every couple seconds.
(no 100ms busy-wake), so the CPU can idle.
Correctness / safety
These came out of a self-review of the new backend code:
loop via a
CFRunLoopSource, so a stop that races startup can't be lost(previously
CFRunLoopStopbeforeCFRunLoopRuncould hangstop_watching).stop_watchingno longer holds the shared watcher lock while joining themonitor thread, so a slow teardown can't block other kinds' subscribe/stop.
abort the process.
watch()removes its event listener if the backend fails to start; theexample surfaces a failed start instead of leaving an unhandled rejection.
Testing
cargo test— added watcher lifecycle tests (spawn → emit → stop → join → free)for every kind, including the native macOS paths; ran the start/stop test in a
loop to confirm no teardown hang.
cargo clippyclean.Docs & versioning
CHANGELOG.md.Commits
Split into reviewable layers: backend watcher · JS bindings · example app ·
docs · release/changelog.
🤖 Generated with Claude Code