Skip to content

Commit 0d4add1

Browse files
committed
merge main
2 parents 8d02009 + 6915c12 commit 0d4add1

File tree

16 files changed

+153
-137
lines changed

16 files changed

+153
-137
lines changed

.changeset/dirty-pianos-sparkle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': minor
3+
---
4+
5+
feat: allow state created in deriveds/effects to be written/read locally without self-invalidation

benchmarking/compare/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import fs from 'node:fs';
22
import path from 'node:path';
33
import { execSync, fork } from 'node:child_process';
44
import { fileURLToPath } from 'node:url';
5-
import { benchmarks } from '../benchmarks.js';
65

76
// if (execSync('git status --porcelain').toString().trim()) {
87
// console.error('Working directory is not clean');

benchmarking/compare/runner.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { benchmarks } from '../benchmarks.js';
1+
import { reactivity_benchmarks } from '../benchmarks/reactivity/index.js';
22

33
const results = [];
4-
for (const benchmark of benchmarks) {
4+
for (const benchmark of reactivity_benchmarks) {
55
const result = await benchmark();
66
console.error(result.benchmark);
77
results.push(result);

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/compiler/phases/3-transform/client/transform-client.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ export function client_component(analysis, options) {
219219
for (const [name, binding] of analysis.instance.scope.declarations) {
220220
if (binding.kind === 'legacy_reactive') {
221221
legacy_reactive_declarations.push(
222-
b.const(name, b.call('$.mutable_state', undefined, analysis.immutable ? b.true : undefined))
222+
b.const(
223+
name,
224+
b.call('$.mutable_source', undefined, analysis.immutable ? b.true : undefined)
225+
)
223226
);
224227
}
225228
if (binding.kind === 'store_sub') {

packages/svelte/src/compiler/phases/3-transform/client/visitors/AssignmentExpression.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,7 @@ function build_assignment(operator, left, right, context) {
6969
is_non_coercive_operator(operator) &&
7070
should_proxy(value, context.state.scope);
7171

72-
return b.call(
73-
// inside the constructor, we use `$.simple_set` rather than using `$.set`,
74-
// that only assign the value and eventually call onchange since nothing is tracking the signal at this point
75-
context.state.in_constructor ? '$.simple_set' : '$.set',
76-
left,
77-
value,
78-
needs_proxy && b.true
79-
);
72+
return b.call('$.set', left, value, needs_proxy && b.true);
8073
}
8174
}
8275

packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ function create_state_declarators(declarator, { scope, analysis }, value) {
305305
return [
306306
b.declarator(
307307
declarator.id,
308-
b.call('$.mutable_state', value, analysis.immutable ? b.true : undefined)
308+
b.call('$.mutable_source', value, analysis.immutable ? b.true : undefined)
309309
)
310310
];
311311
}
@@ -320,7 +320,7 @@ function create_state_declarators(declarator, { scope, analysis }, value) {
320320
return b.declarator(
321321
path.node,
322322
binding?.kind === 'state'
323-
? b.call('$.mutable_state', value, analysis.immutable ? b.true : undefined)
323+
? b.call('$.mutable_source', value, analysis.immutable ? b.true : undefined)
324324
: value
325325
);
326326
})

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const LEGACY_DERIVED_PROP = 1 << 17;
2020
export const INSPECT_EFFECT = 1 << 18;
2121
export const HEAD_EFFECT = 1 << 19;
2222
export const EFFECT_HAS_DERIVED = 1 << 20;
23+
export const EFFECT_IS_UPDATING = 1 << 21;
2324

2425
export const STATE_SYMBOL = Symbol('$state');
2526
export const PROXY_ONCHANGE_SYMBOL = Symbol('proxy onchange');

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}

0 commit comments

Comments
 (0)