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 bind:this support for each blocks #10510

Merged
merged 1 commit into from
Feb 17, 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/cool-rabbits-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: improve bind:this support for each blocks
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,11 @@ function serialize_inline_component(node, component_name, context) {
const assignment = b.assignment('=', bind_this, b.id('$$value'));
const bind_this_id = /** @type {import('estree').Expression} */ (
// if expression is not an identifier, we know it can't be a signal
bind_this.type === 'Identifier' ? bind_this : undefined
bind_this.type === 'Identifier'
? bind_this
: bind_this.type === 'MemberExpression' && bind_this.object.type === 'Identifier'
? bind_this.object
: undefined
);
fn = (node_id) =>
b.call(
Expand Down Expand Up @@ -2742,7 +2746,11 @@ export const template_visitors = {
setter,
/** @type {import('estree').Expression} */ (
// if expression is not an identifier, we know it can't be a signal
expression.type === 'Identifier' ? expression : undefined
expression.type === 'Identifier'
? expression
: expression.type === 'MemberExpression' && expression.object.type === 'Identifier'
? expression.object
: undefined
)
);
break;
Expand Down
23 changes: 18 additions & 5 deletions packages/svelte/src/internal/client/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ import {
} from './utils.js';
import { is_promise } from '../common.js';
import { bind_transition, trigger_transitions } from './transitions.js';
import { proxy } from './proxy.js';
import { STATE_SYMBOL, proxy } from './proxy.js';

/** @type {Set<string>} */
const all_registerd_events = new Set();
Expand Down Expand Up @@ -1295,16 +1295,29 @@ export function bind_prop(props, prop, value) {
}
}

/**
* @param {unknown} value
*/
function is_state_object(value) {
return value != null && typeof value === 'object' && STATE_SYMBOL in value;
}

/**
* @param {Element} element_or_component
* @param {(value: unknown) => void} update
* @param {import('./types.js').MaybeSignal} binding
* @returns {void}
*/
export function bind_this(element_or_component, update, binding) {
untrack(() => {
update(element_or_component);
render_effect(() => () => {
render_effect(() => {
// If we are reading from a proxied state binding, then we don't need to untrack
// the update function as it will be fine-grain.
if (is_state_object(binding) || (is_signal(binding) && is_state_object(binding.v))) {
update(element_or_component);
} else {
untrack(() => update(element_or_component));
}
return () => {
// Defer to the next tick so that all updates can be reconciled first.
// This solves the case where one variable is shared across multiple this-bindings.
render_effect(() => {
Expand All @@ -1314,7 +1327,7 @@ export function bind_this(element_or_component, update, binding) {
}
});
});
});
};
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
const { text } = $props();

let boundParagraph = $state();

export function changeBackgroundToRed() {
boundParagraph.style.backgroundColor = 'red';
}
</script>

<p bind:this={boundParagraph}>
{text}
</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { flushSync } from '../../../../src/main/main-client';
import { test } from '../../test';

export default test({
async test({ assert, target }) {
const [btn, btn2, btn3] = target.querySelectorAll('button');

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

assert.htmlEqual(
target.innerHTML,
`<div><p style="background-color: red;">b1</p><button>change</button><button>delete</button></div><div><p>b2</p><button>change</button><button>delete</button></div>`
);

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

assert.htmlEqual(
target.innerHTML,
`<div><p>b2</p><button>change</button><button>delete</button></div>`
);

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

assert.htmlEqual(
target.innerHTML,
`<div><p style="background-color: red;">b2</p><button>change</button><button>delete</button></div>`
);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script>
import Paragraph from './Paragraph.svelte';
let boundParagraphs = $state([]);

let store = $state([
{ id: 1, text: 'b1' },
{ id: 2, text: 'b2' }
]);
</script>

{#each store as text, i (text.id)}
<div>
<Paragraph bind:this={boundParagraphs[i]} text={text.text}></Paragraph>
<button onclick={() => boundParagraphs[i].changeBackgroundToRed()}>
change
</button>
<button onclick={() => store.splice(store.indexOf(text), 1)}>
delete
</button>
</div>
{/each}
Loading