fix: leak in todomvc example (closes #706)

This commit is contained in:
Greg Johnston 2023-05-15 14:53:37 -04:00
parent b472aaf6a0
commit f0a9940364

View file

@ -43,7 +43,7 @@ impl Todos {
}
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 {
@ -76,7 +76,23 @@ impl Todos {
}
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
let (mode, set_mode) = create_signal(cx, Mode::All);
window_event_listener_untyped("hashchange", move |_| {
window_event_listener(ev::hashchange, move |_| {
let new_mode =
location_hash().map(|hash| route(&hash)).unwrap_or_default();
set_mode(new_mode);