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

Adding implemenatation of From trait to Model for derived signals #189

Closed
wants to merge 2 commits into from
Closed

Adding implemenatation of From trait to Model for derived signals #189

wants to merge 2 commits into from

Conversation

kandrelczyk
Copy link
Contributor

@kandrelczyk kandrelczyk commented May 11, 2024

Hi,

I'm trying to do something like that:

let error : RwSignal<Option<SomeError>> = create_rw_signal(None);
let show_error = Signal::derive(move||error.get().is_some()); 

<Modal title="title" show=show_error>
   { display error}
</Modal> 

This didn't work because show has type Model and you can't create Model from derived signal.

I've added the From<Signal<T>> for Model<T> implementation and it's working now.

Is this a good approach? I think especially for components like Modal handling derived signal is very useful but maybe there's better way to do it.

@@ -162,6 +162,14 @@ impl<T> From<(Memo<T>, WriteSignal<T>)> for Model<T> {
}
}

impl<T: Clone> From<Signal<T>> for Model<T> {
fn from(read: Signal<T>) -> Self {
let mut model = Self::new(read.get());
Copy link
Collaborator

@luoxiaozero luoxiaozero May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does read call get here? If you did this on purpose, why not change it to:

let error : RwSignal<Option<SomeError>> = create_rw_signal(None);
let show_error = Signal::derive(move||error.get().is_some()); 

<Modal title="title" show=show_error.get()>
   { display error}
</Modal>

If not, you can change it to ``

impl<T> From<(Signal<T>, Option<WriteSignal<T>>)> for Model<T>

Because I wrote the Model to extend the behavior of RwSignal, I used tuples to distinguish the extended behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, show=show_error.get() works fine. Don't know why I though this will not get updated on signal change. I will just close this. sorry.

@kandrelczyk
Copy link
Contributor Author

Closing since it's not needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants