Browse Source

Reuse existing slots in provide context

Jonathan Kelley 2 years ago
parent
commit
1489492ec4
1 changed files with 13 additions and 5 deletions
  1. 13 5
      packages/core/src/scopes.rs

+ 13 - 5
packages/core/src/scopes.rs

@@ -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();
+
+        // 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;
+            }
+        }
 
-        self.shared_contexts
-            .borrow_mut()
-            .push((TypeId::of::<T>(), Box::new(value)));
+        // Else, just push it
+        contexts.push((TypeId::of::<T>(), Box::new(value.clone())));
 
-        value2
+        value
     }
 
     /// Pushes the future onto the poll queue to be polled after the component renders.