monorepo/kifu/gtk/src/lib.rs

77 lines
1.8 KiB
Rust
Raw Normal View History

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;
2023-04-07 01:52:39 +00:00
#[derive(Clone)]
pub struct CoreApi {
pub rt: Arc<Runtime>,
pub core: Core,
2023-04-07 01:52:39 +00:00
}
impl CoreApi {
pub fn dispatch(&self, request: CoreRequest) {
/*
spawn({
/*
2023-04-07 01:52:39 +00:00
let gtk_tx = self.gtk_tx.clone();
let core = self.core.clone();
async move { gtk_tx.send(core.dispatch(request).await) }
*/
2023-04-07 01:52:39 +00:00
});
*/
2023-04-07 01:52:39 +00:00
}
}
pub fn perftrace<F, A>(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);
2023-10-05 16:19:57 +00:00
result
}
struct LocalObserver<T> {
join_handle: glib::JoinHandle<()>,
handler: Rc<dyn Fn(T)>,
}
impl<T: 'static> LocalObserver<T> {
fn new(observable: &dyn Observable<T>, 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<T> Drop for LocalObserver<T> {
fn drop(&mut self) {
self.join_handle.abort();
}
}