From 23c215da0218281db7e342e053e1709b4ae3495d Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Sun, 17 Sep 2023 14:11:13 -0500 Subject: [PATCH 1/3] make RuntimeGuard public --- packages/core/src/runtime.rs | 81 +++++++++++++++--------------------- 1 file changed, 34 insertions(+), 47 deletions(-) diff --git a/packages/core/src/runtime.rs b/packages/core/src/runtime.rs index 4b9dcdab0..566e9ba7c 100644 --- a/packages/core/src/runtime.rs +++ b/packages/core/src/runtime.rs @@ -7,51 +7,6 @@ thread_local! { static RUNTIMES: RefCell>> = RefCell::new(vec![]); } -/// Run some code within a runtime -pub fn in_runtime(runtime: Rc, f: impl FnOnce() -> R) -> R { - let _guard = RuntimeGuard::new(runtime); - f() -} - -/// Override the current runtime. This must be used to override the current runtime when importing components from a dynamic library that has it's own runtime. -/// -/// ```rust -/// use dioxus::prelude::*; -/// -/// fn main() { -/// let virtual_dom = VirtualDom::new(app); -/// } -/// -/// fn app(cx: Scope) -> Element { -/// render!{ Component { runtime: Runtime::current().unwrap() } } -/// } -/// -/// // In a dynamic library -/// #[derive(Props)] -/// struct ComponentProps { -/// runtime: std::rc::Rc, -/// } -/// -/// impl PartialEq for ComponentProps { -/// fn eq(&self, _other: &Self) -> bool { -/// true -/// } -/// } -/// -/// fn Component(cx: Scope) -> Element { -/// cx.use_hook(|| override_runtime(cx.props.runtime.clone())); -/// -/// render! { div {} } -/// } -/// ``` -pub fn override_runtime(runtime: Rc) { - RUNTIMES.with(|stack| { - let mut stack = stack.borrow_mut(); - stack.pop(); - stack.push(runtime); - }); -} - /// Pushes a new scope onto the stack pub(crate) fn push_runtime(runtime: Rc) { RUNTIMES.with(|stack| stack.borrow_mut().push(runtime)); @@ -143,10 +98,42 @@ impl Runtime { } } -pub(crate) struct RuntimeGuard(Rc); +/// A gaurd for a new runtime. This must be used to override the current runtime when importing components from a dynamic library that has it's own runtime. +/// +/// ```rust +/// use dioxus::prelude::*; +/// +/// fn main() { +/// let virtual_dom = VirtualDom::new(app); +/// } +/// +/// fn app(cx: Scope) -> Element { +/// render!{ Component { runtime: Runtime::current().unwrap() } } +/// } +/// +/// // In a dynamic library +/// #[derive(Props)] +/// struct ComponentProps { +/// runtime: std::rc::Rc, +/// } +/// +/// impl PartialEq for ComponentProps { +/// fn eq(&self, _other: &Self) -> bool { +/// true +/// } +/// } +/// +/// fn Component(cx: Scope) -> Element { +/// cx.use_hook(|| RuntimeGuard::new(cx.props.runtime.clone())); +/// +/// render! { div {} } +/// } +/// ``` +pub struct RuntimeGuard(Rc); impl RuntimeGuard { - pub(crate) fn new(runtime: Rc) -> Self { + /// Create a new runtime guard that sets the current Dioxus runtime. The runtime will be reset when the guard is dropped + pub fn new(runtime: Rc) -> Self { push_runtime(runtime.clone()); Self(runtime) } From b9a92a1bd814101f65d7038e07678152f0bfa848 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Sun, 17 Sep 2023 20:03:58 -0500 Subject: [PATCH 2/3] fix core exports --- packages/core/src/lib.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/core/src/lib.rs b/packages/core/src/lib.rs index 5b2f83305..029e884db 100644 --- a/packages/core/src/lib.rs +++ b/packages/core/src/lib.rs @@ -32,7 +32,7 @@ pub(crate) mod innerlude { pub use crate::nodes::RenderReturn; pub use crate::nodes::*; pub use crate::properties::*; - pub use crate::runtime::{in_runtime, override_runtime, Runtime}; + pub use crate::runtime::{Runtime, RuntimeGuard}; pub use crate::scheduler::*; pub use crate::scope_context::*; pub use crate::scopes::*; @@ -87,11 +87,11 @@ pub use crate::innerlude::{ pub mod prelude { pub use crate::innerlude::{ consume_context, consume_context_from_scope, current_scope_id, fc_to_builder, has_context, - in_runtime, override_runtime, provide_context, provide_context_to_scope, - provide_root_context, push_future, remove_future, schedule_update_any, spawn, - spawn_forever, suspend, throw, AnyValue, Component, Element, Event, EventHandler, Fragment, - IntoAttributeValue, LazyNodes, Properties, Runtime, Scope, ScopeId, ScopeState, Scoped, - TaskId, Template, TemplateAttribute, TemplateNode, Throw, VNode, VirtualDom, + provide_context, provide_context_to_scope, provide_root_context, push_future, + remove_future, schedule_update_any, spawn, spawn_forever, suspend, throw, AnyValue, + Component, Element, Event, EventHandler, Fragment, IntoAttributeValue, LazyNodes, + Properties, Runtime, RuntimeGuard, Scope, ScopeId, ScopeState, Scoped, TaskId, Template, + TemplateAttribute, TemplateNode, Throw, VNode, VirtualDom, }; } From ef6e9098fdd57e434b7efe67606fc4e5802c5b87 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Sun, 17 Sep 2023 20:06:41 -0500 Subject: [PATCH 3/3] fix formatting --- packages/fullstack/src/adapters/warp_adapter.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/fullstack/src/adapters/warp_adapter.rs b/packages/fullstack/src/adapters/warp_adapter.rs index 5afcb5077..316d70e56 100644 --- a/packages/fullstack/src/adapters/warp_adapter.rs +++ b/packages/fullstack/src/adapters/warp_adapter.rs @@ -143,7 +143,7 @@ pub fn register_server_fns(server_fn_route: &'static str) -> BoxedFilter<(impl R let req = warp::hyper::Request::from_parts(parts, bytes.into()); service.run(req).await.map_err(|err| { tracing::error!("Server function error: {}", err); - + struct WarpServerFnError(String); impl std::fmt::Debug for WarpServerFnError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -152,7 +152,7 @@ pub fn register_server_fns(server_fn_route: &'static str) -> BoxedFilter<(impl R } impl warp::reject::Reject for WarpServerFnError {} - + warp::reject::custom(WarpServerFnError(err.to_string())) }) }