fix: address regression with untrack (#15079)

* fix: address regression with untrack

* add test
pull/15080/head
Dominic Gannaway 8 months ago committed by GitHub
parent c75f1f5f2f
commit 09510c8c5c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: address regression with untrack

@ -116,7 +116,7 @@ export function mutable_state(v, immutable = false) {
*/ */
/*#__NO_SIDE_EFFECTS__*/ /*#__NO_SIDE_EFFECTS__*/
function push_derived_source(source) { 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) { if (derived_sources === null) {
set_derived_sources([source]); set_derived_sources([source]);
} else { } else {

@ -0,0 +1,5 @@
import { test } from '../../test';
export default test({
html: `3`
});

@ -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…
Cancel
Save