feat: add Resource::map

This commit is contained in:
Matt Kane 2024-08-19 23:38:36 -05:00
parent b932bd5e04
commit 3b88c8ccd2

View file

@ -129,6 +129,14 @@ where
}
}
#[track_caller]
pub fn map<U>(&self, f: impl FnOnce(&T) -> U) -> Option<U>
where
T: Send + Sync + 'static,
{
self.data.try_with(|n| n.as_ref().map(f))?
}
/// Re-runs the async function with the current source data.
pub fn refetch(&self) {
*self.refetch.write() += 1;
@ -675,6 +683,12 @@ where
}
}
pub fn map<U>(&self, f: impl FnOnce(&T) -> U) -> Option<U> {
self.data
.try_with(|n| n.as_ref().map(|n| Some(f(n))))?
.flatten()
}
/// Re-runs the async function with the current source data.
pub fn refetch(&self) {
self.refetch.try_update(|n| *n += 1);