Skip to content

Commit 0b51ff0

Browse files
authored
breaking: remove $state.link callback (#12942)
* breaking: remove `$state.link` callback * simplify * regenerate
1 parent af35fb7 commit 0b51ff0

File tree

9 files changed

+10
-110
lines changed

9 files changed

+10
-110
lines changed

.changeset/sharp-foxes-whisper.md

+5
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

-18
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

+1-1
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

+1-5
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

+2-17
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,10 @@ 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;
57-
var init = false;
5856
var local_source = source(/** @type {V} */ (undefined));
5957

6058
var linked_derived = derived(() => {
@@ -77,20 +75,7 @@ export function source_link(get_value, callback) {
7775
return value;
7876
}
7977

80-
var linked_value = get(linked_derived);
81-
82-
if (init) {
83-
if (callback !== undefined) {
84-
untrack(() => callback(linked_value));
85-
return local_source.v;
86-
}
87-
} else {
88-
init = true;
89-
}
90-
91-
local_source.v = linked_value;
92-
93-
return linked_value;
78+
return (local_source.v = get(linked_derived));
9479
};
9580
}
9681

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

-38
This file was deleted.

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

-12
This file was deleted.

packages/svelte/types/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2937,7 +2937,7 @@ declare namespace $state {
29372937
*
29382938
* @param value The linked value
29392939
*/
2940-
export function link<T>(value: T, callback?: (value: T) => void): T;
2940+
export function link<T>(value: T): T;
29412941

29422942
export function raw<T>(initial: T): T;
29432943
export function raw<T>(): T | undefined;

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

-18
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)