From 194afd48b6532b5375f2e4b55a9accc7c1ef79ca Mon Sep 17 00:00:00 2001 From: Ari Seyhun Date: Fri, 15 Nov 2024 15:15:21 +0800 Subject: [PATCH] feat(reactive_stores): add `map_untracked` to `OptionStoreExt` --- reactive_stores/src/option.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/reactive_stores/src/option.rs b/reactive_stores/src/option.rs index a02fe8e823..439eceb5b8 100644 --- a/reactive_stores/src/option.rs +++ b/reactive_stores/src/option.rs @@ -1,5 +1,5 @@ use crate::{StoreField, Subfield}; -use reactive_graph::traits::Read; +use reactive_graph::traits::{Read, ReadUntracked}; use std::ops::Deref; /// Extends optional store fields, with the ability to unwrap or map over them. @@ -23,12 +23,23 @@ where self, map_fn: impl FnOnce(Subfield, Self::Output>) -> U, ) -> Option; + + /// Unreactively maps over the field. + /// + /// This returns `None` if the subfield is currently `None`, + /// and a new store subfield with the inner value if it is `Some`. This is an unreactive variant of + /// `[OptionStoreExt::map]`, and will not cause the reactive context to re-run if the field changes. + fn map_untracked( + self, + map_fn: impl FnOnce(Subfield, Self::Output>) -> U, + ) -> Option; } impl OptionStoreExt for S where - S: StoreField> + Read, + S: StoreField> + Read + ReadUntracked, ::Value: Deref>, + ::Value: Deref>, { type Output = T; @@ -51,6 +62,17 @@ where None } } + + fn map_untracked( + self, + map_fn: impl FnOnce(Subfield, T>) -> U, + ) -> Option { + if self.read_untracked().is_some() { + Some(map_fn(self.unwrap())) + } else { + None + } + } } #[cfg(test)]