Runtime

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() -> Rc<Self>

Get the current runtime

Source

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)

Source

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

Source

pub fn current_owner<S: AnyStorage>(&self) -> Owner<S>

Get the owner for the current scope.

Source

pub fn scope_owner<S: AnyStorage>(&self, scope: ScopeId) -> Owner<S>

Get the owner for the current scope.

Source

pub fn current_scope_id(&self) -> ScopeId

Get the current scope id

Source

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

Source

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

Source

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”

Source

pub fn consume_context<T: 'static + Clone>(&self, id: ScopeId) -> Option<T>

Consume context from the current scope

Source

pub fn consume_context_from_scope<T: 'static + Clone>( &self, scope_id: ScopeId, ) -> Option<T>

Consume context from the current scope

Source

pub fn has_context<T: 'static + Clone>(&self, id: ScopeId) -> Option<T>

Check if the current scope has a context

Source

pub fn provide_context<T: 'static + Clone>(&self, id: ScopeId, value: T) -> T

Provide context to the current scope

Source

pub fn parent_scope(&self, scope: ScopeId) -> Option<ScopeId>

Get the parent of the current scope if it exists

Source

pub fn is_descendant_of(&self, us: ScopeId, other: ScopeId) -> bool

Check if the current scope is a descendant of the given scope

Source

pub fn needs_update(&self, scope: ScopeId)

Mark the current scope as dirty, causing it to re-render

Source

pub fn height(&self, id: ScopeId) -> u32

Get the height of the current scope

Source

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!()
}
Source

pub fn suspense_context(&self) -> Option<SuspenseContext>

Get the suspense context the current scope is in

Source

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.

Source

pub fn vdom_is_rendering(&self) -> bool

Check if the virtual dom is currently rendering

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