pub trait Provider<Param = ()>:
Clone
+ PartialEq
+ 'staticwhere
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::spawnfor 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§
Sourcetype Output: ProviderOutputBounds
type Output: ProviderOutputBounds
The type of data returned on success
Sourcetype Error: ProviderErrorBounds
type Error: ProviderErrorBounds
The type of error returned on failure
Required Methods§
Provided Methods§
Sourcefn id(&self, param: &Param) -> String
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.
Sourcefn interval(&self) -> Option<Duration>
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.
Sourcefn cache_expiration(&self) -> Option<Duration>
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.
Sourcefn stale_time(&self) -> Option<Duration>
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".