dioxus_lib::prelude

Struct Runtime

source
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

source

pub fn current() -> Result<Rc<Runtime>, RuntimeError>

Get the current runtime

source

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

source

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

source

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

source

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}");
    }
});
source

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.

source

pub fn current_task(&self) -> Option<Task>

Get the currently running task

source

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> InitializeFromFunction<T> for T

source§

fn initialize_from_function(f: fn() -> T) -> T

Create an instance of this type from an initialization function
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<Ret> SpawnIfAsync<(), Ret> for Ret

source§

fn spawn(self) -> Ret

Spawn the value into the dioxus runtime if it is an async block
source§

impl<T, O> SuperFrom<T> for O
where O: From<T>,

source§

fn super_from(input: T) -> O

Convert from a type to another type.
source§

impl<T, O, M> SuperInto<O, M> for T
where O: SuperFrom<T, M>,

source§

fn super_into(self) -> O

Convert from a type to another type.
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more