chore: make store initialization logic simpler (#12281)

* chore: make store initialization logic simpler

* simpler still

* add comment
pull/12284/head
Rich Harris 3 months ago committed by GitHub
parent 07b0088dab
commit 7e64e84479
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
chore: make store initialization logic simpler

@ -24,7 +24,6 @@ import {
UNOWNED,
MAYBE_DIRTY
} from '../constants.js';
import { UNINITIALIZED } from '../../../constants.js';
import * as e from '../errors.js';
let inspect_effects = new Set();
@ -84,14 +83,7 @@ export function mutate(source, value) {
* @returns {V}
*/
export function set(source, value) {
var initialized = source.v !== UNINITIALIZED;
if (
initialized &&
current_reaction !== null &&
is_runes() &&
(current_reaction.f & DERIVED) !== 0
) {
if (current_reaction !== null && is_runes() && (current_reaction.f & DERIVED) !== 0) {
e.state_unsafe_mutation();
}
@ -112,7 +104,6 @@ export function set(source, value) {
// We additionally want to skip this logic when initialising store sources
if (
is_runes() &&
initialized &&
current_effect !== null &&
(current_effect.f & CLEAN) !== 0 &&
(current_effect.f & BRANCH_EFFECT) === 0

@ -2,7 +2,6 @@
/** @import { Store } from '#shared' */
import { subscribe_to_store } from '../../../store/utils.js';
import { noop } from '../../shared/utils.js';
import { UNINITIALIZED } from '../../../constants.js';
import { get } from '../runtime.js';
import { teardown } from './effects.js';
import { mutable_source, set } from './sources.js';
@ -20,7 +19,7 @@ import { mutable_source, set } from './sources.js';
export function store_get(store, store_name, stores) {
const entry = (stores[store_name] ??= {
store: null,
source: mutable_source(UNINITIALIZED),
source: mutable_source(undefined),
unsubscribe: noop
});
@ -32,7 +31,18 @@ export function store_get(store, store_name, stores) {
set(entry.source, undefined);
entry.unsubscribe = noop;
} else {
entry.unsubscribe = subscribe_to_store(store, (v) => set(entry.source, v));
var initial = true;
entry.unsubscribe = subscribe_to_store(store, (v) => {
if (initial) {
// if the first time the store value is read is inside a derived,
// we will hit the `state_unsafe_mutation` error if we `set` the value
entry.source.v = v;
initial = false;
} else {
set(entry.source, v);
}
});
}
}

Loading…
Cancel
Save