pub mod ui; mod view_models; mod views; use async_std::task::yield_now; use kifu_core::{Core, CoreRequest, CoreResponse, Observable}; use std::{rc::Rc, sync::Arc}; use tokio::runtime::Runtime; #[derive(Clone)] pub struct CoreApi { pub rt: Arc, pub core: Core, } impl CoreApi { pub fn dispatch(&self, request: CoreRequest) { /* spawn({ /* let gtk_tx = self.gtk_tx.clone(); let core = self.core.clone(); async move { gtk_tx.send(core.dispatch(request).await) } */ }); */ } } pub fn perftrace(trace_name: &str, f: F) -> A where F: FnOnce() -> A, { let start = std::time::Instant::now(); let result = f(); let end = std::time::Instant::now(); println!("[Trace: {}] {:?}", trace_name, end - start); result } struct LocalObserver { join_handle: glib::JoinHandle<()>, handler: Rc, } impl LocalObserver { fn new(observable: &dyn Observable, handler: impl Fn(T) + 'static) -> Self { let listener = observable.subscribe(); let handler = Rc::new(handler); let join_handle = glib::spawn_future_local({ let handler = handler.clone(); async move { loop { match listener.recv().await { Ok(msg) => handler(msg), Err(err) => { unimplemented!("Should display an error message in the UI: {}", err) } } yield_now().await; } } }); Self { join_handle, handler, } } } impl Drop for LocalObserver { fn drop(&mut self) { self.join_handle.abort(); } }