Procházet zdrojové kódy

add context_provider method for !Send context in fullstack apps

Evan Almloff před 1 rokem
rodič
revize
5291b00798
1 změnil soubory, kde provedl 16 přidání a 0 odebrání
  1. 16 0
      packages/dioxus/src/launch.rs

+ 16 - 0
packages/dioxus/src/launch.rs

@@ -27,6 +27,22 @@ impl LaunchBuilder {
         }
     }
 
+    #[cfg(feature = "fullstack")]
+    /// Inject state into the root component's context that is created on the thread that the app is launched on.
+    pub fn context_provider(mut self, state: impl Fn() -> Box<dyn Any> + Send + 'static) -> Self {
+        self.contexts
+            .push(Box::new(state) as Box<dyn Fn() -> Box<dyn Any> + Send>);
+        self
+    }
+
+    #[cfg(not(feature = "fullstack"))]
+    /// Inject state into the root component's context that is created on the thread that the app is launched on.
+    pub fn context_provider(mut self, state: impl Fn() -> Box<dyn Any> + 'static) -> Self {
+        self.contexts
+            .push(Box::new(state) as Box<dyn Fn() -> Box<dyn Any>>);
+        self
+    }
+
     #[cfg(feature = "fullstack")]
     /// Inject state into the root component's context.
     pub fn context(mut self, state: impl Any + Clone + Send + 'static) -> Self {