Skip to content

Commit c03e6f8

Browse files
committed
fix: improve internal signal dependency checking logic
1 parent 0c58524 commit c03e6f8

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

.changeset/olive-kangaroos-brake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: improve internal signal dependency checking logic

packages/svelte/src/internal/client/runtime.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,15 +349,21 @@ function execute_signal_fn(signal) {
349349
if (current_dependencies !== null) {
350350
let i;
351351
if (dependencies !== null) {
352-
const dep_length = dependencies.length;
352+
// Include any dependencies up until the current_dependencies_index.
353+
const full_dependencies =
354+
current_dependencies_index === 0
355+
? dependencies
356+
: dependencies.slice(0, current_dependencies_index).concat(current_dependencies);
357+
const dep_length = full_dependencies.length;
353358
// If we have more than 16 elements in the array then use a Set for faster performance
354359
// TODO: evaluate if we should always just use a Set or not here?
355-
const current_dependencies_set = dep_length > 16 ? new Set(current_dependencies) : null;
360+
const current_dependencies_set = dep_length > 16 ? new Set(full_dependencies) : null;
361+
356362
for (i = current_dependencies_index; i < dep_length; i++) {
357-
const dependency = dependencies[i];
363+
const dependency = full_dependencies[i];
358364
if (
359365
(current_dependencies_set !== null && !current_dependencies_set.has(dependency)) ||
360-
!current_dependencies.includes(dependency)
366+
!full_dependencies.includes(dependency)
361367
) {
362368
remove_consumer(signal, dependency, false);
363369
}

packages/svelte/tests/runtime-runes/samples/each-updates/_config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default test({
3333

3434
assert.htmlEqual(
3535
target.innerHTML,
36-
`<p>test costs $1</p><p>test 2 costs $2000</p><p>test costs $1</p><p>test 2 costs $2000</p><button>add</button><button>change</button><button>reload</button>`
36+
`<p>test costs $1</p><p>test 2 costs $2</p><p>test costs $1</p><p>test 2 costs $2</p><button>add</button><button>change</button><button>reload</button>`
3737
);
3838
}
3939
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
html: `<p>0 - 0</p><button>+</button`,
6+
7+
async test({ assert, target }) {
8+
const [btn1] = target.querySelectorAll('button');
9+
10+
flushSync(() => {
11+
btn1.click();
12+
});
13+
14+
assert.htmlEqual(target.innerHTML, `<p>1 - 1</p><button>+</button`);
15+
16+
flushSync(() => {
17+
btn1.click();
18+
});
19+
20+
assert.htmlEqual(target.innerHTML, `<p>2 - 2</p><button>+</button`);
21+
}
22+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script>
2+
let x = $state({a: 0, b:0});
3+
let count = 0;
4+
</script>
5+
6+
<p>{x.a} - {x.b}</p>
7+
8+
<button onclick={() => {
9+
const a = ++count;
10+
x = {a, b: a};
11+
}}>+</button>
12+

0 commit comments

Comments
 (0)