Skip to content
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/rich-garlics-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: improve handled of unowned derived signals
18 changes: 15 additions & 3 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,21 @@ export function check_dirtiness(reaction) {
// is also dirty.
var version = dependency.version;

if (is_unowned && version > /** @type {import('#client').Derived} */ (reaction).version) {
/** @type {import('#client').Derived} */ (reaction).version = version;
return true;
if (is_unowned) {
if (version > /** @type {import('#client').Derived} */ (reaction).version) {
/** @type {import('#client').Derived} */ (reaction).version = version;
return true;
} else if (!current_skip_reaction && !dependency?.reactions?.includes(reaction)) {
// If we are working with an unowned signal as part of an effect (due to !current_skip_reaction)
// and the version hasn't changed, we still need to check that this reaction
// if linked to the dependency source – otherwise future updates will not be caught.
var reactions = dependency.reactions;
if (reactions === null) {
dependency.reactions = [reaction];
} else {
reactions.push(reaction);
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { test } from '../../test';

export default test({
async test({ assert, target }) {
// The test has a bunch of queueMicrotasks
await Promise.resolve();
await Promise.resolve();
await Promise.resolve();

assert.htmlEqual(target.innerHTML, `<div>Zeeba Neighba</div>`);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<script context="module">
export class Thing {
data = $state();

subscribe() {
queueMicrotask(() => {
this.data = {
name: `Zeeba Neighba`,
};
});
}

name = $derived(this.data?.name);
}

export class Things {
thing = $state();

subscribe() {
queueMicrotask(() => {
this.thing = new Thing();
this.thing.subscribe();
this.thing.name;
});
}
}
</script>

<script>
let model = new Things();
$effect(() => model.subscribe());
</script>

<div>{model.thing?.name}</div>