Skip to content

Commit 85871d7

Browse files
committed
Revert "breaking: remove $state.link callback (#12942)"
This reverts commit 0b51ff0.
1 parent 0b51ff0 commit 85871d7

File tree

9 files changed

+110
-10
lines changed

9 files changed

+110
-10
lines changed

.changeset/sharp-foxes-whisper.md

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

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,24 @@ 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+
92110
## `$state.raw`
93111

94112
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): T;
151+
export function link<T>(value: T, callback?: (value: T) => void): 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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ export function VariableDeclaration(node, context) {
131131
}
132132

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

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ export function source(v) {
4949
/**
5050
* @template V
5151
* @param {() => V} get_value
52+
* @param {(value: V) => void} [callback]
5253
* @returns {(value?: V) => V}
5354
*/
54-
export function source_link(get_value) {
55+
export function source_link(get_value, callback) {
5556
var was_local = false;
57+
var init = false;
5658
var local_source = source(/** @type {V} */ (undefined));
5759

5860
var linked_derived = derived(() => {
@@ -75,7 +77,20 @@ export function source_link(get_value) {
7577
return value;
7678
}
7779

78-
return (local_source.v = get(linked_derived));
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;
7994
};
8095
}
8196

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
html: `<button>0</button><button>0</button><button>false</button>`,
6+
7+
test({ assert, target }) {
8+
const [btn1, btn2, btn3] = target.querySelectorAll('button');
9+
10+
flushSync(() => btn1.click());
11+
assert.htmlEqual(
12+
target.innerHTML,
13+
`<button>1</button><button>1</button><button>false</button>`
14+
);
15+
16+
flushSync(() => btn2.click());
17+
assert.htmlEqual(
18+
target.innerHTML,
19+
`<button>1</button><button>2</button><button>false</button>`
20+
);
21+
22+
flushSync(() => btn3.click());
23+
assert.htmlEqual(target.innerHTML, `<button>1</button><button>2</button><button>true</button>`);
24+
25+
flushSync(() => btn1.click());
26+
assert.htmlEqual(target.innerHTML, `<button>2</button><button>2</button><button>true</button>`);
27+
28+
flushSync(() => btn1.click());
29+
assert.htmlEqual(target.innerHTML, `<button>3</button><button>2</button><button>true</button>`);
30+
31+
flushSync(() => btn1.click());
32+
flushSync(() => btn3.click());
33+
assert.htmlEqual(
34+
target.innerHTML,
35+
`<button>4</button><button>4</button><button>false</button>`
36+
);
37+
}
38+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script>
2+
let a = $state(0);
3+
let b = $state.link(a, (value) => {
4+
if (c) return;
5+
b = value;
6+
});
7+
let c = $state(false);
8+
</script>
9+
10+
<button onclick={() => a++}>{a}</button>
11+
<button onclick={() => b++}>{b}</button>
12+
<button onclick={() => c = !c}>{c}</button>

packages/svelte/types/index.d.ts

Lines changed: 1 addition & 1 deletion
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): T;
2940+
export function link<T>(value: T, callback?: (value: T) => void): 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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ 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+
94112
## `$state.raw`
95113

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