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§
§impl Runtime
impl Runtime
pub 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
pub 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
pub 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”
§impl Runtime
impl Runtime
pub 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}");
}
});
pub 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.
pub fn current_task(&self) -> Option<Task>
pub fn current_task(&self) -> Option<Task>
Get the currently running task
pub 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
Auto Trait Implementations§
impl !Freeze for Runtime
impl !RefUnwindSafe for Runtime
impl !Send for Runtime
impl !Sync for Runtime
impl Unpin for Runtime
impl !UnwindSafe for Runtime
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> InitializeFromFunction<T> for T
impl<T> InitializeFromFunction<T> for T
§fn initialize_from_function(f: fn() -> T) -> T
fn initialize_from_function(f: fn() -> T) -> T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more