Skip to content

Commit

Permalink
fix: address regression with untrack (#15079)
Browse files Browse the repository at this point in the history
* fix: address regression with untrack

* add test
  • Loading branch information
trueadm authored Jan 21, 2025
1 parent c75f1f5 commit 09510c8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/ninety-chefs-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: address regression with untrack
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/reactivity/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export function mutable_state(v, immutable = false) {
*/
/*#__NO_SIDE_EFFECTS__*/
function push_derived_source(source) {
if (active_reaction !== null && (active_reaction.f & DERIVED) !== 0) {
if (active_reaction !== null && !untracking && (active_reaction.f & DERIVED) !== 0) {
if (derived_sources === null) {
set_derived_sources([source]);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
html: `3`
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<script module>
import { untrack } from 'svelte'
import { SvelteMap } from 'svelte/reactivity'
class Foo {
id
updateTime = $state(Date.now())
constructor(id) {
this.id = id
}
}
class Store {
cache = new SvelteMap()
ids = $state([1, 2, 3])
getOrDefault(id) {
let ret = this.cache.get(id)
if (ret) {
return ret
}
ret = untrack(() => {
ret = new Foo(id)
this.cache.set(id, ret)
return ret
})
this.cache.get(id)
return ret
}
get values() {
return this.ids.map(id => this.getOrDefault(id)).sort((a, b) => b.updateTime - a.updateTime)
}
}
const store = new Store()
</script>

<script>
const test = $derived.by(() => store.values.length)
</script>

{test}

0 comments on commit 09510c8

Please sign in to comment.