mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 02:36:54 +01:00
Merge look-fix-request-drain framework work (a waiting request asks for the next frame)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
197ff5ae6b
2 files changed
+42
-3
No files matched your search
@@ -171,6 +171,8 @@ pub fn result_reply(r: Result<Value, String>) -> ControlResponse {
|
||||
#[non_exhaustive]
|
||||
pub struct ControlChannel {
|
||||
rx: Receiver<ControlRequest>,
|
||||
/// The request [`poll_input`](Self::poll_input) took from `rx` to see that one waits; it runs first.
|
||||
held: Option<ControlRequest>,
|
||||
input_waiters: Vec<Sender<ControlResponse>>,
|
||||
job_waiters: Vec<(JobId, Sender<ControlResponse>)>,
|
||||
tree_waiters: Vec<Sender<ControlResponse>>,
|
||||
@@ -181,14 +183,14 @@ pub struct ControlChannel {
|
||||
impl ControlChannel {
|
||||
/// Attach a transport's receiver (TCP, stdin, tests).
|
||||
pub fn new(rx: Receiver<ControlRequest>) -> Self {
|
||||
Self { rx, input_waiters: Vec::new(), job_waiters: Vec::new(), tree_waiters: Vec::new(), captures: Captures::default() }
|
||||
Self { rx, held: None, input_waiters: Vec::new(), job_waiters: Vec::new(), tree_waiters: Vec::new(), captures: Captures::default() }
|
||||
}
|
||||
|
||||
/// The next request to run, claimed through [`Dispatch::start`]. Expired or abandoned
|
||||
/// requests are replied with [`expired_error`] here and skipped. `None` when the queue is
|
||||
/// empty this frame.
|
||||
pub fn next_request(&mut self) -> Option<ControlRequest> {
|
||||
while let Ok(req) = self.rx.try_recv() {
|
||||
while let Some(req) = self.held.take().or_else(|| self.rx.try_recv().ok()) {
|
||||
if req.deadline.is_some_and(|deadline| motion::now() >= deadline) || !req.dispatch.start() {
|
||||
let _ = req.reply.send(json!({"ok": false, "error": expired_error()}));
|
||||
continue;
|
||||
@@ -244,9 +246,17 @@ impl ControlChannel {
|
||||
/// once nothing is queued or in flight. Call it once per pass, at the end of the pass (after its
|
||||
/// UI ran): the step that pass consumed counts as run, so the reply goes out with that pass
|
||||
/// ([`WindowHost::injected_idle_at_pass_end`]). Called between passes, it waits for the pass. It also has the host
|
||||
/// keep its access trees, which `control` targets and `ui.find` read.
|
||||
/// keep its access trees, which `control` targets and `ui.find` read, and asks for the main window's next frame
|
||||
/// while a request waits: the transport's wake from its own thread names the pass it was sent in, and eframe drops
|
||||
/// a wake more than one pass old, so a request sent before a frame that runs twice would wait for other input.
|
||||
pub fn poll_input(&mut self, host: &WindowHost) {
|
||||
host.keep_access_trees();
|
||||
if self.held.is_none() {
|
||||
self.held = self.rx.try_recv().ok();
|
||||
}
|
||||
if self.held.is_some() {
|
||||
host.repaint_in(egui::ViewportId::ROOT, -1.0);
|
||||
}
|
||||
let gone = host.take_injected_gone();
|
||||
if host.take_injected_withdrawn() {
|
||||
self.input_cancelled(&gone.map_or_else(|| TARGET_CHANGED.to_owned(), |node| crate::synthetic::gone(&node)));
|
||||
|
||||
@@ -65,6 +65,35 @@ fn a_request_without_a_deadline_is_dispatched_after_its_reply_receiver_is_droppe
|
||||
assert!(channel.next_request().is_none(), "the request runs once");
|
||||
}
|
||||
|
||||
/// A request that arrives after the app took its requests for a frame asks for the next frame, also when the frame
|
||||
/// runs twice: the transport's own wake, sent before the frame, is spent on its first pass.
|
||||
#[trial]
|
||||
fn a_request_left_waiting_at_a_frame_end_asks_for_the_next_frame() {
|
||||
let ctx = Context::default();
|
||||
let (tx, rx) = channel();
|
||||
let mut channel = ControlChannel::new(rx);
|
||||
let host = WindowHost::of(&ctx);
|
||||
let wakes = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
|
||||
let sink = wakes.clone();
|
||||
ctx.set_request_repaint_callback(move |info| sink.lock().expect("wakes").push(info.current_cumulative_pass_nr));
|
||||
let frame = |ui: &mut dyn FnMut(&mut egui::Ui)| ctx.run_ui(egui::RawInput::default(), ui).textures_delta.clear();
|
||||
frame(&mut |_| {});
|
||||
frame(&mut |_| {});
|
||||
let (request, _reply) = ControlRequest::new("engine.execute", json!({}));
|
||||
tx.send(request).expect("sent");
|
||||
ctx.request_repaint();
|
||||
frame(&mut |ui| {
|
||||
if ui.ctx().current_pass_index() == 0 {
|
||||
ui.ctx().request_discard("a layout change");
|
||||
}
|
||||
channel.poll_input(&host);
|
||||
});
|
||||
// eframe keeps a wake only while the pass number it names is at most one pass old.
|
||||
let now = ctx.cumulative_pass_nr();
|
||||
assert!(wakes.lock().expect("wakes").iter().any(|nr| now <= nr + 1), "the frame asks for the next one");
|
||||
assert_eq!(channel.next_request().map(|request| request.method), Some("engine.execute".to_owned()), "the request still runs");
|
||||
}
|
||||
|
||||
/// A job's reply goes to the requests waiting on that job only, as the app built it (an error with partial results
|
||||
/// keeps both); the other job's waiter keeps waiting.
|
||||
#[trial]
|
||||
|
||||
Reference in new issue
Block a user