mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-23 12:43:08 +00:00
Reuse existing slots in provide context
This commit is contained in:
parent
3a1b22aec2
commit
1489492ec4
1 changed files with 13 additions and 5 deletions
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue