Skip to content

breaking: remove $state.link callback #12942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sharp-foxes-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

breaking: remove callback from `$state.link`
18 changes: 0 additions & 18 deletions documentation/docs/03-runes/01-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,6 @@ console.log(a, b); // 3, 3

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

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:

```js
let { stuff } = $props();

let incoming = $state();
let hasUnsavedChanges = $state(false);

let current = $state.link({ ...stuff }, (stuff) => {
if (hasUnsavedChanges) {
incoming = stuff;
} else {
incoming = null;
current = stuff;
}
});
```

## `$state.raw`

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:
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ declare namespace $state {
*
* @param value The linked value
*/
export function link<T>(value: T, callback?: (value: T) => void): T;
export function link<T>(value: T): T;

export function raw<T>(initial: T): T;
export function raw<T>(): T | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,7 @@ export function VariableDeclaration(node, context) {
}

if (rune === '$state.link') {
value = b.call(
'$.source_link',
b.thunk(value),
args.length === 2 && /** @type {Expression} */ (context.visit(args[1]))
);
value = b.call('$.source_link', b.thunk(value));
} else if (is_state_source(binding, context.state.analysis)) {
value = b.call('$.source', value);
}
Expand Down
19 changes: 2 additions & 17 deletions packages/svelte/src/internal/client/reactivity/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ export function source(v) {
/**
* @template V
* @param {() => V} get_value
* @param {(value: V) => void} [callback]
* @returns {(value?: V) => V}
*/
export function source_link(get_value, callback) {
export function source_link(get_value) {
var was_local = false;
var init = false;
var local_source = source(/** @type {V} */ (undefined));

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

var linked_value = get(linked_derived);

if (init) {
if (callback !== undefined) {
untrack(() => callback(linked_value));
return local_source.v;
}
} else {
init = true;
}

local_source.v = linked_value;

return linked_value;
return (local_source.v = get(linked_derived));
};
}

Expand Down

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2937,7 +2937,7 @@ declare namespace $state {
*
* @param value The linked value
*/
export function link<T>(value: T, callback?: (value: T) => void): T;
export function link<T>(value: T): T;

export function raw<T>(initial: T): T;
export function raw<T>(): T | undefined;
Expand Down
18 changes: 0 additions & 18 deletions sites/svelte-5-preview/src/routes/docs/content/01-api/02-runes.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,6 @@ console.log(a, b); // 3, 3

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

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:

```js
let { stuff } = $props();

let incoming = $state();
let hasUnsavedChanges = $state(false);

let current = $state.link({ ...stuff }, (stuff) => {
if (hasUnsavedChanges) {
incoming = stuff;
} else {
incoming = null;
current = stuff;
}
});
```

## `$state.raw`

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:
Expand Down
Loading