Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transpose to the OptionStoreExt #3534

Merged
merged 4 commits into from
Feb 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions reactive_stores/src/option.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{StoreField, Subfield};
use reactive_graph::traits::{Read, ReadUntracked};
use reactive_graph::traits::{FlattenOptionRefOption, Read, ReadUntracked};
use std::ops::Deref;

/// Extends optional store fields, with the ability to unwrap or map over them.
Expand All @@ -13,6 +13,13 @@ where
/// Provides access to the inner value, as a subfield, unwrapping the outer value.
fn unwrap(self) -> Subfield<Self, Option<Self::Output>, Self::Output>;

/// Inverts a subfield of an `Option` to an `Option` of a subfield.
fn invert(
self,
) -> Option<Subfield<Self, Option<Self::Output>, Self::Output>> {
self.map(|f| f)
}

/// Reactively maps over the field.
///
/// This returns `None` if the subfield is currently `None`,
Expand Down Expand Up @@ -56,7 +63,7 @@ where
self,
map_fn: impl FnOnce(Subfield<S, Option<T>, T>) -> U,
) -> Option<U> {
if self.read().is_some() {
if self.try_read().as_deref().flatten().is_some() {
Some(map_fn(self.unwrap()))
} else {
None
Expand All @@ -67,7 +74,7 @@ where
self,
map_fn: impl FnOnce(Subfield<S, Option<T>, T>) -> U,
) -> Option<U> {
if self.read_untracked().is_some() {
if self.try_read_untracked().as_deref().flatten().is_some() {
Some(map_fn(self.unwrap()))
} else {
None
Expand Down
Loading