Skip to content

Commit 4a89898

Browse files
committed
fix
1 parent f32818c commit 4a89898

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

packages/svelte/src/internal/client/reactivity/props.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
/** @import { ComponentContext } from '#client' */
12
/** @import { Derived, Source } from './types.js' */
23
import { DEV } from 'esm-env';
34
import {
45
PROPS_IS_BINDABLE,
56
PROPS_IS_IMMUTABLE,
67
PROPS_IS_LAZY_INITIAL,
78
PROPS_IS_RUNES,
8-
PROPS_IS_UPDATED
9+
PROPS_IS_UPDATED,
10+
UNINITIALIZED
911
} from '../../../constants.js';
1012
import { get_descriptor, is_function } from '../../shared/utils.js';
1113
import { set, source, update } from './sources.js';

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
} from './context.js';
4141
import { handle_error, invoke_error_boundary } from './error-handling.js';
4242
import { snapshot } from '../shared/clone.js';
43+
import { UNINITIALIZED } from '../../constants.js';
4344

4445
let is_flushing = false;
4546

@@ -818,7 +819,13 @@ export function get(signal) {
818819
if (is_derived) {
819820
derived = /** @type {Derived} */ (signal);
820821

821-
var value = (derived.f & CLEAN) !== 0 ? execute_derived(derived) : derived.v;
822+
var value = derived.v;
823+
824+
// if the derived is dirty, or depends on the values that just changed, re-execute
825+
if ((derived.f & CLEAN) !== 0 || depends_on_old_values(derived)) {
826+
value = execute_derived(derived);
827+
}
828+
822829
old_values.set(derived, value);
823830

824831
return value;
@@ -828,6 +835,24 @@ export function get(signal) {
828835
return signal.v;
829836
}
830837

838+
/** @param {Derived} derived */
839+
function depends_on_old_values(derived) {
840+
if (derived.v === UNINITIALIZED) return true; // we don't know, so assume the worst
841+
if (derived.deps === null) return false;
842+
843+
for (const dep of derived.deps) {
844+
if (old_values.has(dep)) {
845+
return true;
846+
}
847+
848+
if ((dep.f & DERIVED) !== 0 && depends_on_old_values(/** @type {Derived} */ (dep))) {
849+
return true;
850+
}
851+
}
852+
853+
return false;
854+
}
855+
831856
/**
832857
* Like `get`, but checks for `undefined`. Used for `var` declarations because they can be accessed before being declared
833858
* @template V

0 commit comments

Comments
 (0)