Skip to content

feat: allow state created in deriveds/effects to be written/read locally without self-invalidation #15553

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

Merged
merged 23 commits into from
Mar 20, 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/dirty-pianos-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': minor
---

feat: allow state created in deriveds/effects to be written/read locally without self-invalidation
6 changes: 0 additions & 6 deletions documentation/docs/98-reference/.generated/client-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,6 @@ Property descriptors defined on `$state` objects must contain `value` and always
Cannot set prototype of `$state` object
```

### state_unsafe_local_read

```
Reading state that was created inside the same derived is forbidden. Consider using `untrack` to read locally created state
```

### state_unsafe_mutation

```
Expand Down
4 changes: 0 additions & 4 deletions packages/svelte/messages/client-errors/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ See the [migration guide](/docs/svelte/v5-migration-guide#Components-are-no-long

> Cannot set prototype of `$state` object

## state_unsafe_local_read

> Reading state that was created inside the same derived is forbidden. Consider using `untrack` to read locally created state

## state_unsafe_mutation

> Updating state inside a derived or a template expression is forbidden. If the value should not be reactive, declare it without `$state`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,10 @@ export function client_component(analysis, options) {
for (const [name, binding] of analysis.instance.scope.declarations) {
if (binding.kind === 'legacy_reactive') {
legacy_reactive_declarations.push(
b.const(name, b.call('$.mutable_state', undefined, analysis.immutable ? b.true : undefined))
b.const(
name,
b.call('$.mutable_source', undefined, analysis.immutable ? b.true : undefined)
)
);
}
if (binding.kind === 'store_sub') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ function create_state_declarators(declarator, { scope, analysis }, value) {
return [
b.declarator(
declarator.id,
b.call('$.mutable_state', value, analysis.immutable ? b.true : undefined)
b.call('$.mutable_source', value, analysis.immutable ? b.true : undefined)
)
];
}
Expand All @@ -314,7 +314,7 @@ function create_state_declarators(declarator, { scope, analysis }, value) {
return b.declarator(
path.node,
binding?.kind === 'state'
? b.call('$.mutable_state', value, analysis.immutable ? b.true : undefined)
? b.call('$.mutable_source', value, analysis.immutable ? b.true : undefined)
: value
);
})
Expand Down
1 change: 1 addition & 0 deletions packages/svelte/src/internal/client/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const LEGACY_DERIVED_PROP = 1 << 17;
export const INSPECT_EFFECT = 1 << 18;
export const HEAD_EFFECT = 1 << 19;
export const EFFECT_HAS_DERIVED = 1 << 20;
export const EFFECT_IS_UPDATING = 1 << 21;

export const STATE_SYMBOL = Symbol('$state');
export const STATE_SYMBOL_METADATA = Symbol('$state metadata');
Expand Down
15 changes: 0 additions & 15 deletions packages/svelte/src/internal/client/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,21 +307,6 @@ export function state_prototype_fixed() {
}
}

/**
* Reading state that was created inside the same derived is forbidden. Consider using `untrack` to read locally created state
* @returns {never}
*/
export function state_unsafe_local_read() {
if (DEV) {
const error = new Error(`state_unsafe_local_read\nReading state that was created inside the same derived is forbidden. Consider using \`untrack\` to read locally created state\nhttps://svelte.dev/e/state_unsafe_local_read`);

error.name = 'Svelte error';
throw error;
} else {
throw new Error(`https://svelte.dev/e/state_unsafe_local_read`);
}
}

/**
* Updating state inside a derived or a template expression is forbidden. If the value should not be reactive, declare it without `$state`
* @returns {never}
Expand Down
9 changes: 8 additions & 1 deletion packages/svelte/src/internal/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,14 @@ export {
user_effect,
user_pre_effect
} from './reactivity/effects.js';
export { mutable_state, mutate, set, state, update, update_pre } from './reactivity/sources.js';
export {
mutable_source,
mutate,
set,
source as state,
update,
update_pre
} from './reactivity/sources.js';
export {
prop,
rest_props,
Expand Down
67 changes: 53 additions & 14 deletions packages/svelte/src/internal/client/proxy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @import { ProxyMetadata, Source } from '#client' */
import { DEV } from 'esm-env';
import { get, active_effect } from './runtime.js';
import { get, active_effect, active_reaction, set_active_reaction } from './runtime.js';
import { component_context } from './context.js';
import {
array_prototype,
Expand All @@ -17,14 +17,16 @@ import * as e from './errors.js';
import { get_stack } from './dev/tracing.js';
import { tracing_mode_flag } from '../flags/index.js';

/** @type {ProxyMetadata | null} */
var parent_metadata = null;

/**
* @template T
* @param {T} value
* @param {ProxyMetadata | null} [parent]
* @param {Source<T>} [prev] dev mode only
* @returns {T}
*/
export function proxy(value, parent = null, prev) {
export function proxy(value, prev) {
// if non-proxyable, or is already a proxy, return `value`
if (typeof value !== 'object' || value === null || STATE_SYMBOL in value) {
return value;
Expand All @@ -42,6 +44,31 @@ export function proxy(value, parent = null, prev) {
var version = source(0);

var stack = DEV && tracing_mode_flag ? get_stack('CreatedAt') : null;
var reaction = active_reaction;

/**
* @template T
* @param {() => T} fn
*/
var with_parent = (fn) => {
var previous_reaction = active_reaction;
set_active_reaction(reaction);

/** @type {T} */
var result;

if (DEV) {
var previous_metadata = parent_metadata;
parent_metadata = metadata;
result = fn();
parent_metadata = previous_metadata;
} else {
result = fn();
}

set_active_reaction(previous_reaction);
return result;
};

if (is_proxied_array) {
// We need to create the length source eagerly to ensure that
Expand All @@ -54,7 +81,7 @@ export function proxy(value, parent = null, prev) {

if (DEV) {
metadata = {
parent,
parent: parent_metadata,
owners: null
};

Expand All @@ -66,7 +93,7 @@ export function proxy(value, parent = null, prev) {
metadata.owners = prev_owners ? new Set(prev_owners) : null;
} else {
metadata.owners =
parent === null
parent_metadata === null
? component_context !== null
? new Set([component_context.function])
: null
Expand All @@ -92,10 +119,13 @@ export function proxy(value, parent = null, prev) {
var s = sources.get(prop);

if (s === undefined) {
s = source(descriptor.value, stack);
s = with_parent(() => source(descriptor.value, stack));
sources.set(prop, s);
} else {
set(s, proxy(descriptor.value, metadata));
set(
s,
with_parent(() => proxy(descriptor.value))
);
}

return true;
Expand All @@ -106,7 +136,10 @@ export function proxy(value, parent = null, prev) {

if (s === undefined) {
if (prop in target) {
sources.set(prop, source(UNINITIALIZED, stack));
sources.set(
prop,
with_parent(() => source(UNINITIALIZED, stack))
);
}
} else {
// When working with arrays, we need to also ensure we update the length when removing
Expand Down Expand Up @@ -140,7 +173,7 @@ export function proxy(value, parent = null, prev) {

// create a source, but only if it's an own property and not a prototype property
if (s === undefined && (!exists || get_descriptor(target, prop)?.writable)) {
s = source(proxy(exists ? target[prop] : UNINITIALIZED, metadata), stack);
s = with_parent(() => source(proxy(exists ? target[prop] : UNINITIALIZED), stack));
sources.set(prop, s);
}

Expand Down Expand Up @@ -208,7 +241,7 @@ export function proxy(value, parent = null, prev) {
(active_effect !== null && (!has || get_descriptor(target, prop)?.writable))
) {
if (s === undefined) {
s = source(has ? proxy(target[prop], metadata) : UNINITIALIZED, stack);
s = with_parent(() => source(has ? proxy(target[prop]) : UNINITIALIZED, stack));
sources.set(prop, s);
}

Expand All @@ -235,7 +268,7 @@ export function proxy(value, parent = null, prev) {
// If the item exists in the original, we need to create a uninitialized source,
// else a later read of the property would result in a source being created with
// the value of the original item at that index.
other_s = source(UNINITIALIZED, stack);
other_s = with_parent(() => source(UNINITIALIZED, stack));
sources.set(i + '', other_s);
}
}
Expand All @@ -247,13 +280,19 @@ export function proxy(value, parent = null, prev) {
// object property before writing to that property.
if (s === undefined) {
if (!has || get_descriptor(target, prop)?.writable) {
s = source(undefined, stack);
set(s, proxy(value, metadata));
s = with_parent(() => source(undefined, stack));
set(
s,
with_parent(() => proxy(value))
);
sources.set(prop, s);
}
} else {
has = s.v !== UNINITIALIZED;
set(s, proxy(value, metadata));
set(
s,
with_parent(() => proxy(value))
);
}

if (DEV) {
Expand Down
57 changes: 15 additions & 42 deletions packages/svelte/src/internal/client/reactivity/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
untrack,
increment_write_version,
update_effect,
derived_sources,
set_derived_sources,
reaction_sources,
set_reaction_sources,
check_dirtiness,
untracking,
is_destroying_effect
Expand All @@ -27,7 +27,8 @@ import {
UNOWNED,
MAYBE_DIRTY,
BLOCK_EFFECT,
ROOT_EFFECT
ROOT_EFFECT,
EFFECT_IS_UPDATING
} from '../constants.js';
import * as e from '../errors.js';
import { legacy_mode_flag, tracing_mode_flag } from '../../flags/index.js';
Expand All @@ -51,6 +52,7 @@ export function set_inspect_effects(v) {
* @param {Error | null} [stack]
* @returns {Source<V>}
*/
// TODO rename this to `state` throughout the codebase
export function source(v, stack) {
/** @type {Value} */
var signal = {
Expand All @@ -62,6 +64,14 @@ export function source(v, stack) {
wv: 0
};

if (active_reaction !== null && active_reaction.f & EFFECT_IS_UPDATING) {
if (reaction_sources === null) {
set_reaction_sources([signal]);
} else {
reaction_sources.push(signal);
}
}

if (DEV && tracing_mode_flag) {
signal.created = stack ?? get_stack('CreatedAt');
signal.debug = null;
Expand All @@ -70,14 +80,6 @@ export function source(v, stack) {
return signal;
}

/**
* @template V
* @param {V} v
*/
export function state(v) {
return push_derived_source(source(v));
}

/**
* @template V
* @param {V} initial_value
Expand All @@ -100,33 +102,6 @@ export function mutable_source(initial_value, immutable = false) {
return s;
}

/**
* @template V
* @param {V} v
* @param {boolean} [immutable]
* @returns {Source<V>}
*/
export function mutable_state(v, immutable = false) {
return push_derived_source(mutable_source(v, immutable));
}

/**
* @template V
* @param {Source<V>} source
*/
/*#__NO_SIDE_EFFECTS__*/
function push_derived_source(source) {
if (active_reaction !== null && !untracking && (active_reaction.f & DERIVED) !== 0) {
if (derived_sources === null) {
set_derived_sources([source]);
} else {
derived_sources.push(source);
}
}

return source;
}

/**
* @template V
* @param {Value<V>} source
Expand All @@ -153,14 +128,12 @@ export function set(source, value, should_proxy = false) {
!untracking &&
is_runes() &&
(active_reaction.f & (DERIVED | BLOCK_EFFECT)) !== 0 &&
// If the source was created locally within the current derived, then
// we allow the mutation.
(derived_sources === null || !derived_sources.includes(source))
!reaction_sources?.includes(source)
) {
e.state_unsafe_mutation();
}

let new_value = should_proxy ? proxy(value, null, source) : value;
let new_value = should_proxy ? proxy(value, source) : value;

return internal_set(source, new_value);
}
Expand Down
Loading