Skip to content

fix: better handling of derived signals that have no dependencies #10558

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

Merged
merged 2 commits into from
Feb 20, 2024
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/witty-tomatoes-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: better handling of derived signals that have no dependencies
3 changes: 2 additions & 1 deletion packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,8 @@ function update_derived(signal, force_schedule) {
destroy_references(signal);
const value = execute_signal_fn(signal);
updating_derived = previous_updating_derived;
const status = current_skip_consumer || (signal.f & UNOWNED) !== 0 ? DIRTY : CLEAN;
const status =
(current_skip_consumer || (signal.f & UNOWNED) !== 0) && signal.d !== null ? DIRTY : CLEAN;
set_signal_status(signal, status);
const equals = /** @type {import('./types.js').EqualsFunctions} */ (signal.e);
if (!equals(value, signal.v)) {
Expand Down
21 changes: 21 additions & 0 deletions packages/svelte/tests/signals/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,27 @@ describe('signals', () => {
};
});

let no_deps = $.derived(() => {
return [];
});

test('two effects with an unowned derived that has no depedencies', () => {
const log: Array<Array<any>> = [];

$.render_effect(() => {
log.push($.get(no_deps));
});

$.render_effect(() => {
log.push($.get(no_deps));
});

return () => {
$.flushSync();
assert.deepEqual(log, [[], []]);
};
});

test('schedules rerun when writing to signal before reading it', (runes) => {
if (!runes) return () => {};

Expand Down