Skip to content

Commit 4fc61c7

Browse files
committed
add effect_pending_outside_reaction error
1 parent 70d4a3d commit 4fc61c7

File tree

7 files changed

+38
-4
lines changed

7 files changed

+38
-4
lines changed

documentation/docs/98-reference/.generated/client-errors.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ Effect cannot be created inside a `$derived` value that was not itself created i
8080
`%rune%` can only be used inside an effect (e.g. during component initialisation)
8181
```
8282

83+
### effect_pending_outside_reaction
84+
85+
```
86+
`$effect.pending()` can only be called inside an effect or derived
87+
```
88+
8389
### effect_update_depth_exceeded
8490

8591
```

packages/svelte/messages/client-errors/errors.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ See the [migration guide](/docs/svelte/v5-migration-guide#Components-are-no-long
5454

5555
> `%rune%` can only be used inside an effect (e.g. during component initialisation)
5656
57+
## effect_pending_outside_reaction
58+
59+
> `$effect.pending()` can only be called inside an effect or derived
60+
5761
## effect_update_depth_exceeded
5862

5963
> Maximum update depth exceeded. This can happen when a reactive block or effect repeatedly sets a new value. Svelte limits the number of nested updates to prevent infinite loops

packages/svelte/scripts/process-messages/templates/client-errors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { DEV } from 'esm-env';
2+
export * from '../shared/errors.js';
23

34
/**
45
* MESSAGE

packages/svelte/scripts/process-messages/templates/server-errors.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export * from '../shared/errors.js';
2+
13
/**
24
* MESSAGE
35
* @param {string} PARAMETER

packages/svelte/src/internal/client/dom/blocks/boundary.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
} from '../hydration.js';
2121
import { get_next_sibling } from '../operations.js';
2222
import { queue_micro_task } from '../task.js';
23-
import * as e from '../../../shared/errors.js';
23+
import * as e from '../../errors.js';
2424
import { DEV } from 'esm-env';
2525
import { from_async_derived, set_from_async_derived } from '../../reactivity/deriveds.js';
2626
import { Batch } from '../../reactivity/batch.js';
@@ -454,8 +454,11 @@ function exit() {
454454
}
455455

456456
export function pending() {
457-
// TODO throw helpful error if called outside an effect
458-
const boundary = /** @type {Effect} */ (active_effect).b;
457+
if (active_effect === null) {
458+
e.effect_pending_outside_reaction();
459+
}
460+
461+
var boundary = active_effect.b;
459462

460463
if (boundary === null) {
461464
return 0; // TODO eventually we will need this to be global

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import { DEV } from 'esm-env';
44

5+
export * from '../shared/errors.js';
6+
57
/**
68
* Cannot create a `$derived(...)` with an `await` expression outside of an effect tree
79
* @returns {never}
@@ -195,6 +197,22 @@ export function effect_orphan(rune) {
195197
}
196198
}
197199

200+
/**
201+
* `$effect.pending()` can only be called inside an effect or derived
202+
* @returns {never}
203+
*/
204+
export function effect_pending_outside_reaction() {
205+
if (DEV) {
206+
const error = new Error(`effect_pending_outside_reaction\n\`$effect.pending()\` can only be called inside an effect or derived\nhttps://svelte.dev/e/effect_pending_outside_reaction`);
207+
208+
error.name = 'Svelte error';
209+
210+
throw error;
211+
} else {
212+
throw new Error(`https://svelte.dev/e/effect_pending_outside_reaction`);
213+
}
214+
}
215+
198216
/**
199217
* Maximum update depth exceeded. This can happen when a reactive block or effect repeatedly sets a new value. Svelte limits the number of nested updates to prevent infinite loops
200218
* @returns {never}

packages/svelte/src/internal/server/errors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* This file is generated by scripts/process-messages/index.js. Do not edit! */
22

3-
3+
export * from '../shared/errors.js';
44

55
/**
66
* `%name%(...)` is not available on the server

0 commit comments

Comments
 (0)