Skip to main content

Provider

Trait Provider 

Source
pub trait Provider<Param = ()>:
    Clone
    + PartialEq
    + 'static
where Param: ProviderParamBounds,
{ type Output: ProviderOutputBounds; type Error: ProviderErrorBounds; // Required method fn run( &self, param: Param, ) -> impl Future<Output = Result<Self::Output, Self::Error>>; // Provided methods fn id(&self, param: &Param) -> String { ... } fn interval(&self) -> Option<Duration> { ... } fn cache_expiration(&self) -> Option<Duration> { ... } fn stale_time(&self) -> Option<Duration> { ... } }
Expand description

A unified trait for defining providers - async operations that return data

This trait supports both simple providers (no parameters) and parameterized providers. Use Provider<()> for simple providers and Provider<ParamType> for parameterized providers.

§Features

  • Async Execution: All providers are async by default
  • Configurable Caching: Optional cache expiration times
  • Stale-While-Revalidate: Serve stale data while revalidating in background
  • Auto-Refresh: Optional automatic refresh at intervals
  • Auto-Dispose: Automatic cleanup when providers are no longer used

§Cross-Platform Compatibility

The Provider trait is designed to work across platforms using Dioxus’s spawn system:

  • Uses dioxus::spawn for async execution (no Send + Sync required for most types)
  • Parameters may need Send + Sync if shared across contexts
  • Output and Error types only need Clone since they stay within Dioxus context

§Example

use dioxus_provider::prelude::*;
use std::time::Duration;

#[provider(stale_time = "1m", cache_expiration = "5m")]
async fn data_provider() -> Result<String, String> {
    // Fetch data from API
    Ok("Hello, World!".to_string())
}

#[component]
fn Consumer() -> Element {
    let data = use_provider(data_provider(), ());
    // ...
}

Required Associated Types§

Source

type Output: ProviderOutputBounds

The type of data returned on success

Source

type Error: ProviderErrorBounds

The type of error returned on failure

Required Methods§

Source

fn run( &self, param: Param, ) -> impl Future<Output = Result<Self::Output, Self::Error>>

Execute the async operation

This method performs the actual work of the provider, such as fetching data from an API, reading from a database, or computing a value.

Provided Methods§

Source

fn id(&self, param: &Param) -> String

Get a unique identifier for this provider instance with the given parameters

This ID is used for caching and invalidation. The default implementation hashes the provider’s type and parameters to generate a unique ID.

Source

fn interval(&self) -> Option<Duration>

Get the interval duration for automatic refresh (None means no interval)

When set, the provider will automatically refresh its data at the specified interval, even if no component is actively watching it.

Source

fn cache_expiration(&self) -> Option<Duration>

Get the cache expiration duration (None means no expiration)

When set, cached data will be considered expired after this duration and will be removed from the cache, forcing a fresh fetch on the next access.

Source

fn stale_time(&self) -> Option<Duration>

Get the stale time duration for stale-while-revalidate behavior (None means no SWR)

When set, data older than this duration will be considered stale and will trigger a background revalidation while still serving the stale data to the UI.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§