Files
figcraft/crates/collab-net/src/throttle.rs
T
BFlatandClaude Opus 5.5 435415770f M9: live sessions in the browser; join links and dialog polish (#106)
- Browser client (step 4): the web build joins sessions a desktop app hosts
  through the browser's WebSocket API (web-sys, binary frames, the same
  message encoding). Its connection logic is a target-independent state
  machine (collab-net driver.rs): hello on open, changes and presence held
  until the welcome, throttled presence, keep-alive pings, a silent host
  counted as dropped, reconnect with backoff, final refusals. Tested natively
  against a scripted socket and a real host over loopback. Hosting stays
  desktop-only (LiveLink::can_host; collab.peers canHost). https pages are
  told up front that they can only reach ws:// sessions on this computer.
- Shared join logic (join.rs: GuestConfig, outcomes, taking in host messages)
  and transport limits (limits.rs) used by the native guest and the browser.
- Invite links: `figcraft figcraft://join?...` joins on start (a bad link
  opens the Join dialog with it); file:// arguments are paths. Windows
  registers a per-user figcraft:// opener (HKCU\Software\Classes\figcraft)
  behind a Registry trait (tests use a fake): collab.registerLinks {on?} and a
  Preferences checkbox, default on for installed builds, never for builds
  from source. macOS Info.plist declares CFBundleURLTypes; the Linux desktop
  entry declares x-scheme-handler/figcraft and passes %U.
- Join dialog: Escape closes it and no longer reaches the canvas (selection
  kept); its field has the keyboard when it opens.
- Docs: collab-protocol (browser client, links, mixed content), mcp,
  control-protocol, README, web hosting notes, collaboration wording in the
  progress docs (numbers unchanged).

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 09:10:59 +07:00

56 lines
1.8 KiB
Rust

//! Latest-wins throttling: at most one value per interval goes out; a value offered sooner waits
//! (replacing any older waiting value) until the interval has passed, so the last one is never
//! lost. Used for presence on both ends (about 30 updates a second).
use std::time::Duration;
// `std::time::Instant` on native builds; the browser's clock in the web build.
use web_time::Instant;
/// How often presence goes out at most.
pub const PRESENCE_INTERVAL: Duration = Duration::from_millis(33);
#[derive(Debug)]
pub struct Throttle<T> {
interval: Duration,
last: Option<Instant>,
pending: Option<T>,
}
impl<T> Throttle<T> {
pub fn new(interval: Duration) -> Self {
Self { interval, last: None, pending: None }
}
/// Offer `v` at `now`: returns it when it may go out now, else keeps it for [`take_due`](Self::take_due).
pub fn offer(&mut self, v: T, now: Instant) -> Option<T> {
if self.last.is_some_and(|l| now.saturating_duration_since(l) < self.interval) {
self.pending = Some(v);
return None;
}
self.last = Some(now);
self.pending = None;
Some(v)
}
/// When the waiting value may go out (none waiting: `None`).
pub fn due(&self) -> Option<Instant> {
self.pending.as_ref()?;
Some(self.last.map_or_else(Instant::now, |l| l + self.interval))
}
/// The waiting value, once its time has come.
pub fn take_due(&mut self, now: Instant) -> Option<T> {
if self.due().is_some_and(|d| d <= now) {
self.last = Some(now);
return self.pending.take();
}
None
}
}
/// The earliest of some deadlines.
pub fn earliest(times: impl IntoIterator<Item = Option<Instant>>) -> Option<Instant> {
times.into_iter().flatten().min()
}