Skip to content

Commit

Permalink
fix: further avoid duplicate signal dependencies
Browse files Browse the repository at this point in the history
visitor

another approach

tune

tune
  • Loading branch information
trueadm committed Jul 2, 2024
1 parent 84325bf commit bb9c448
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-phones-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: handle duplicate signal dependencies gracefully
12 changes: 7 additions & 5 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,15 @@ function remove_reaction(signal, dependency) {
export function remove_reactions(signal, start_index) {
const dependencies = signal.deps;
if (dependencies !== null) {
const active_dependencies = start_index === 0 ? null : dependencies.slice(0, start_index);
var active_dependencies = start_index === 0 ? null : dependencies.slice(0, start_index);
var visited = new Set();
let i;
for (i = start_index; i < dependencies.length; i++) {
const dependency = dependencies[i];
if (visited.has(dependency)) {
continue;
}
visited.add(dependency);
// Avoid removing a reaction if we know that it is active (start_index will not be 0)
if (active_dependencies === null || !active_dependencies.includes(dependency)) {
remove_reaction(signal, dependency);
Expand Down Expand Up @@ -774,10 +779,7 @@ export function get(signal) {
) {
if (current_dependencies === null) {
current_dependencies = [signal];
} else if (
current_dependencies[current_dependencies.length - 1] !== signal &&
!current_dependencies.includes(signal)
) {
} else if (current_dependencies[current_dependencies.length - 1] !== signal) {
current_dependencies.push(signal);
}
}
Expand Down

0 comments on commit bb9c448

Please sign in to comment.