From 546808a03db010ffc0376b2d62b5955b7316db5f Mon Sep 17 00:00:00 2001 From: bflatastic Date: Wed, 18 Nov 2026 14:11:11 +0700 Subject: [PATCH] M9: the browser hands each team library over as it comes, not after the whole check exchange::libraries kept the fetches' answers until every job had run, and Driver::follow handed them over only at Next::Done, so a library that had answered waited behind later libraries and reports that connected or timed out. A new Next::Tell gives the UI its events, then goes on; libraries ends each fetch with it, before connecting for the next job, as the desktop's library_background does. Test: exchange_tests::a_library_check_hands_each_fetch_over_as_it_comes (the first library is in the inbox while a later report's server never answers). --- crates/collab-net/src/driver.rs | 6 +++++ crates/collab-net/src/exchange.rs | 35 +++++++++++-------------- crates/collab-net/src/exchange_tests.rs | 29 +++++++++++++++++++- crates/collab-net/src/web.rs | 2 +- 4 files changed, 50 insertions(+), 22 deletions(-) diff --git a/crates/collab-net/src/driver.rs b/crates/collab-net/src/driver.rs index e55eba957..526cf0941 100644 --- a/crates/collab-net/src/driver.rs +++ b/crates/collab-net/src/driver.rs @@ -152,6 +152,12 @@ impl Driver { None => self.done = true, } } + exchange::Next::Tell(events, then) => { + for e in events { + self.inbox.push(e); + } + self.follow(*then, now); + } } } diff --git a/crates/collab-net/src/exchange.rs b/crates/collab-net/src/exchange.rs index 89e2225b6..5a75e382c 100644 --- a/crates/collab-net/src/exchange.rs +++ b/crates/collab-net/src/exchange.rs @@ -55,6 +55,8 @@ pub(crate) enum Next { Ask(Box, Then), /// Done: tell the UI these, then join the file this invite names (`None`: nothing to join). Done(Vec, Option), + /// Tell the UI these now, then go on with what follows. + Tell(Vec, Box), } impl Exchange { @@ -248,30 +250,23 @@ fn library_reply(answers: Result) -> Result, mut events: Vec, fetching: Arc) -> Next { +/// own. A fetch's answer goes to the UI as it comes, before the next connection, as the desktop's +/// does, so a slow or silent server further on holds no library back; the others' answers are left +/// out (a report's says nothing, and an older server refuses it). `fetching` goes down at the end. +pub(crate) fn libraries(mut jobs: VecDeque<(String, LibraryReq)>, fetching: Arc) -> Next { let Some((link, req)) = jobs.pop_front() else { fetching.store(false, Ordering::SeqCst); - return Next::Done(events, None); + return Next::Done(Vec::new(), None); }; let fetch = matches!(req, LibraryReq::Fetch { .. }); - match Invite::parse(&link) { - Err(reply) => { - if fetch { - events.push(Event::Library { link, reply: Err(reply) }); - } - libraries(jobs, events, fetching) - } - Ok(invite) => Next::Ask( - Box::new(Exchange::library(invite, req)), - Box::new(move |answers| { - if fetch { - events.push(Event::Library { link, reply: library_reply(answers) }); - } - libraries(jobs, events, fetching) - }), - ), + let invite = Invite::parse(&link); + let then = move |reply: Result| { + let rest = libraries(jobs, fetching); + if fetch { Next::Tell(vec![Event::Library { link, reply }], Box::new(rest)) } else { rest } + }; + match invite { + Err(e) => then(Err(e)), + Ok(invite) => Next::Ask(Box::new(Exchange::library(invite, req)), Box::new(move |answers| then(library_reply(answers)))), } } diff --git a/crates/collab-net/src/exchange_tests.rs b/crates/collab-net/src/exchange_tests.rs index 5e58d8376..ecebdc8f7 100644 --- a/crates/collab-net/src/exchange_tests.rs +++ b/crates/collab-net/src/exchange_tests.rs @@ -411,7 +411,7 @@ fn a_library_check_runs_in_order_and_answers_only_its_fetches() { ]; let fetching = Arc::new(AtomicBool::new(true)); let t0 = Instant::now(); - let (mut d, inbox) = run(exchange::libraries(jobs.into(), Vec::new(), fetching.clone()), t0); + let (mut d, inbox) = run(exchange::libraries(jobs.into(), fetching.clone()), t0); let ready = library(LibraryReply::Ready { access: Access::View }); d.opened(t0); say(&mut d, &ready, t0); @@ -429,3 +429,30 @@ fn a_library_check_runs_in_order_and_answers_only_its_fetches() { assert!(replies[0].is_err()); assert!(matches!(replies[1], Ok(LibraryReply::Empty))); } + +/// A fetch's answer reaches the UI as it comes, not at the end of the check: a report after it +/// whose server never answers holds the first library back no longer than its own connection. +#[test] +fn a_library_check_hands_each_fetch_over_as_it_comes() { + let fetch = LibraryReq::Fetch { have: None, usage: None }; + let jobs = vec![(link(), fetch.clone()), (link(), LibraryReq::ReportDetached(Detached::default())), (link(), fetch)]; + let fetching = Arc::new(AtomicBool::new(true)); + let t0 = Instant::now(); + let (mut d, inbox) = run(exchange::libraries(jobs.into(), fetching.clone()), t0); + let fetched = |events: &[Event]| events.iter().filter(|e| matches!(e, Event::Library { .. })).count(); + let ready = library(LibraryReply::Ready { access: Access::View }); + d.opened(t0); + say(&mut d, &ready, t0); + say(&mut d, &library(LibraryReply::Empty), t0); + assert_eq!(fetched(&inbox.drain()), 1, "the first library is in while the report's server is asked"); + // The report's connection never opens: the check gives up on it and goes on, saying nothing. + let t1 = t0 + Duration::from_secs(5); + d.tick(t1); + assert_eq!(fetched(&inbox.drain()), 0); + assert!(fetching.load(Ordering::SeqCst), "the check is still running"); + d.opened(t1); + say(&mut d, &ready, t1); + say(&mut d, &library(LibraryReply::Empty), t1); + assert_eq!(fetched(&inbox.drain()), 1); + assert!(!fetching.load(Ordering::SeqCst) && d.done()); +} diff --git a/crates/collab-net/src/web.rs b/crates/collab-net/src/web.rs index 404ca13c7..602665dc6 100644 --- a/crates/collab-net/src/web.rs +++ b/crates/collab-net/src/web.rs @@ -247,7 +247,7 @@ impl LiveLink for NetLink { fn library_background(&mut self, jobs: Vec<(String, LibraryReq)>) { if !jobs.is_empty() && !self.fetching.swap(true, Ordering::SeqCst) { - self.ask(exchange::libraries(jobs.into(), Vec::new(), self.fetching.clone())); + self.ask(exchange::libraries(jobs.into(), self.fetching.clone())); } }