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

Replace try_get() with get() in several trait implementations for signals #3569

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
106 changes: 69 additions & 37 deletions tachys/src/reactive_graph/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ where
Key: AttributeKey,
Sig: IntoSplitSignal<Value = T>,
T: FromEventTarget + AttributeValue + PartialEq + Sync + 'static,
Signal<BoolOrT<T>>: IntoProperty,
Signal<Option<BoolOrT<T>>>: IntoProperty,
<Sig as IntoSplitSignal>::Read:
Get<Value = T> + Send + Sync + Clone + 'static,
<Sig as IntoSplitSignal>::Write: Send + Clone + 'static,
Expand Down Expand Up @@ -167,17 +167,42 @@ where

/// Creates the signal to update the value of the attribute. This signal is different
/// when using a `"group"` attribute
pub fn read_signal(&self, el: &Element) -> Signal<BoolOrT<T>> {
pub fn read_signal(&self, el: &Element) -> Signal<Option<BoolOrT<T>>> {
let read_signal = self.read_signal.clone();

let el = SendWrapper::new(el.clone());
if Key::KEY == "group" {
let el = SendWrapper::new(el.clone());

Signal::derive(move || {
BoolOrT::Bool(el.get_value() == read_signal.get())
read_signal
.try_get()
.map(|r| BoolOrT::Bool(el.get_value() == r))
.or_else(|| {
crate::dispose_warn!(
read_signal,
format!(
"Reactive value passed to: bind:{}\nElement: \
{}",
Key::KEY,
el.node_name().to_lowercase(),
)
);
None
})
})
} else {
Signal::derive(move || BoolOrT::T(read_signal.get()))
Signal::derive(move || {
read_signal.try_get().map(BoolOrT::T).or_else(|| {
crate::dispose_warn!(
read_signal,
format!(
"Reactive value passed to: bind:{}\nElement: {}",
Key::KEY,
el.node_name().to_lowercase(),
),
);
None
})
})
}
}

Expand All @@ -197,14 +222,14 @@ where
Key: AttributeKey,
T: FromEventTarget + AttributeValue + PartialEq + Sync + 'static,
R: Get<Value = T> + Clone + Send + Sync + 'static,
Signal<BoolOrT<T>>: IntoProperty,
Signal<Option<BoolOrT<T>>>: IntoProperty,
W: Update<Value = T> + Clone + Send + 'static,
Element: ChangeEvent + GetValue<T>,
{
const MIN_LENGTH: usize = 0;

type State = (
<Signal<BoolOrT<T>> as IntoProperty>::State,
<Signal<Option<BoolOrT<T>>> as IntoProperty>::State,
(Element, Option<RemoveEventHandler<Element>>),
);
type AsyncOutput = Self;
Expand Down Expand Up @@ -277,7 +302,7 @@ where
Key: AttributeKey,
T: FromEventTarget + AttributeValue + PartialEq + Sync + 'static,
R: Get<Value = T> + Clone + Send + Sync + 'static,
Signal<BoolOrT<T>>: IntoProperty,
Signal<Option<BoolOrT<T>>>: IntoProperty,
W: Update<Value = T> + Clone + Send + 'static,
Element: ChangeEvent + GetValue<T>,
{
Expand Down Expand Up @@ -492,14 +517,14 @@ pub enum BoolOrT<T> {
T(T),
}

impl<T> IntoProperty for BoolOrT<T>
impl<T> IntoProperty for Option<BoolOrT<T>>
where
T: IntoProperty<State = (Element, JsValue)>
+ Into<JsValue>
+ Clone
+ 'static,
{
type State = (Element, JsValue);
type State = Option<(Element, JsValue)>;
type Cloneable = Self;
type CloneableOwned = Self;

Expand All @@ -508,41 +533,48 @@ where
el: &Element,
key: &str,
) -> Self::State {
match self.clone() {
Self::T(s) => {
s.hydrate::<FROM_SERVER>(el, key);
}
Self::Bool(b) => {
<bool as IntoProperty>::hydrate::<FROM_SERVER>(b, el, key);
}
};
self.clone().map(|f| {
match f.clone() {
BoolOrT::T(s) => {
s.hydrate::<FROM_SERVER>(el, key);
}
BoolOrT::Bool(b) => {
<bool as IntoProperty>::hydrate::<FROM_SERVER>(b, el, key);
}
};

(el.clone(), self.into())
(el.clone(), self.into())
})
}

fn build(self, el: &Element, key: &str) -> Self::State {
match self.clone() {
Self::T(s) => {
s.build(el, key);
}
Self::Bool(b) => {
<bool as IntoProperty>::build(b, el, key);
self.clone().map(|f| {
match f.clone() {
BoolOrT::T(s) => {
s.build(el, key);
}
BoolOrT::Bool(b) => {
<bool as IntoProperty>::build(b, el, key);
}
}
}

(el.clone(), self.into())
(el.clone(), self.into())
})
}

fn rebuild(self, state: &mut Self::State, key: &str) {
let (el, prev) = state;

match self {
Self::T(s) => s.rebuild(&mut (el.clone(), prev.clone()), key),
Self::Bool(b) => <bool as IntoProperty>::rebuild(
b,
&mut (el.clone(), prev.clone()),
key,
),
if let (Some(state), Some(f)) = (state, self) {
let (el, prev) = state;
match f {
BoolOrT::T(s) => {
s.rebuild(&mut (el.clone(), prev.clone()), key)
}
BoolOrT::Bool(b) => <bool as IntoProperty>::rebuild(
b,
&mut (el.clone(), prev.clone()),
key,
),
}
}
}

Expand Down
Loading
Loading