Skip to content

Commit e407e8b

Browse files
committed
remove state_unsafe_local_read error
1 parent 550a064 commit e407e8b

File tree

7 files changed

+15
-79
lines changed

7 files changed

+15
-79
lines changed

documentation/docs/98-reference/.generated/client-errors.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,6 @@ Property descriptors defined on `$state` objects must contain `value` and always
122122
Cannot set prototype of `$state` object
123123
```
124124

125-
### state_unsafe_local_read
126-
127-
```
128-
Reading state that was created inside the same derived is forbidden. Consider using `untrack` to read locally created state
129-
```
130-
131125
### state_unsafe_mutation
132126

133127
```

packages/svelte/messages/client-errors/errors.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ See the [migration guide](/docs/svelte/v5-migration-guide#Components-are-no-long
8080

8181
> Cannot set prototype of `$state` object
8282
83-
## state_unsafe_local_read
84-
85-
> Reading state that was created inside the same derived is forbidden. Consider using `untrack` to read locally created state
86-
8783
## state_unsafe_mutation
8884

8985
> Updating state inside a derived or a template expression is forbidden. If the value should not be reactive, declare it without `$state`

packages/svelte/src/internal/client/errors.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -307,21 +307,6 @@ export function state_prototype_fixed() {
307307
}
308308
}
309309

310-
/**
311-
* Reading state that was created inside the same derived is forbidden. Consider using `untrack` to read locally created state
312-
* @returns {never}
313-
*/
314-
export function state_unsafe_local_read() {
315-
if (DEV) {
316-
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`);
317-
318-
error.name = 'Svelte error';
319-
throw error;
320-
} else {
321-
throw new Error(`https://svelte.dev/e/state_unsafe_local_read`);
322-
}
323-
}
324-
325310
/**
326311
* Updating state inside a derived or a template expression is forbidden. If the value should not be reactive, declare it without `$state`
327312
* @returns {never}

packages/svelte/src/internal/client/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,14 @@ export {
113113
user_effect,
114114
user_pre_effect
115115
} from './reactivity/effects.js';
116-
export { mutable_source, mutate, set, state, update, update_pre } from './reactivity/sources.js';
116+
export {
117+
mutable_source,
118+
mutate,
119+
set,
120+
source as state,
121+
update,
122+
update_pre
123+
} from './reactivity/sources.js';
117124
export {
118125
prop,
119126
rest_props,

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

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import {
1111
untrack,
1212
increment_write_version,
1313
update_effect,
14-
derived_sources,
15-
set_derived_sources,
1614
check_dirtiness,
1715
untracking,
1816
is_destroying_effect
@@ -51,6 +49,7 @@ export function set_inspect_effects(v) {
5149
* @param {Error | null} [stack]
5250
* @returns {Source<V>}
5351
*/
52+
// TODO rename this to `state` throughout the codebase
5453
export function source(v, stack) {
5554
/** @type {Value} */
5655
var signal = {
@@ -71,25 +70,6 @@ export function source(v, stack) {
7170
return signal;
7271
}
7372

74-
/**
75-
* @template V
76-
* @param {V} v
77-
*/
78-
export function state(v) {
79-
var s = source(v);
80-
81-
// TODO maybe we make this dev-only?
82-
if (active_reaction !== null && !untracking && (active_reaction.f & DERIVED) !== 0) {
83-
if (derived_sources === null) {
84-
set_derived_sources([s]);
85-
} else {
86-
derived_sources.push(s);
87-
}
88-
}
89-
90-
return s;
91-
}
92-
9373
/**
9474
* @template V
9575
* @param {V} initial_value

packages/svelte/src/internal/client/runtime.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,6 @@ export function set_active_effect(effect) {
8686
active_effect = effect;
8787
}
8888

89-
/**
90-
* When sources are created within a derived, we record them so that we can safely allow
91-
* local mutations to these sources without the side-effect error being invoked unnecessarily.
92-
* @type {null | Source[]}
93-
*/
94-
export let derived_sources = null;
95-
96-
/**
97-
* @param {Source[] | null} sources
98-
*/
99-
export function set_derived_sources(sources) {
100-
derived_sources = sources;
101-
}
102-
10389
/**
10490
* The dependencies of the reaction that is currently being executed. In many cases,
10591
* the dependencies are unchanged between runs, and so this will be `null` unless
@@ -393,7 +379,6 @@ export function update_reaction(reaction) {
393379
var previous_untracked_writes = untracked_writes;
394380
var previous_reaction = active_reaction;
395381
var previous_skip_reaction = skip_reaction;
396-
var prev_derived_sources = derived_sources;
397382
var previous_component_context = component_context;
398383
var previous_untracking = untracking;
399384
var flags = reaction.f;
@@ -405,7 +390,6 @@ export function update_reaction(reaction) {
405390
(flags & UNOWNED) !== 0 && (untracking || !is_updating_effect || active_reaction === null);
406391
active_reaction = (flags & (BRANCH_EFFECT | ROOT_EFFECT)) === 0 ? reaction : null;
407392

408-
derived_sources = null;
409393
set_component_context(reaction.ctx);
410394
untracking = false;
411395
read_version++;
@@ -479,7 +463,6 @@ export function update_reaction(reaction) {
479463
untracked_writes = previous_untracked_writes;
480464
active_reaction = previous_reaction;
481465
skip_reaction = previous_skip_reaction;
482-
derived_sources = prev_derived_sources;
483466
set_component_context(previous_component_context);
484467
untracking = previous_untracking;
485468
}
@@ -868,9 +851,6 @@ export function get(signal) {
868851

869852
// Register the dependency on the current reaction signal.
870853
if (active_reaction !== null && !untracking) {
871-
if (derived_sources !== null && derived_sources.includes(signal)) {
872-
e.state_unsafe_local_read();
873-
}
874854
var deps = active_reaction.deps;
875855
if (signal.rv < read_version) {
876856
signal.rv = read_version;

packages/svelte/tests/signals/test.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import {
88
render_effect,
99
user_effect
1010
} from '../../src/internal/client/reactivity/effects';
11-
import { state, set, update, update_pre } from '../../src/internal/client/reactivity/sources';
11+
import {
12+
source as state,
13+
set,
14+
update,
15+
update_pre
16+
} from '../../src/internal/client/reactivity/sources';
1217
import type { Derived, Effect, Value } from '../../src/internal/client/types';
1318
import { proxy } from '../../src/internal/client/proxy';
1419
import { derived } from '../../src/internal/client/reactivity/deriveds';
@@ -958,17 +963,6 @@ describe('signals', () => {
958963
};
959964
});
960965

961-
test('deriveds cannot depend on state they own', () => {
962-
return () => {
963-
const d = derived(() => {
964-
const s = state(0);
965-
return $.get(s);
966-
});
967-
968-
assert.throws(() => $.get(d), 'state_unsafe_local_read');
969-
};
970-
});
971-
972966
test('proxy version state does not trigger self-dependency guard', () => {
973967
return () => {
974968
const s = proxy({ a: { b: 1 } });

0 commit comments

Comments
 (0)