improve context tracing

This commit is contained in:
Evan Almloff committed 2023-09-08 10:00:05 -05:00
1 parent c55c17cb81
commit 66333e9767
1 file changed
+34 -7
+34 -7
View File
@@ -107,20 +107,30 @@ impl ScopeContext {
/// ///
/// Clones the state if it exists. /// Clones the state if it exists.
pub fn consume_context<T: 'static + Clone>(&self) -> Option<T> { pub fn consume_context<T: 'static + Clone>(&self) -> Option<T> {
tracing::trace!(
"looking for context {} ({:?}) in {}",
std::any::type_name::<T>(),
std::any::TypeId::of::<T>(),
self.name
);
if let Some(this_ctx) = self.has_context() { if let Some(this_ctx) = self.has_context() {
return Some(this_ctx); return Some(this_ctx);
} }
let mut search_parent = self.parent_id; let mut search_parent = self.parent_id;
with_runtime(|runtime| { match with_runtime(|runtime: &crate::innerlude::Runtime| {
while let Some(parent_id) = search_parent { while let Some(parent_id) = search_parent {
let parent = runtime.get_context(parent_id).unwrap(); let parent = runtime.get_context(parent_id).unwrap();
if let Some(shared) = parent tracing::trace!(
.shared_contexts "looking for context {} ({:?}) in {}",
.borrow() std::any::type_name::<T>(),
.iter() std::any::TypeId::of::<T>(),
.find_map(|any| any.downcast_ref::<T>()) parent.name
{ );
if let Some(shared) = parent.shared_contexts.borrow().iter().find_map(|any| {
tracing::trace!("found context {:?}", any.type_id());
any.downcast_ref::<T>()
}) {
return Some(shared.clone()); return Some(shared.clone());
} }
search_parent = parent.parent_id; search_parent = parent.parent_id;
@@ -128,6 +138,17 @@ impl ScopeContext {
None None
}) })
.flatten() .flatten()
{
Some(ctx) => Some(ctx),
None => {
tracing::trace!(
"context {} ({:?}) not found",
std::any::type_name::<T>(),
std::any::TypeId::of::<T>()
);
None
}
}
} }
/// Expose state to children further down the [`crate::VirtualDom`] Tree. Requires `Clone` on the context to allow getting values down the tree. /// Expose state to children further down the [`crate::VirtualDom`] Tree. Requires `Clone` on the context to allow getting values down the tree.
@@ -152,6 +173,12 @@ impl ScopeContext {
/// } /// }
/// ``` /// ```
pub fn provide_context<T: 'static + Clone>(&self, value: T) -> T { pub fn provide_context<T: 'static + Clone>(&self, value: T) -> T {
tracing::trace!(
"providing context {} ({:?}) in {}",
std::any::type_name::<T>(),
std::any::TypeId::of::<T>(),
self.name
);
let mut contexts = self.shared_contexts.borrow_mut(); let mut contexts = self.shared_contexts.borrow_mut();
// If the context exists, swap it out for the new value // If the context exists, swap it out for the new value