Replies: 2 comments 3 replies
-
So, one line is half your create_effect(cx, move |prev_value| {
costly.with(|costly| log::debug!("costly.value changed: {}", costly.value));
}); Of course that's only half your situation. The other half is that creating a signal says "hey, this signal owns this data." Creating a slice (which is just a memo) says "hey, this memo also owns this data." The only way to do that is to clone it — we don't (at the moment) have anything like a "memo by reference." One way forward of course would be to wrap Another way is to rewrite so you have something like create_effect(cx, move |prev_value| {
state.with(|state| log::debug!("value = {}", state.costly.value));
}); In this case you'll notice The general assumption with the system of signals/memos/effects is that your data is relatively cheap, and side effects are several orders of magnitude more expensive, so the system is optimized for that. If your data are expensive to clone, I'll admit a model that's based on ownership is not quite as good. Here's a version that uses an inner #[derive(Clone, Default, Debug, PartialEq)]
struct Costly(Rc<CostlyInner>);
#[derive(Default, Debug, PartialEq)]
struct CostlyInner {
value: u32,
}
// A hypothetical, costly to clone type.
impl Clone for CostlyInner {
fn clone(&self) -> Self {
log!("CostlyInner.clone() :(");
CostlyInner {
value: self.value
}
}
}
#[derive(Default, Clone)]
struct GlobalState {
count: u32,
costly: Costly,
} |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot for the prompt replies, and detailed explanation of trade-offs and alternatives! |
Beta Was this translation helpful? Give feedback.
-
I'm basing this off the Global State Management chapter in the Leptos book.
Let's assume I have some global state defined like this, where one field is really costly to clone:
Running the following I see calls to
Costly.clone()
after eachset_count(...)
:Any way to avoid this?
Perhaps
create_slice
(or a variant of it) could accept a function that allows to first compare the sliced field against a reference to the memorized value?Beta Was this translation helpful? Give feedback.
All reactions