Skip to content

Commit 61adc3d

Browse files
committed
use $state.eager(value) instead of $effect.pending(value)
1 parent 323a641 commit 61adc3d

File tree

6 files changed

+50
-13
lines changed

6 files changed

+50
-13
lines changed

packages/svelte/src/ambient.d.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,24 @@ declare namespace $state {
9595
: never
9696
: never;
9797

98+
/**
99+
* Returns the latest `value`, even if the rest of the UI is suspending
100+
* while async work (such as data loading) completes.
101+
*
102+
* ```svelte
103+
* <nav>
104+
* <a href="/" aria-current={$state.eager(href) === '/' ? 'page' : null}>home</a>
105+
* <a href="/about" aria-current={$state.eager(href) === '/about' ? 'page' : null}>about</a>
106+
* </nav>
107+
* ```
108+
*/
109+
export function eager<T>(value: T): T;
98110
/**
99111
* Declares state that is _not_ made deeply reactive — instead of mutating it,
100112
* you must reassign it.
101113
*
102114
* Example:
103-
* ```ts
115+
* ```svelte
104116
* <script>
105117
* let items = $state.raw([0]);
106118
*
@@ -109,7 +121,7 @@ declare namespace $state {
109121
* };
110122
* </script>
111123
*
112-
* <button on:click={addItem}>
124+
* <button onclick={addItem}>
113125
* {items.join(', ')}
114126
* </button>
115127
* ```
@@ -124,7 +136,7 @@ declare namespace $state {
124136
* To take a static snapshot of a deeply reactive `$state` proxy, use `$state.snapshot`:
125137
*
126138
* Example:
127-
* ```ts
139+
* ```svelte
128140
* <script>
129141
* let counter = $state({ count: 0 });
130142
*

packages/svelte/src/compiler/phases/2-analyze/visitors/CallExpression.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,13 @@ export function CallExpression(node, context) {
226226
break;
227227
}
228228

229+
case '$state.eager':
230+
if (node.arguments.length !== 1) {
231+
e.rune_invalid_arguments_length(node, rune, 'exactly one argument');
232+
}
233+
234+
break;
235+
229236
case '$state.snapshot':
230237
if (node.arguments.length !== 1) {
231238
e.rune_invalid_arguments_length(node, rune, 'exactly one argument');

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ export function CallExpression(node, context) {
4949
return b.call('$.derived', rune === '$derived' ? b.thunk(fn) : fn);
5050
}
5151

52+
case '$state.eager':
53+
return b.call(
54+
'$.pending',
55+
b.thunk(/** @type {Expression} */ (context.visit(node.arguments[0])))
56+
);
57+
5258
case '$state.snapshot':
5359
return b.call(
5460
'$.snapshot',
@@ -63,12 +69,7 @@ export function CallExpression(node, context) {
6369
);
6470

6571
case '$effect.pending':
66-
return b.call(
67-
'$.pending',
68-
node.arguments.length > 0
69-
? b.thunk(/** @type {Expression} */ (context.visit(node.arguments[0])))
70-
: undefined
71-
);
72+
return b.call('$.pending');
7273

7374
case '$inspect':
7475
case '$inspect().with':

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function CallExpression(node, context) {
2626
}
2727

2828
if (rune === '$effect.pending') {
29-
return node.arguments[0] ?? b.literal(0);
29+
return b.literal(0);
3030
}
3131

3232
if (rune === '$state' || rune === '$state.raw') {
@@ -38,6 +38,10 @@ export function CallExpression(node, context) {
3838
return b.call('$.derived', rune === '$derived' ? b.thunk(fn) : fn);
3939
}
4040

41+
if (rune === '$state.eager') {
42+
return node.arguments[0];
43+
}
44+
4145
if (rune === '$state.snapshot') {
4246
return b.call(
4347
'$.snapshot',

packages/svelte/src/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ const STATE_CREATION_RUNES = /** @type {const} */ ([
436436

437437
const RUNES = /** @type {const} */ ([
438438
...STATE_CREATION_RUNES,
439+
'$state.eager',
439440
'$state.snapshot',
440441
'$props',
441442
'$props.id',

packages/svelte/types/index.d.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3185,12 +3185,24 @@ declare namespace $state {
31853185
: never
31863186
: never;
31873187

3188+
/**
3189+
* Returns the latest `value`, even if the rest of the UI is suspending
3190+
* while async work (such as data loading) completes.
3191+
*
3192+
* ```svelte
3193+
* <nav>
3194+
* <a href="/" aria-current={$state.eager(href) === '/' ? 'page' : null}>home</a>
3195+
* <a href="/about" aria-current={$state.eager(href) === '/about' ? 'page' : null}>about</a>
3196+
* </nav>
3197+
* ```
3198+
*/
3199+
export function eager<T>(value: T): T;
31883200
/**
31893201
* Declares state that is _not_ made deeply reactive — instead of mutating it,
31903202
* you must reassign it.
31913203
*
31923204
* Example:
3193-
* ```ts
3205+
* ```svelte
31943206
* <script>
31953207
* let items = $state.raw([0]);
31963208
*
@@ -3199,7 +3211,7 @@ declare namespace $state {
31993211
* };
32003212
* </script>
32013213
*
3202-
* <button on:click={addItem}>
3214+
* <button onclick={addItem}>
32033215
* {items.join(', ')}
32043216
* </button>
32053217
* ```
@@ -3214,7 +3226,7 @@ declare namespace $state {
32143226
* To take a static snapshot of a deeply reactive `$state` proxy, use `$state.snapshot`:
32153227
*
32163228
* Example:
3217-
* ```ts
3229+
* ```svelte
32183230
* <script>
32193231
* let counter = $state({ count: 0 });
32203232
*

0 commit comments

Comments
 (0)