File tree Expand file tree Collapse file tree 6 files changed +50
-13
lines changed Expand file tree Collapse file tree 6 files changed +50
-13
lines changed Original file line number Diff line number Diff 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 *
Original file line number Diff line number Diff 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' ) ;
Original file line number Diff line number Diff 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' :
Original file line number Diff line number Diff 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' ,
Original file line number Diff line number Diff line change @@ -436,6 +436,7 @@ const STATE_CREATION_RUNES = /** @type {const} */ ([
436436
437437const RUNES = /** @type {const } */ ( [
438438 ...STATE_CREATION_RUNES ,
439+ '$state.eager' ,
439440 '$state.snapshot' ,
440441 '$props' ,
441442 '$props.id' ,
Original file line number Diff line number Diff 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 *
You can’t perform that action at this time.
0 commit comments