Skip to content

Commit 31882d1

Browse files
committed
add $effect.pending()
1 parent a0a4d4f commit 31882d1

File tree

5 files changed

+22
-0
lines changed

5 files changed

+22
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ export function CallExpression(node, context) {
3030
.../** @type {Expression[]} */ (node.arguments.map((arg) => context.visit(arg)))
3131
);
3232

33+
case '$effect.pending':
34+
return b.call('$.get', b.id('$.pending'));
35+
3336
case '$inspect':
3437
case '$inspect().with':
3538
return transform_inspect_rune(node, context);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export function CallExpression(node, context) {
2525
return b.arrow([], b.block([]));
2626
}
2727

28+
if (rune === '$effect.pending') {
29+
return b.false;
30+
}
31+
2832
if (rune === '$state.snapshot') {
2933
return b.call(
3034
'$.snapshot',

packages/svelte/src/internal/client/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export {
115115
user_effect,
116116
user_pre_effect
117117
} from './reactivity/effects.js';
118+
export { pending } from './reactivity/forks.js';
118119
export { mutable_state, mutate, set, state, update, update_pre } from './reactivity/sources.js';
119120
export {
120121
prop,

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/** @import { Effect, Source } from '#client' */
22
import { noop } from '../../shared/utils.js';
33
import { flushSync } from '../runtime.js';
4+
import { internal_set, source } from './sources.js';
45

56
/** @type {Set<Fork>} */
67
const forks = new Set();
@@ -12,6 +13,12 @@ export function remove_active_fork() {
1213
active_fork = null;
1314
}
1415

16+
export let pending = source(false);
17+
18+
function update_pending() {
19+
internal_set(pending, forks.size > 0);
20+
}
21+
1522
let uid = 1;
1623

1724
export class Fork {
@@ -97,6 +104,8 @@ export class Fork {
97104
}
98105
}
99106
}
107+
108+
update_pending();
100109
}
101110

102111
/**
@@ -134,6 +143,10 @@ export class Fork {
134143

135144
static ensure() {
136145
if (active_fork === null) {
146+
if (forks.size === 0) {
147+
requestAnimationFrame(update_pending);
148+
}
149+
137150
active_fork = new Fork();
138151
forks.add(active_fork); // TODO figure out where we remove this
139152
}

packages/svelte/src/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ const RUNES = /** @type {const} */ ([
441441
'$effect.pre',
442442
'$effect.tracking',
443443
'$effect.root',
444+
'$effect.pending',
444445
'$inspect',
445446
'$inspect().with',
446447
'$inspect.trace',

0 commit comments

Comments
 (0)