2023-03-23 03:11:56 +00:00
|
|
|
pub mod ui;
|
2024-02-27 13:34:17 +00:00
|
|
|
|
|
|
|
mod view_models;
|
|
|
|
mod views;
|
|
|
|
|
2024-02-28 04:21:54 +00:00
|
|
|
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>,
|
2024-02-27 13:34:17 +00:00
|
|
|
pub core: Core,
|
2023-04-07 01:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CoreApi {
|
2023-05-13 18:02:24 +00:00
|
|
|
pub fn dispatch(&self, request: CoreRequest) {
|
2024-02-27 12:49:10 +00:00
|
|
|
/*
|
|
|
|
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) }
|
2024-02-27 12:49:10 +00:00
|
|
|
*/
|
2023-04-07 01:52:39 +00:00
|
|
|
});
|
2024-02-27 12:49:10 +00:00
|
|
|
*/
|
2023-04-07 01:52:39 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-15 03:47:12 +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
|
2023-06-15 03:47:12 +00:00
|
|
|
}
|
2024-02-28 04:21:54 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|