Skip to content

fix: improve bind:this support around proxyied state #10732

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 3 commits into from
Mar 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/blue-ants-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: improve bind:this support around proxyied state
16 changes: 14 additions & 2 deletions packages/svelte/src/internal/client/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import { run } from '../common.js';
import { bind_transition, trigger_transitions } from './transitions.js';
import { mutable_source, source, set } from './reactivity/sources.js';
import { safe_equal, safe_not_equal } from './reactivity/equality.js';
import { STATE_SYMBOL } from './constants.js';

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

/**
* @param {any} bound_value
* @param {Element} element_or_component
* @returns {boolean}
*/
function is_bound_this(bound_value, element_or_component) {
// Find the original target if the value is proxied.
const proxy_target = bound_value && bound_value[STATE_SYMBOL]?.t;
return bound_value === element_or_component || proxy_target === element_or_component;
}

/**
* @param {Element} element_or_component
* @param {(value: unknown, ...parts: unknown[]) => void} update
Expand All @@ -1360,7 +1372,7 @@ export function bind_this(element_or_component, update, get_value, get_parts) {
update(element_or_component, ...parts);
// If this is an effect rerun (cause: each block context changes), then nullfiy the binding at
// the previous position if it isn't already taken over by a different effect.
if (old_parts && get_value(...old_parts) === element_or_component) {
if (old_parts && is_bound_this(get_value(...old_parts), element_or_component)) {
update(null, ...old_parts);
}
}
Expand All @@ -1374,7 +1386,7 @@ export function bind_this(element_or_component, update, get_value, get_parts) {
// 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.
effect(() => {
if (get_value(...parts) === element_or_component) {
if (parts && is_bound_this(get_value(...parts), element_or_component)) {
update(null, ...parts);
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script>
export const a = {};
</script>
<div>Hello world</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { flushSync } from 'svelte';
import { test } from '../../test';
import { log } from './log.js';

export default test({
before_test: () => {
log.length = 0;
},

html: `<button>Toggle</button><div>Hello\nworld</div>`,

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

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

assert.htmlEqual(target.innerHTML, `<button>Toggle</button>`);

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

assert.htmlEqual(target.innerHTML, `<button>Toggle</button><div>Hello\nworld</div>`);

assert.deepEqual(log, [{ a: {} }, null, { a: {} }]);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** @type {any[]} */
export const log = [];
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script>
import { log } from './log.js';
import Component from './Component.svelte';

let type = $state(Component)
let elem = $state()

$effect(() => {
log.push(elem);
});
</script>

<button onclick={() => {
if (!type) {
type = Component
} else {
type = false
}
}}>Toggle</button>

<svelte:component bind:this={elem} this={type}>
Content
</svelte:component>