Skip to content

Commit d9d1022

Browse files
authored
fix: improve indexed each equality (#10702)
1 parent 4285e6d commit d9d1022

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

.changeset/spotty-houses-search.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 indexed each equality

packages/svelte/src/internal/client/dom/blocks/each.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,11 @@ function reconcile_indexed_array(
332332
flags,
333333
apply_transitions
334334
) {
335+
// If we are working with an array that isn't proxied or frozen, then remove strict equality and ensure the items
336+
// are treated as reactive, so they get wrapped in a signal.
337+
if ((flags & EACH_IS_STRICT_EQUALS) !== 0 && !is_frozen(array) && !(STATE_SYMBOL in array)) {
338+
flags ^= EACH_IS_STRICT_EQUALS;
339+
}
335340
var a_blocks = each_block.v;
336341
var active_transitions = each_block.s;
337342

@@ -449,7 +454,8 @@ function reconcile_tracked_array(
449454
apply_transitions,
450455
keys
451456
) {
452-
// If we are working with an array that isn't proxied or frozen, then remove strict equality.
457+
// If we are working with an array that isn't proxied or frozen, then remove strict equality and ensure the items
458+
// are treated as reactive, so they get wrapped in a signal.
453459
if ((flags & EACH_IS_STRICT_EQUALS) !== 0 && !is_frozen(array) && !(STATE_SYMBOL in array)) {
454460
flags ^= EACH_IS_STRICT_EQUALS;
455461
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
html: `1\n1\n<button>+</button>`,
6+
7+
async test({ assert, target }) {
8+
/**
9+
* @type {{ click: () => void; }}
10+
*/
11+
let btn1;
12+
13+
[btn1] = target.querySelectorAll('button');
14+
15+
flushSync(() => {
16+
btn1.click();
17+
});
18+
19+
assert.htmlEqual(target.innerHTML, `2\n2\n<button>+</button>`);
20+
}
21+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<script>
2+
import { writable } from "svelte/store";
3+
4+
let store = writable([{ value: 1 }]);
5+
let storeDeeper = writable({ items: [{ value: 1 }] });
6+
7+
function increment() {
8+
$store[0].value++;
9+
$storeDeeper.items[0].value++;
10+
}
11+
</script>
12+
13+
{#each $store as item}
14+
{item.value}
15+
{/each}
16+
{#each $storeDeeper.items as item}
17+
{item.value}
18+
{/each}
19+
20+
<button onclick={increment}>+</button>

0 commit comments

Comments
 (0)