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: address regression with untrack #15079

Merged
merged 2 commits into from
Jan 21, 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/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}
Loading