Skip to content

Commit b56c71a

Browse files
committed
fix: execute deriveds eagerly when they are set if DIRTY
1 parent 097fbb4 commit b56c71a

File tree

3 files changed

+18
-25
lines changed

3 files changed

+18
-25
lines changed

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,17 @@ export const DISCONNECTED = 1 << 9;
1010
export const CLEAN = 1 << 10;
1111
export const DIRTY = 1 << 11;
1212
export const MAYBE_DIRTY = 1 << 12;
13-
export const ASSIGNED_DERIVED = 1 << 13;
14-
export const INERT = 1 << 14;
15-
export const DESTROYED = 1 << 15;
16-
export const EFFECT_RAN = 1 << 16;
13+
export const INERT = 1 << 13;
14+
export const DESTROYED = 1 << 14;
15+
export const EFFECT_RAN = 1 << 15;
1716
/** 'Transparent' effects do not create a transition boundary */
18-
export const EFFECT_TRANSPARENT = 1 << 17;
17+
export const EFFECT_TRANSPARENT = 1 << 16;
1918
/** Svelte 4 legacy mode props need to be handled with deriveds and be recognized elsewhere, hence the dedicated flag */
20-
export const LEGACY_DERIVED_PROP = 1 << 18;
21-
export const INSPECT_EFFECT = 1 << 19;
22-
export const HEAD_EFFECT = 1 << 20;
23-
export const EFFECT_HAS_DERIVED = 1 << 21;
24-
export const EFFECT_IS_UPDATING = 1 << 22;
19+
export const LEGACY_DERIVED_PROP = 1 << 17;
20+
export const INSPECT_EFFECT = 1 << 18;
21+
export const HEAD_EFFECT = 1 << 19;
22+
export const EFFECT_HAS_DERIVED = 1 << 20;
23+
export const EFFECT_IS_UPDATING = 1 << 21;
2524

2625
export const STATE_SYMBOL = Symbol('$state');
2726
export const STATE_SYMBOL_METADATA = Symbol('$state metadata');

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ import {
2828
UNOWNED,
2929
MAYBE_DIRTY,
3030
BLOCK_EFFECT,
31-
ROOT_EFFECT,
32-
ASSIGNED_DERIVED
31+
ROOT_EFFECT
3332
} from '../constants.js';
3433
import * as e from '../errors.js';
3534
import { legacy_mode_flag, tracing_mode_flag } from '../../flags/index.js';
3635
import { get_stack } from '../dev/tracing.js';
3736
import { component_context, is_runes } from '../context.js';
3837
import { proxy } from '../proxy.js';
38+
import { execute_derived } from './deriveds.js';
3939

4040
export let inspect_effects = new Set();
4141
export const old_values = new Map();
@@ -172,11 +172,11 @@ export function internal_set(source, value) {
172172
}
173173

174174
if ((source.f & DERIVED) !== 0) {
175-
// if we are assigning a derived we set it to clean but we also
176-
// keep the assigned derived flag so that we can read the dependencies
177-
// in the get function
178-
var flags = (source.f & UNOWNED) === 0 ? CLEAN : MAYBE_DIRTY;
179-
set_signal_status(source, flags | ASSIGNED_DERIVED);
175+
// if we are assigning to a dirty derived we set it to clean/maybe dirty but we also eagerly execute it to track the dependencies
176+
if ((source.f & DIRTY) !== 0) {
177+
execute_derived(/** @type {Derived} */ (source));
178+
}
179+
set_signal_status(source, (source.f & UNOWNED) === 0 ? CLEAN : MAYBE_DIRTY);
180180
}
181181

182182
mark_reactions(source, DIRTY);

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ import {
2323
LEGACY_DERIVED_PROP,
2424
DISCONNECTED,
2525
BOUNDARY_EFFECT,
26-
EFFECT_IS_UPDATING,
27-
ASSIGNED_DERIVED
26+
EFFECT_IS_UPDATING
2827
} from './constants.js';
2928
import { flush_tasks } from './dom/task.js';
3029
import { internal_set, old_values } from './reactivity/sources.js';
@@ -927,11 +926,6 @@ export function get(signal) {
927926

928927
if (check_dirtiness(derived)) {
929928
update_derived(derived);
930-
} else if ((derived.f & ASSIGNED_DERIVED) !== 0) {
931-
// if the derived is clean but has been assigned a new value, then we need to
932-
// still invoke the derived function and update the dependencies or else
933-
// it will not depend on the original source
934-
execute_derived(derived);
935929
}
936930
}
937931

@@ -1049,7 +1043,7 @@ export function untrack(fn) {
10491043
}
10501044
}
10511045

1052-
const STATUS_MASK = ~(DIRTY | MAYBE_DIRTY | CLEAN | ASSIGNED_DERIVED);
1046+
const STATUS_MASK = ~(DIRTY | MAYBE_DIRTY | CLEAN);
10531047

10541048
/**
10551049
* @param {Signal} signal

0 commit comments

Comments
 (0)