Reuse existing slots in provide context

This commit is contained in:
Jonathan Kelley 2023-07-07 12:18:15 -07:00
parent 3a1b22aec2
commit 1489492ec4

View file

@ -401,13 +401,21 @@ impl<'src> ScopeState {
/// }
/// ```
pub fn provide_context<T: 'static + Clone>(&self, value: T) -> T {
let value2 = value.clone();
let mut contexts = self.shared_contexts.borrow_mut();
self.shared_contexts
.borrow_mut()
.push((TypeId::of::<T>(), Box::new(value)));
// If the context exists, swap it out for the new value
for ctx in contexts.iter_mut() {
// Swap the ptr directly
if let Some(ctx) = ctx.1.downcast_mut::<T>() {
std::mem::swap(ctx, &mut value.clone());
return value;
}
}
value2
// Else, just push it
contexts.push((TypeId::of::<T>(), Box::new(value.clone())));
value
}
/// Pushes the future onto the poll queue to be polled after the component renders.