Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set deriveds as CLEAN if they are assigned to #15592

Merged
merged 7 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nervous-kids-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: set deriveds as `CLEAN` if they are assigned to
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/reactivity/deriveds.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function get_derived_parent_effect(derived) {
* @param {Derived} derived
* @returns {T}
*/
function execute_derived(derived) {
export function execute_derived(derived) {
var value;
var prev_active_effect = active_effect;

Expand Down
12 changes: 10 additions & 2 deletions packages/svelte/src/internal/client/reactivity/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ import {
UNOWNED,
MAYBE_DIRTY,
BLOCK_EFFECT,
ROOT_EFFECT,
EFFECT_IS_UPDATING
ROOT_EFFECT
} from '../constants.js';
import * as e from '../errors.js';
import { legacy_mode_flag, tracing_mode_flag } from '../../flags/index.js';
import { get_stack } from '../dev/tracing.js';
import { component_context, is_runes } from '../context.js';
import { proxy } from '../proxy.js';
import { execute_derived } from './deriveds.js';

export let inspect_effects = new Set();
export const old_values = new Map();
Expand Down Expand Up @@ -171,6 +171,14 @@ export function internal_set(source, value) {
}
}

if ((source.f & DERIVED) !== 0) {
// 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
if ((source.f & DIRTY) !== 0) {
execute_derived(/** @type {Derived} */ (source));
}
set_signal_status(source, (source.f & UNOWNED) === 0 ? CLEAN : MAYBE_DIRTY);
}

mark_reactions(source, DIRTY);

// It's possible that the current reaction might not have up-to-date dependencies
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
} from './constants.js';
import { flush_tasks } from './dom/task.js';
import { internal_set, old_values } from './reactivity/sources.js';
import { destroy_derived_effects, update_derived } from './reactivity/deriveds.js';
import { destroy_derived_effects, execute_derived, update_derived } from './reactivity/deriveds.js';
import * as e from './errors.js';
import { FILENAME } from '../../constants.js';
import { tracing_mode_flag } from '../flags/index.js';
Expand Down
32 changes: 32 additions & 0 deletions packages/svelte/tests/signals/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,38 @@ describe('signals', () => {
};
});

test("deriveds set after they are DIRTY doesn't get updated on get", () => {
return () => {
const a = state(0);
const b = derived(() => $.get(a));

set(b, 1);
assert.equal($.get(b), 1);

set(a, 2);
assert.equal($.get(b), 2);
set(b, 3);

assert.equal($.get(b), 3);
};
});

test("unowned deriveds set after they are DIRTY doesn't get updated on get", () => {
return () => {
const a = state(0);
const b = derived(() => $.get(a));
const c = derived(() => $.get(b));

set(b, 1);
assert.equal($.get(c), 1);

set(a, 2);

assert.equal($.get(b), 2);
assert.equal($.get(c), 2);
};
});

test('deriveds containing effects work correctly when used with untrack', () => {
return () => {
let a = render_effect(() => {});
Expand Down