Skip to content

Commit 029dcfe

Browse files
committed
breaking: remove $state.link callback
1 parent 78677e4 commit 029dcfe

File tree

8 files changed

+9
-98
lines changed

8 files changed

+9
-98
lines changed

.changeset/sharp-foxes-whisper.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+
breaking: remove callback from `$state.link`

documentation/docs/03-runes/01-state.md

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,6 @@ console.log(a, b); // 3, 3
8989

9090
As with `$state`, if `$state.link` is passed a plain object or array it will be made deeply reactive. If passed an existing state proxy it will be reused, meaning that mutating the linked state will mutate the original. To clone a state proxy, you can use [`$state.snapshot`](#$state-snapshot).
9191

92-
If you pass a callback to `$state.link`, changes to the input value will invoke the callback rather than updating the linked state, allowing you to choose whether to (for example) preserve or discard local changes, or merge incoming changes with local ones:
93-
94-
```js
95-
let { stuff } = $props();
96-
97-
let incoming = $state();
98-
let hasUnsavedChanges = $state(false);
99-
100-
let current = $state.link({ ...stuff }, (stuff) => {
101-
if (hasUnsavedChanges) {
102-
incoming = stuff;
103-
} else {
104-
incoming = null;
105-
current = stuff;
106-
}
107-
});
108-
```
109-
11092
## `$state.raw`
11193

11294
State declared with `$state.raw` cannot be mutated; it can only be _reassigned_. In other words, rather than assigning to a property of an object, or using an array method like `push`, replace the object or array altogether if you'd like to update it:

packages/svelte/src/ambient.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ declare namespace $state {
148148
*
149149
* @param value The linked value
150150
*/
151-
export function link<T>(value: T, callback?: (value: T) => void): T;
151+
export function link<T>(value: T): T;
152152

153153
export function raw<T>(initial: T): T;
154154
export function raw<T>(): T | undefined;

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,7 @@ export function VariableDeclaration(node, context) {
131131
}
132132

133133
if (rune === '$state.link') {
134-
value = b.call(
135-
'$.source_link',
136-
b.thunk(value),
137-
args.length === 2 && /** @type {Expression} */ (context.visit(args[1]))
138-
);
134+
value = b.call('$.source_link', b.thunk(value));
139135
} else if (is_state_source(binding, context.state.analysis)) {
140136
value = b.call('$.source', value);
141137
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,9 @@ export function source(v) {
4949
/**
5050
* @template V
5151
* @param {() => V} get_value
52-
* @param {(value: V) => void} [callback]
5352
* @returns {(value?: V) => V}
5453
*/
55-
export function source_link(get_value, callback) {
54+
export function source_link(get_value) {
5655
var was_local = false;
5756
var init = false;
5857
var local_source = source(/** @type {V} */ (undefined));
@@ -80,10 +79,7 @@ export function source_link(get_value, callback) {
8079
var linked_value = get(linked_derived);
8180

8281
if (init) {
83-
if (callback !== undefined) {
84-
untrack(() => callback(linked_value));
85-
return local_source.v;
86-
}
82+
// do nothing
8783
} else {
8884
init = true;
8985
}

packages/svelte/tests/runtime-runes/samples/state-link-callback/_config.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

packages/svelte/tests/runtime-runes/samples/state-link-callback/main.svelte

Lines changed: 0 additions & 12 deletions
This file was deleted.

sites/svelte-5-preview/src/routes/docs/content/01-api/02-runes.md

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,6 @@ console.log(a, b); // 3, 3
9191

9292
As with `$state`, if `$state.link` is passed a plain object or array it will be made deeply reactive. If passed an existing state proxy it will be reused, meaning that mutating the linked state will mutate the original. To clone a state proxy, you can use [`$state.snapshot`](#$state-snapshot).
9393

94-
If you pass a callback to `$state.link`, changes to the input value will invoke the callback rather than updating the linked state, allowing you to choose whether to (for example) preserve or discard local changes, or merge incoming changes with local ones:
95-
96-
```js
97-
let { stuff } = $props();
98-
99-
let incoming = $state();
100-
let hasUnsavedChanges = $state(false);
101-
102-
let current = $state.link({ ...stuff }, (stuff) => {
103-
if (hasUnsavedChanges) {
104-
incoming = stuff;
105-
} else {
106-
incoming = null;
107-
current = stuff;
108-
}
109-
});
110-
```
111-
11294
## `$state.raw`
11395

11496
State declared with `$state.raw` cannot be mutated; it can only be _reassigned_. In other words, rather than assigning to a property of an object, or using an array method like `push`, replace the object or array altogether if you'd like to update it:

0 commit comments

Comments
 (0)