re_dataframe_ui/
requested_object.rsuse std::sync::mpsc::{sync_channel, Receiver};
use re_viewer_context::{AsyncRuntimeHandle, WasmNotSend};
#[derive(Debug)]
pub enum RequestedObject<T: Send + 'static> {
Pending(Receiver<T>),
Completed(T),
}
impl<T: Send + 'static> RequestedObject<T> {
pub fn new<F>(runtime: &AsyncRuntimeHandle, func: F) -> Self
where
F: std::future::Future<Output = T> + WasmNotSend + 'static,
{
let (tx, rx) = sync_channel(1);
let handle = Self::Pending(rx);
runtime.spawn_future(async move {
let result = func.await;
let _ = tx.send(result);
});
handle
}
pub fn new_with_repaint<F>(
runtime: &AsyncRuntimeHandle,
egui_ctx: egui::Context,
func: F,
) -> Self
where
F: std::future::Future<Output = T> + WasmNotSend + 'static,
{
Self::new(runtime, async move {
let result = func.await;
egui_ctx.request_repaint();
result
})
}
pub fn on_frame_start(&mut self) {
let result = match self {
Self::Pending(rx) => rx.try_recv().ok(),
Self::Completed(_) => None,
};
if let Some(result) = result {
*self = Self::Completed(result);
}
}
pub fn try_as_ref(&self) -> Option<&T> {
match self {
Self::Pending(_) => None,
Self::Completed(result) => Some(result),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn requested_object_not_clone() {
static_assertions::assert_not_impl_any!(RequestedObject<usize>: Clone);
}
}