pub struct Runtime { /* private fields */ }Expand description
A global runtime that is shared across all scopes that provides the async runtime and context API
Implementations§
Source§impl Runtime
impl Runtime
Sourcepub fn try_current() -> Option<Rc<Self>>
pub fn try_current() -> Option<Rc<Self>>
Try to get the current runtime, returning None if it doesn’t exist (outside the context of a dioxus app)
Sourcepub fn wrap_closure<'a, I, O>(f: impl Fn(I) -> O + 'a) -> impl Fn(I) -> O + 'a
pub fn wrap_closure<'a, I, O>(f: impl Fn(I) -> O + 'a) -> impl Fn(I) -> O + 'a
Wrap a closure so that it always runs in the runtime that is currently active
Sourcepub fn current_owner<S: AnyStorage>(&self) -> Owner<S>
pub fn current_owner<S: AnyStorage>(&self) -> Owner<S>
Get the owner for the current scope.
Sourcepub fn scope_owner<S: AnyStorage>(&self, scope: ScopeId) -> Owner<S>
pub fn scope_owner<S: AnyStorage>(&self, scope: ScopeId) -> Owner<S>
Get the owner for the current scope.
Sourcepub fn current_scope_id(&self) -> ScopeId
pub fn current_scope_id(&self) -> ScopeId
Get the current scope id
Sourcepub fn try_current_scope_id(&self) -> Option<ScopeId>
pub fn try_current_scope_id(&self) -> Option<ScopeId>
Try to get the current scope id, returning None if it we aren’t actively inside a scope
Sourcepub fn in_scope<O>(self: &Rc<Self>, id: ScopeId, f: impl FnOnce() -> O) -> O
pub fn in_scope<O>(self: &Rc<Self>, id: ScopeId, f: impl FnOnce() -> O) -> O
Call this function with the current scope set to the given scope
Sourcepub fn handle_event(
self: &Rc<Self>,
name: &str,
event: Event<dyn Any>,
element: ElementId,
)
pub fn handle_event( self: &Rc<Self>, name: &str, event: Event<dyn Any>, element: ElementId, )
Call a listener inside the VirtualDom with data from outside the VirtualDom. The ElementId passed in must be the id of an element with a listener, not a static node or a text node.
This method will identify the appropriate element. The data must match up with the listener declared. Note that this method does not give any indication as to the success of the listener call. If the listener is not found, nothing will happen.
It is up to the listeners themselves to mark nodes as dirty.
If you have multiple events, you can call this method multiple times before calling “render_with_deadline”
Sourcepub fn consume_context<T: 'static + Clone>(&self, id: ScopeId) -> Option<T>
pub fn consume_context<T: 'static + Clone>(&self, id: ScopeId) -> Option<T>
Consume context from the current scope
Sourcepub fn consume_context_from_scope<T: 'static + Clone>(
&self,
scope_id: ScopeId,
) -> Option<T>
pub fn consume_context_from_scope<T: 'static + Clone>( &self, scope_id: ScopeId, ) -> Option<T>
Consume context from the current scope
Sourcepub fn has_context<T: 'static + Clone>(&self, id: ScopeId) -> Option<T>
pub fn has_context<T: 'static + Clone>(&self, id: ScopeId) -> Option<T>
Check if the current scope has a context
Sourcepub fn provide_context<T: 'static + Clone>(&self, id: ScopeId, value: T) -> T
pub fn provide_context<T: 'static + Clone>(&self, id: ScopeId, value: T) -> T
Provide context to the current scope
Sourcepub fn parent_scope(&self, scope: ScopeId) -> Option<ScopeId>
pub fn parent_scope(&self, scope: ScopeId) -> Option<ScopeId>
Get the parent of the current scope if it exists
Sourcepub fn is_descendant_of(&self, us: ScopeId, other: ScopeId) -> bool
pub fn is_descendant_of(&self, us: ScopeId, other: ScopeId) -> bool
Check if the current scope is a descendant of the given scope
Sourcepub fn needs_update(&self, scope: ScopeId)
pub fn needs_update(&self, scope: ScopeId)
Mark the current scope as dirty, causing it to re-render
Sourcepub fn throw_error(
&self,
id: ScopeId,
error: impl Into<CapturedError> + 'static,
)
pub fn throw_error( &self, id: ScopeId, error: impl Into<CapturedError> + 'static, )
Throw a CapturedError into a scope. The error will bubble up to the nearest ErrorBoundary or the root of the app.
§Examples
fn Component() -> Element {
let request = spawn(async move {
match reqwest::get("https://api.example.com").await {
Ok(_) => unimplemented!(),
// You can explicitly throw an error into a scope with throw_error
Err(err) => dioxus::core::Runtime::current().throw_error(ScopeId::APP, err),
}
});
unimplemented!()
}Sourcepub fn suspense_context(&self) -> Option<SuspenseContext>
pub fn suspense_context(&self) -> Option<SuspenseContext>
Get the suspense context the current scope is in
Sourcepub fn force_all_dirty(&self)
pub fn force_all_dirty(&self)
Force every component to be dirty and require a re-render. Used by hot-reloading.
This might need to change to a different flag in the event hooks order changes within components. What we really need is a way to mark components as needing a complete rebuild if they were hit by changes.
Sourcepub fn vdom_is_rendering(&self) -> bool
pub fn vdom_is_rendering(&self) -> bool
Check if the virtual dom is currently rendering
Source§impl Runtime
impl Runtime
Sourcepub fn spawn_isomorphic(
&self,
scope: ScopeId,
task: impl Future<Output = ()> + 'static,
) -> Task
pub fn spawn_isomorphic( &self, scope: ScopeId, task: impl Future<Output = ()> + 'static, ) -> Task
Start a new future on the same thread as the rest of the VirtualDom.
You should generally use spawn instead of this method unless you specifically need to need to run a task during suspense
This future will not contribute to suspense resolving but it will run during suspense.
Because this future runs during suspense, you need to be careful to work with hydration. It is not recommended to do any async IO work in this future, as it can easily cause hydration issues. However, you can use isomorphic tasks to do work that can be consistently replicated on the server and client like logging or responding to state changes.
// ❌ Do not do requests in isomorphic tasks. It may resolve at a different time on the server and client, causing hydration issues.
let mut state = use_signal(|| None);
spawn_isomorphic(async move {
state.set(Some(reqwest::get("https://api.example.com").await));
});
// ✅ You may wait for a signal to change and then log it
let mut state = use_signal(|| 0);
spawn_isomorphic(async move {
loop {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
println!("State is {state}");
}
});Sourcepub fn spawn(
&self,
scope: ScopeId,
task: impl Future<Output = ()> + 'static,
) -> Task
pub fn spawn( &self, scope: ScopeId, task: impl Future<Output = ()> + 'static, ) -> Task
Start a new future on the same thread as the rest of the VirtualDom.
This future will not contribute to suspense resolving, so you should primarily use this for reacting to changes and long running tasks.
Whenever the component that owns this future is dropped, the future will be dropped as well.
Spawning a future onto the root scope will cause it to be dropped when the root component is dropped - which will only occur when the VirtualDom itself has been dropped.
Sourcepub fn current_task(&self) -> Option<Task>
pub fn current_task(&self) -> Option<Task>
Get the currently running task
Sourcepub fn parent_task(&self, task: Task) -> Option<Task>
pub fn parent_task(&self, task: Task) -> Option<Task>
Get the parent task of the given task, if it exists