Skip to content

Commit abe01a7

Browse files
committed
add invalidate fn
1 parent 0166bc4 commit abe01a7

File tree

7 files changed

+51
-6
lines changed

7 files changed

+51
-6
lines changed

packages/svelte/src/ambient.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ declare namespace $state {
144144
*
145145
* @param initial The initial value
146146
*/
147-
export function opaque<T>(initial: T): [T, () => void];
148-
export function opaque<T>(): [T | undefined, () => void];
147+
export function opaque<T>(initial: T): [T, (mutate?: (value: T) => void) => void];
148+
export function opaque<T>(): [T | undefined, (mutate?: (value: T) => void) => void];
149149

150150
/**
151151
* To take a static snapshot of a deeply reactive `$state` proxy, use `$state.snapshot`:

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,13 @@ export function VariableDeclaration(node, context) {
164164
b.declarator(state_id, b.call('$.opaque_state', value)),
165165
b.declarator(
166166
invalidation_id,
167-
b.thunk(b.call('$.set', state_id, b.member(state_id, b.id('v'))))
167+
b.arrow(
168+
[b.id('$$fn')],
169+
b.sequence([
170+
b.call(b.id('$$fn'), b.member(state_id, b.id('v'))),
171+
b.call('$.set', state_id, b.member(state_id, b.id('v')))
172+
])
173+
)
168174
)
169175
);
170176
continue;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export function VariableDeclaration(node, context) {
9898
const invalidation_id = /** @type {Identifier} */ (pattern.elements[1]);
9999
declarations.push(
100100
b.declarator(state_id, value),
101-
b.declarator(invalidation_id, b.thunk(b.block([])))
101+
b.declarator(invalidation_id, b.arrow([b.id('$$fn')], b.call(b.id('$$fn'), state_id)))
102102
);
103103
continue;
104104
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script>
2+
let { count } = $props();
3+
4+
let [obj, invalidate] = $state.opaque({ count: 0 });
5+
6+
$effect(() => {
7+
invalidate((obj) => {
8+
obj.count = count;
9+
});
10+
});
11+
</script>
12+
13+
<p>{obj.count}</p>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
html: `<p>0</p><button>+1</button>`,
6+
7+
test({ assert, target }) {
8+
const button = target.querySelector('button');
9+
10+
button?.click();
11+
flushSync();
12+
assert.htmlEqual(target.innerHTML, `<p>1</p><button>+1</button>`);
13+
14+
button?.click();
15+
flushSync();
16+
assert.htmlEqual(target.innerHTML, `<p>2</p><button>+1</button>`);
17+
}
18+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
import Child from './Child.svelte';
3+
4+
let count = $state(0);
5+
</script>
6+
7+
<Child {count} />
8+
<button onclick={() => count += 1}>+1</button>

packages/svelte/types/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2691,8 +2691,8 @@ declare namespace $state {
26912691
*
26922692
* @param initial The initial value
26932693
*/
2694-
export function opaque<T>(initial: T): [T, () => void];
2695-
export function opaque<T>(): [T | undefined, () => void];
2694+
export function opaque<T>(initial: T): [T, (mutate?: (value: T) => void) => void];
2695+
export function opaque<T>(): [T | undefined, (mutate?: (value: T) => void) => void];
26962696

26972697
/**
26982698
* To take a static snapshot of a deeply reactive `$state` proxy, use `$state.snapshot`:

0 commit comments

Comments
 (0)