Skip to content

Commit f8eb0d1

Browse files
authored
fix: improve each keyed block handling of item used as key (#10703)
* fix: improve each keyed block handling of item used as key * fix
1 parent d9d1022 commit f8eb0d1

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2393,15 +2393,15 @@ export const template_visitors = {
23932393
const binding = /** @type {import('#compiler').Binding} */ (context.state.scope.get(item.name));
23942394
binding.expression = (id) => {
23952395
const item_with_loc = with_loc(item, id);
2396-
return each_item_is_reactive ? b.call('$.unwrap', item_with_loc) : item_with_loc;
2396+
return b.call('$.unwrap', item_with_loc);
23972397
};
23982398
if (node.index) {
23992399
const index_binding = /** @type {import('#compiler').Binding} */ (
24002400
context.state.scope.get(node.index)
24012401
);
24022402
index_binding.expression = (id) => {
24032403
const index_with_loc = with_loc(index, id);
2404-
return each_item_is_reactive ? b.call('$.unwrap', index_with_loc) : index_with_loc;
2404+
return b.call('$.unwrap', index_with_loc);
24052405
};
24062406
}
24072407

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,10 @@ function reconcile_tracked_array(
458458
// are treated as reactive, so they get wrapped in a signal.
459459
if ((flags & EACH_IS_STRICT_EQUALS) !== 0 && !is_frozen(array) && !(STATE_SYMBOL in array)) {
460460
flags ^= EACH_IS_STRICT_EQUALS;
461+
// Additionally as we're in an keyed each block, we'll need ensure the itens are all wrapped in signals.
462+
if ((flags & EACH_ITEM_REACTIVE) === 0) {
463+
flags ^= EACH_ITEM_REACTIVE;
464+
}
461465
}
462466
var a_blocks = each_block.v;
463467
const is_computed_key = keys !== null;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { flushSync } from 'svelte';
22
import { test } from '../../test';
33

44
export default test({
5-
html: `1\n1\n<button>+</button>`,
5+
html: `1\n1\n1\n1\n<button>+</button>`,
66

77
async test({ assert, target }) {
88
/**
@@ -16,6 +16,6 @@ export default test({
1616
btn1.click();
1717
});
1818

19-
assert.htmlEqual(target.innerHTML, `2\n2\n<button>+</button>`);
19+
assert.htmlEqual(target.innerHTML, `2\n2\n2\n2\n<button>+</button>`);
2020
}
2121
});

packages/svelte/tests/runtime-runes/samples/each-updates-5/main.svelte

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
}
1111
</script>
1212

13+
{#each $store as item (item)}
14+
{item.value}
15+
{/each}
16+
{#each $storeDeeper.items as item (item)}
17+
{item.value}
18+
{/each}
1319
{#each $store as item}
1420
{item.value}
1521
{/each}

0 commit comments

Comments
 (0)