From f0380fd52f3e29c5c0b85c581fd7d6b93701c2ab Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 13 Mar 2024 10:06:23 -0400 Subject: [PATCH] alternative approach to mutating props (#10788) Co-authored-by: Rich Harris --- .../compiler/phases/3-transform/client/utils.js | 14 ++++++++------ .../svelte/src/internal/client/reactivity/props.js | 11 ++++------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/packages/svelte/src/compiler/phases/3-transform/client/utils.js b/packages/svelte/src/compiler/phases/3-transform/client/utils.js index 91a56def4589..d93f8f5b92c8 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/utils.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/utils.js @@ -470,12 +470,14 @@ export function serialize_set_binding(node, context, fallback, options) { if (binding.kind === 'prop') { return b.call( left, - b.assignment( - node.operator, - /** @type {import('estree').Pattern} */ (visit(node.left)), - value - ), - b.literal(true) + b.sequence([ + b.assignment( + node.operator, + /** @type {import('estree').Pattern} */ (visit(node.left)), + value + ), + b.call(left) + ]) ); } else { return b.call( diff --git a/packages/svelte/src/internal/client/reactivity/props.js b/packages/svelte/src/internal/client/reactivity/props.js index 72123fb521d1..586f0e64b1b6 100644 --- a/packages/svelte/src/internal/client/reactivity/props.js +++ b/packages/svelte/src/internal/client/reactivity/props.js @@ -175,13 +175,10 @@ export function prop(props, key, flags, initial) { // intermediate mode — prop is written to, but the parent component had // `bind:foo` which means we can just call `$$props.foo = value` directly if (setter) { - return function (/** @type {V} */ value, mutation = false) { + return function (/** @type {V} */ value) { if (arguments.length === 1) { /** @type {Function} */ (setter)(value); return value; - } else if (mutation) { - /** @type {Function} */ (setter)(getter()); - return value; } else { return getter(); } @@ -213,7 +210,7 @@ export function prop(props, key, flags, initial) { if (!immutable) current_value.equals = safe_equals; - return function (/** @type {V} */ value, mutation = false) { + return function (/** @type {V} */ value) { var current = get(current_value); // legacy nonsense — need to ensure the source is invalidated when necessary @@ -229,9 +226,9 @@ export function prop(props, key, flags, initial) { } if (arguments.length > 0) { - if (mutation || (immutable ? value !== current : safe_not_equal(value, current))) { + if (!current_value.equals(value)) { from_child = true; - set(inner_current_value, mutation ? current : value); + set(inner_current_value, value); get(current_value); // force a synchronisation immediately }