Skip to content

Commit c538868

Browse files
committed
add some comments
1 parent 96bb197 commit c538868

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

packages/svelte/src/internal/client/runtime.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,10 +1433,13 @@ export function prop(props, key, flags, initial) {
14331433
return value === undefined ? /** @type {V} */ (initial) : value;
14341434
};
14351435

1436+
// easy mode — prop is never written to
14361437
if ((flags & PROPS_IS_UPDATED) === 0) {
14371438
return getter;
14381439
}
14391440

1441+
// intermediate mode — prop is written to, but the parent component had
1442+
// `bind:foo` which means we can just call `$$props.foo = value` directly
14401443
if (setter) {
14411444
return function (/** @type {V} */ value) {
14421445
if (arguments.length === 1) {
@@ -1448,41 +1451,51 @@ export function prop(props, key, flags, initial) {
14481451
};
14491452
}
14501453

1451-
var setting_from_child = false;
1452-
var last_set_from_child = false;
1454+
// hard mode. this is where it gets ugly — the value in the child should
1455+
// synchronize with the parent, but it should also be possible to temporarily
1456+
// set the value to something else locally. to make this work, we need to
1457+
// do a little dance.
1458+
var from_child = false;
1459+
var was_from_child = false;
1460+
14531461
var s = mutable_source(value);
14541462
var d = derived(() => {
1455-
const from_parent = getter();
1456-
const from_child = get(s);
1463+
var parent_value = getter();
1464+
var child_value = get(s);
14571465

1458-
if (setting_from_child) {
1459-
setting_from_child = false;
1460-
last_set_from_child = true;
1461-
return from_child;
1466+
if (from_child) {
1467+
from_child = false;
1468+
was_from_child = true;
1469+
return child_value;
14621470
}
14631471

1464-
last_set_from_child = false;
1465-
return (s.v = from_parent);
1472+
was_from_child = false;
1473+
return (s.v = parent_value);
14661474
});
14671475

14681476
if (!immutable) d.e = safe_equal;
14691477

14701478
return function (/** @type {V} */ value, mutation = false) {
1471-
const current = get(d);
1479+
var current = get(d);
14721480

14731481
// legacy nonsense — need to ensure the source is invalidated when necessary
14741482
if (is_signals_recorded) {
1475-
setting_from_child = last_set_from_child;
1483+
// set this so that we don't reset to the parent value if `d`
1484+
// is invalidated because of `invalidate_inner_signals` (rather
1485+
// than because the parent or child value changed)
1486+
from_child = was_from_child;
1487+
14761488
getter();
14771489
get(s);
14781490
}
14791491

14801492
if (arguments.length > 0) {
14811493
if (mutation || (immutable ? value !== current : safe_not_equal(value, current))) {
1482-
setting_from_child = true;
1494+
from_child = true;
14831495
set(s, mutation ? current : value);
14841496
get(d); // force a synchronisation immediately
14851497
}
1498+
14861499
return value;
14871500
}
14881501

0 commit comments

Comments
 (0)