Skip to content

Commit 7e64e84

Browse files
authored
chore: make store initialization logic simpler (#12281)
* chore: make store initialization logic simpler * simpler still * add comment
1 parent 07b0088 commit 7e64e84

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

.changeset/fair-beers-help.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
chore: make store initialization logic simpler

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
UNOWNED,
2525
MAYBE_DIRTY
2626
} from '../constants.js';
27-
import { UNINITIALIZED } from '../../../constants.js';
2827
import * as e from '../errors.js';
2928

3029
let inspect_effects = new Set();
@@ -84,14 +83,7 @@ export function mutate(source, value) {
8483
* @returns {V}
8584
*/
8685
export function set(source, value) {
87-
var initialized = source.v !== UNINITIALIZED;
88-
89-
if (
90-
initialized &&
91-
current_reaction !== null &&
92-
is_runes() &&
93-
(current_reaction.f & DERIVED) !== 0
94-
) {
86+
if (current_reaction !== null && is_runes() && (current_reaction.f & DERIVED) !== 0) {
9587
e.state_unsafe_mutation();
9688
}
9789

@@ -112,7 +104,6 @@ export function set(source, value) {
112104
// We additionally want to skip this logic when initialising store sources
113105
if (
114106
is_runes() &&
115-
initialized &&
116107
current_effect !== null &&
117108
(current_effect.f & CLEAN) !== 0 &&
118109
(current_effect.f & BRANCH_EFFECT) === 0

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
/** @import { Store } from '#shared' */
33
import { subscribe_to_store } from '../../../store/utils.js';
44
import { noop } from '../../shared/utils.js';
5-
import { UNINITIALIZED } from '../../../constants.js';
65
import { get } from '../runtime.js';
76
import { teardown } from './effects.js';
87
import { mutable_source, set } from './sources.js';
@@ -20,7 +19,7 @@ import { mutable_source, set } from './sources.js';
2019
export function store_get(store, store_name, stores) {
2120
const entry = (stores[store_name] ??= {
2221
store: null,
23-
source: mutable_source(UNINITIALIZED),
22+
source: mutable_source(undefined),
2423
unsubscribe: noop
2524
});
2625

@@ -32,7 +31,18 @@ export function store_get(store, store_name, stores) {
3231
set(entry.source, undefined);
3332
entry.unsubscribe = noop;
3433
} else {
35-
entry.unsubscribe = subscribe_to_store(store, (v) => set(entry.source, v));
34+
var initial = true;
35+
36+
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
40+
entry.source.v = v;
41+
initial = false;
42+
} else {
43+
set(entry.source, v);
44+
}
45+
});
3646
}
3747
}
3848

0 commit comments

Comments
 (0)