Skip to content

Commit

Permalink
alternative approach to mutating props (#10788)
Browse files Browse the repository at this point in the history
Co-authored-by: Rich Harris <[email protected]>
  • Loading branch information
Rich-Harris and Rich-Harris authored Mar 13, 2024
1 parent 8a7e540 commit f0380fd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
14 changes: 8 additions & 6 deletions packages/svelte/src/compiler/phases/3-transform/client/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 4 additions & 7 deletions packages/svelte/src/internal/client/reactivity/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down

0 comments on commit f0380fd

Please sign in to comment.