Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve internal signal dependency checking logic #10111

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/olive-kangaroos-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: improve internal signal dependency checking logic
14 changes: 10 additions & 4 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,15 +349,21 @@ function execute_signal_fn(signal) {
if (current_dependencies !== null) {
let i;
if (dependencies !== null) {
const dep_length = dependencies.length;
// Include any dependencies up until the current_dependencies_index.
const full_dependencies =
current_dependencies_index === 0
? dependencies
: dependencies.slice(0, current_dependencies_index).concat(current_dependencies);
const dep_length = full_dependencies.length;
// If we have more than 16 elements in the array then use a Set for faster performance
// TODO: evaluate if we should always just use a Set or not here?
const current_dependencies_set = dep_length > 16 ? new Set(current_dependencies) : null;
const current_dependencies_set = dep_length > 16 ? new Set(full_dependencies) : null;

for (i = current_dependencies_index; i < dep_length; i++) {
const dependency = dependencies[i];
const dependency = full_dependencies[i];
if (
(current_dependencies_set !== null && !current_dependencies_set.has(dependency)) ||
!current_dependencies.includes(dependency)
!full_dependencies.includes(dependency)
) {
remove_consumer(signal, dependency, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default test({

assert.htmlEqual(
target.innerHTML,
`<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>`
`<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>`
);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
html: `<p>0 - 0</p><button>+</button`,

async test({ assert, target }) {
const [btn1] = target.querySelectorAll('button');

flushSync(() => {
btn1.click();
});

assert.htmlEqual(target.innerHTML, `<p>1 - 1</p><button>+</button`);

flushSync(() => {
btn1.click();
});

assert.htmlEqual(target.innerHTML, `<p>2 - 2</p><button>+</button`);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
let x = $state({a: 0, b:0});
let count = 0;
</script>

<p>{x.a} - {x.b}</p>

<button onclick={() => {
const a = ++count;
x = {a, b: a};
}}>+</button>