Skip to content

Commit a101b75

Browse files
committed
fix another case
1 parent 5a9c790 commit a101b75

File tree

3 files changed

+25
-17
lines changed
  • packages/svelte

3 files changed

+25
-17
lines changed

packages/svelte/src/internal/client/reactivity/store.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,19 @@ export function store_get(store, store_name, stores) {
3131
set(entry.source, undefined);
3232
entry.unsubscribe = noop;
3333
} else {
34-
var initial = true;
34+
var is_synchronous_callback = true;
3535

3636
entry.unsubscribe = subscribe_to_store(store, (v) => {
37-
if (initial) {
38-
// if the first time the store value is read is inside a derived,
39-
// we will hit the `state_unsafe_mutation` error if we `set` the value
37+
if (is_synchronous_callback) {
38+
// If the first updates to the store value (possibly multiple of them) are synchronously
39+
// inside a derived, we will hit the `state_unsafe_mutation` error if we `set` the value
4040
entry.source.v = v;
41-
initial = false;
4241
} else {
4342
set(entry.source, v);
4443
}
4544
});
4645

47-
// also set initial to false right afterwards in case the store subscription callback is async,
48-
// in which case the `state_unsafe_mutation` check is not relevant anymore and we would instead
49-
// not get notified of the store value change
50-
initial = false;
46+
is_synchronous_callback = false;
5147
}
5248
}
5349

packages/svelte/tests/runtime-runes/samples/store-async-first-value/main.svelte

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
};
1212
}
1313
14-
const value = store();
15-
const derivedValue = $derived($value);
14+
const value1 = store();
15+
const value2 = store();
16+
const derivedValue = $derived($value1);
1617
</script>
1718

18-
{$value} / {derivedValue}
19+
{$value2} / {derivedValue}
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
<script>
2-
import { writable } from "svelte/store";
3-
let store = writable('store');
4-
let name = $derived($store); // store signal is updated during reading this, which normally errors, but shouldn't for stores
1+
<script>
2+
import { writable } from 'svelte/store';
3+
4+
let store1 = writable('store');
5+
let store2 = {
6+
subscribe: (cb) => {
7+
cb('...');
8+
cb('Hello');
9+
return () => {};
10+
}
11+
};
12+
13+
// store signal is updated during reading this, which normally errors, but shouldn't for stores
14+
let name = $derived($store1);
15+
let hello = $derived($store2);
516
</script>
617

7-
<h1>Hello {name}</h1>
18+
<h1>{hello} {name}</h1>

0 commit comments

Comments
 (0)