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 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> 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