anathema_runtime/runtime/
testing.rs1use std::time::{Duration, Instant};
2
3use anathema_backend::Backend;
4use anathema_state::{Watched, Watcher, drain_watchers};
5use anathema_store::stack::Stack;
6use anathema_widgets::query::Children;
7
8use crate::Frame;
9use crate::error::Result;
10use crate::events::GlobalEventHandler;
11
12impl<'bp, G> Frame<'_, 'bp, G>
17where
18 G: GlobalEventHandler,
19{
20 pub fn components(&mut self) -> anathema_widgets::query::Components<'_, '_, 'bp> {
21 panic!()
22 }
28
29 pub fn elements(&mut self) -> Children<'_, 'bp> {
30 Children::new(
31 self.tree.view_mut(),
32 self.layout_ctx.attribute_storage,
33 &mut self.needs_layout,
34 )
35 }
36
37 pub fn wait_for_monitor<B: Backend>(
40 &mut self,
41 backend: &mut B,
42 watcher: Watcher,
43 timeout: Duration,
44 ) -> Result<Watched> {
45 let now = Instant::now();
46
47 let mut watchers = Stack::empty();
48 drain_watchers(&mut watchers);
49
50 if watchers.contains(&watcher) {
51 return Ok(Watched::Triggered);
52 }
53
54 loop {
55 let dur = self.tick(backend)?;
56 self.present(backend);
57 self.cleanup();
58
59 drain_watchers(&mut watchers);
60
61 if watchers.contains(&watcher) {
62 return Ok(Watched::Triggered);
63 }
64
65 if timeout.saturating_sub(now.elapsed()).is_zero() {
66 break Ok(Watched::Timeout);
67 }
68
69 let sleep = self.sleep_micros - dur.as_micros() as u64;
70 std::thread::sleep(Duration::from_micros(sleep));
71 }
72 }
73}