Skip to content

Commit

Permalink
fix: guarantee execution order of effects (closes leptos-rs#2261)
Browse files Browse the repository at this point in the history
  • Loading branch information
gbj committed Feb 5, 2024
1 parent 06721c5 commit aec4d68
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion leptos_reactive/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,18 @@ impl Runtime {
let subs = self.node_subscribers.borrow();
for source in sources.borrow().iter() {
if let Some(source) = subs.get(*source) {
source.borrow_mut().swap_remove(&node_id);
// Using `.shift_remove()` here guarantees that dependencies
// of a signal are always triggered in the same order.
// This is important for cases in which, for example, the first effect
// conditionally checks that the signal value is `Some(_)`, and the
// second one unwraps its value; if they maintain this order, then the check
// will always run first, and will cancel the unwrap if it is None. But if the
// order can be inverted (by using .swap_remove() here), the unwrap will
// run first on a subsequent run.
//
// Maintaining execution order is the intention of using an IndexSet here anyway,
// but using .swap_remove() would undermine that goal.
source.borrow_mut().shift_remove(&node_id);
}
}
}
Expand Down

0 comments on commit aec4d68

Please sign in to comment.