mirror of
https://github.com/leptos-rs/leptos
synced 2024-11-12 23:57:09 +00:00
fix: leak in todomvc example (closes #706)
This commit is contained in:
parent
b472aaf6a0
commit
f0a9940364
1 changed files with 19 additions and 3 deletions
|
@ -43,7 +43,7 @@ impl Todos {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn remove(&mut self, id: Uuid) {
|
pub fn remove(&mut self, id: Uuid) {
|
||||||
self.0.retain(|todo| todo.id != id);
|
self.retain(|todo| todo.id != id);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn remaining(&self) -> usize {
|
pub fn remaining(&self) -> usize {
|
||||||
|
@ -76,7 +76,23 @@ impl Todos {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn clear_completed(&mut self) {
|
fn clear_completed(&mut self) {
|
||||||
self.0.retain(|todo| !todo.completed.get());
|
self.retain(|todo| !todo.completed.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn retain(&mut self, mut f: impl FnMut(&Todo) -> bool) {
|
||||||
|
self.0.retain(|todo| {
|
||||||
|
let retain = f(todo);
|
||||||
|
// because these signals are created at the top level,
|
||||||
|
// they are owned by the <TodoMVC/> component and not
|
||||||
|
// by the individual <Todo/> components. This means
|
||||||
|
// that if they are not manually disposed when removed, they
|
||||||
|
// will be held onto until the <TodoMVC/> is unmounted.
|
||||||
|
if !retain {
|
||||||
|
todo.title.dispose();
|
||||||
|
todo.completed.dispose();
|
||||||
|
}
|
||||||
|
retain
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,7 +152,7 @@ pub fn TodoMVC(cx: Scope) -> impl IntoView {
|
||||||
|
|
||||||
// Handle the three filter modes: All, Active, and Completed
|
// Handle the three filter modes: All, Active, and Completed
|
||||||
let (mode, set_mode) = create_signal(cx, Mode::All);
|
let (mode, set_mode) = create_signal(cx, Mode::All);
|
||||||
window_event_listener_untyped("hashchange", move |_| {
|
window_event_listener(ev::hashchange, move |_| {
|
||||||
let new_mode =
|
let new_mode =
|
||||||
location_hash().map(|hash| route(&hash)).unwrap_or_default();
|
location_hash().map(|hash| route(&hash)).unwrap_or_default();
|
||||||
set_mode(new_mode);
|
set_mode(new_mode);
|
||||||
|
|
Loading…
Reference in a new issue