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 wrap_closure<'a, I, O>(f: impl Fn(I) -> O + 'a) -> impl Fn(I) + 'a
pub fn wrap_closure<'a, I, O>(f: impl Fn(I) -> O + 'a) -> impl Fn(I) + 'a
Wrap a closure so that it always runs in the runtime that is currently active
sourcepub fn on_scope<O>(self: &Rc<Runtime>, id: ScopeId, f: impl FnOnce() -> O) -> O
pub fn on_scope<O>(self: &Rc<Runtime>, id: ScopeId, f: impl FnOnce() -> O) -> O
Call this function with the current scope set to the given scope
Useful in a limited number of scenarios
sourcepub fn handle_event(
self: &Rc<Runtime>,
name: &str,
event: Event<dyn Any>,
element: ElementId,
)
pub fn handle_event( self: &Rc<Runtime>, 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”
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