pub struct MutationContext<'a, Data, Error> { /* private fields */ }Expand description
Provides access to cached mutation data for implementations generated by the macro.
Implementations§
Source§impl<'a, Data, Error> MutationContext<'a, Data, Error>
impl<'a, Data, Error> MutationContext<'a, Data, Error>
Sourcepub fn new(current: Option<&'a Result<Data, Error>>) -> Self
pub fn new(current: Option<&'a Result<Data, Error>>) -> Self
Create a new mutation context from the current cached result.
Sourcepub fn current(&self) -> Option<&Result<Data, Error>>
pub fn current(&self) -> Option<&Result<Data, Error>>
Returns the current cached result, including the error if the cache failed previously.
Sourcepub fn current_success(&self) -> Option<&Data>
pub fn current_success(&self) -> Option<&Data>
Returns a reference to the current successful cached data, if available.
Sourcepub fn cloned_success(&self) -> Option<Data>where
Data: Clone,
pub fn cloned_success(&self) -> Option<Data>where
Data: Clone,
Clones the current successful cached data, if available.
Sourcepub fn map_current<F>(&self, f: F) -> Option<Data>
pub fn map_current<F>(&self, f: F) -> Option<Data>
Applies a transformation to the cloned cached data and returns the updated value.
Sourcepub fn map_or_else<F, D>(&self, default: D, f: F) -> Data
pub fn map_or_else<F, D>(&self, default: D, f: F) -> Data
Applies a transformation to the cloned cached data, or returns a default value if no data is available.
This is useful when you need to ensure a value is always returned, even if the cache is empty.
§Example
use dioxus_provider::prelude::*;
#[mutation(invalidates = [fetch_items])]
async fn add_item(
item: String,
ctx: MutationContext<Vec<String>, String>,
) -> Result<Vec<String>, String> {
Ok(ctx.map_or_else(
|| vec![item.clone()], // default if no cached data
|items| {
items.push(item.clone());
}
))
}Sourcepub fn update_in_place<F>(&self, f: F) -> Option<Data>
pub fn update_in_place<F>(&self, f: F) -> Option<Data>
Updates the cached data in place, returning the modified data or None if no data exists.
This is similar to map_current but provides a more explicit name for mutations
that modify existing data.
§Example
use dioxus_provider::prelude::*;
#[mutation(invalidates = [fetch_counter])]
async fn increment_counter(
ctx: MutationContext<i32, String>,
) -> Result<i32, String> {
ctx.update_in_place(|count| *count += 1)
.ok_or_else(|| "No counter data available".to_string())
}