31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1 | #![allow(dead_code)] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 2 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 3 | use std::time::Duration; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 4 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 5 | use macltok::{ClientHandle, Event, EventStream, TimelineDiff, TimelineItem, TimelineMessage}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 6 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 7 | pub async fn receive_until(events: &mut EventStream, predicate: impl Fn(&Event) -> bool) -> Event { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 8 | tokio::time::timeout(Duration::from_secs(5), async { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 9 | while let Some(event) = events.recv().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 10 | if predicate(&event) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 11 | return event; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 12 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 13 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 14 | panic!("event stream closed before the expected event"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 15 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 16 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 17 | .expect("expected event did not arrive") |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 18 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 19 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 20 | /// asks for a shutdown while still draining, since the engine won't answer until its events are out |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 21 | pub async fn shut_down( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 22 | handle: &ClientHandle, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 23 | events: &mut EventStream, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 24 | running: tokio::task::JoinHandle<Result<(), macltok::EngineError>>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 25 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 26 | let (answer, _) = tokio::join!(handle.shutdown(), async { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 27 | receive_until(events, |event| matches!(event, Event::Stopped)).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 28 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 29 | answer.unwrap(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 30 | running.await.unwrap().unwrap(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 31 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 32 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 33 | pub fn event_messages(event: &Event) -> Vec<&TimelineMessage> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 34 | let items = match event { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 35 | Event::TimelineReset { items, .. } => items.iter().collect::<Vec<_>>(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 36 | Event::TimelineChanged { diffs, .. } => diffs.iter().flat_map(diff_items).collect(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 37 | _ => Vec::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 38 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 39 | items |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 40 | .into_iter() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 41 | .filter_map(|item| match item { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 42 | TimelineItem::Message(message) => Some(message.as_ref()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 43 | _ => None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 44 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 45 | .collect() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 46 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 47 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 48 | pub fn diff_items(diff: &TimelineDiff) -> Vec<&TimelineItem> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 49 | match diff { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 50 | TimelineDiff::Append { values } | TimelineDiff::Reset { values } => values.iter().collect(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 51 | TimelineDiff::PushFront { value } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 52 | | TimelineDiff::PushBack { value } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 53 | | TimelineDiff::Insert { value, .. } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 54 | | TimelineDiff::Set { value, .. } => vec![value], |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 55 | _ => Vec::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 56 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 57 | } |