Skip to content

fix: improve each block item equality for immutable mode #10537

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

Merged
merged 2 commits into from
Feb 19, 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/forty-dogs-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: improve each block item equality for immutable mode
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
DOMBooleanAttributes,
EACH_INDEX_REACTIVE,
EACH_IS_CONTROLLED,
EACH_IS_IMMUTABLE,
EACH_IS_STRICT_EQUALS,
EACH_ITEM_REACTIVE,
EACH_KEYED
} from '../../../../../constants.js';
Expand Down Expand Up @@ -2259,8 +2259,8 @@ export const template_visitors = {
each_type |= EACH_IS_CONTROLLED;
}

if (context.state.analysis.immutable) {
each_type |= EACH_IS_IMMUTABLE;
if (context.state.analysis.runes) {
each_type |= EACH_IS_STRICT_EQUALS;
}

// Find the parent each blocks which contain the arrays to invalidate
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const EACH_KEYED = 1 << 2;
/** See EachBlock interface metadata.is_controlled for an explanation what this is */
export const EACH_IS_CONTROLLED = 1 << 3;
export const EACH_IS_ANIMATED = 1 << 4;
export const EACH_IS_IMMUTABLE = 1 << 6;
export const EACH_IS_STRICT_EQUALS = 1 << 6;

export const PROPS_IS_IMMUTABLE = 1;
export const PROPS_IS_RUNES = 1 << 1;
Expand Down
8 changes: 4 additions & 4 deletions packages/svelte/src/internal/client/each.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
EACH_INDEX_REACTIVE,
EACH_IS_ANIMATED,
EACH_IS_CONTROLLED,
EACH_IS_IMMUTABLE,
EACH_IS_STRICT_EQUALS,
EACH_ITEM_REACTIVE,
EACH_KEYED
} from '../../constants.js';
Expand Down Expand Up @@ -852,9 +852,9 @@ function each_item_block(item, key, index, render_fn, flags) {

const item_value = each_item_not_reactive
? item
: (flags & EACH_IS_IMMUTABLE) === 0
? mutable_source(item)
: source(item);
: (flags & EACH_IS_STRICT_EQUALS) !== 0
? source(item)
: mutable_source(item);

const index_value = (flags & EACH_INDEX_REACTIVE) === 0 ? index : source(index);
const block = create_each_item_block(item_value, index_value, key);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
immutable: true,

test({ assert, target }) {
const btn = target.querySelector('button');

flushSync(() => {
btn?.click();
});

assert.htmlEqual(target.innerHTML, `<button>Update</button><ul><li>test !!!</li></ul>`);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script>
let items = [{id:1,value: "test"}]
const update = () => {
const clone = items.slice();
clone[0].value += " !!!";
items = clone;
}
</script>

<button on:click={update} >Update</button>

<ul>
{#each items as item (item.id)}
<li>{item.value}</li>
{/each}
</ul>