fix: prevent panicking if conditionally rendering Outlet

This commit is contained in:
Greg Johnston 2024-06-30 22:14:51 -04:00
parent 4eea1f046d
commit 33a3708f91
1 changed files with 16 additions and 6 deletions

View File

@ -13,7 +13,7 @@ use leptos::{component, oco::Oco};
use or_poisoned::OrPoisoned; use or_poisoned::OrPoisoned;
use reactive_graph::{ use reactive_graph::{
computed::ScopedFuture, computed::ScopedFuture,
owner::{provide_context, use_context, Owner}, owner::{on_cleanup, provide_context, use_context, Owner},
signal::{ArcRwSignal, ArcTrigger}, signal::{ArcRwSignal, ArcTrigger},
traits::{GetUntracked, ReadUntracked, Set, Track, Trigger}, traits::{GetUntracked, ReadUntracked, Set, Track, Trigger},
wrappers::write::SignalSetter, wrappers::write::SignalSetter,
@ -761,12 +761,22 @@ where
let ctx = use_context::<RouteContext<R>>() let ctx = use_context::<RouteContext<R>>()
.expect("<Outlet/> used without RouteContext being provided."); .expect("<Outlet/> used without RouteContext being provided.");
let RouteContext { trigger, rx, .. } = ctx; let RouteContext { trigger, rx, .. } = ctx;
let rx = rx.lock().or_poisoned().take().expect( let this_rx = rx.lock().or_poisoned().take().expect("<Outlet/> channel could not be acquired. Are you rendering the same <Outlet/> twice?");
"Tried to render <Outlet/> but could not find the view receiver. Are \ let this_rx = Arc::new(Mutex::new(Some(this_rx)));
you using the same <Outlet/> twice?", on_cleanup({
); let this_rx = Arc::clone(&this_rx);
move || {
leptos::logging::log!("restoring the channel");
*rx.lock().or_poisoned() =
Some(this_rx.lock().or_poisoned().take().unwrap());
}
});
move || { move || {
trigger.track(); trigger.track();
rx.try_recv().map(|view| view()) this_rx
.lock()
.or_poisoned()
.as_ref()
.and_then(|rx| rx.try_recv().ok().map(|view| view()))
} }
} }