use super::CatchContext;
use crate::{compose::Compose, data::Data, use_provider, Scope, Signal};
use alloc::rc::Rc;
use core::mem;
pub fn catch<'a, C: Compose>(
on_error: impl Fn(Box<dyn core::error::Error>) + 'a,
content: C,
) -> Catch<'a, C> {
Catch {
content,
f: Rc::new(on_error),
}
}
#[derive(Clone, Data)]
#[actuate(path = "crate")]
pub struct Catch<'a, C> {
content: C,
f: Rc<dyn Fn(Box<dyn core::error::Error>) + 'a>,
}
impl<C: Compose> Compose for Catch<'_, C> {
fn compose(cx: Scope<Self>) -> impl Compose {
let f: &dyn Fn(Box<dyn core::error::Error>) = &*cx.me().f;
let f: Rc<dyn Fn(Box<dyn core::error::Error>)> = unsafe { mem::transmute(f) };
use_provider(&cx, move || CatchContext { f: f.clone() });
unsafe { Signal::map_unchecked(cx.me(), |me| &me.content) }
}
}