Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add infinite loop effect callstack #13231

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-bats-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

feat: add infinite loop effect callstack
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export function ExpressionStatement(node, context) {
const callee = rune === '$effect' ? '$.user_effect' : '$.user_pre_effect';
const func = /** @type {Expression} */ (context.visit(node.expression.arguments[0]));

return b.stmt(b.call(callee, /** @type {Expression} */ (func)));
const expr = b.call(callee, /** @type {Expression} */ (func));
expr.callee.loc = node.expression.callee.loc; // ensure correct mapping

return b.stmt(expr);
}
}

Expand Down
33 changes: 31 additions & 2 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export function set_is_destroying_effect(value) {
let current_queued_root_effects = [];

let flush_count = 0;
/** @type {Effect[]} Stack of effects, dev only */
let dev_effect_stack = [];
// Handle signal reactivity tree dependencies and reactions

/** @type {null | Reaction} */
Expand Down Expand Up @@ -443,8 +445,11 @@ export function update_effect(effect) {
execute_effect_teardown(effect);
var teardown = update_reaction(effect);
effect.teardown = typeof teardown === 'function' ? teardown : null;

effect.version = current_version;

if (DEV) {
dev_effect_stack.push(effect);
}
} catch (error) {
handle_error(/** @type {Error} */ (error), effect, current_component_context);
} finally {
Expand All @@ -460,7 +465,25 @@ export function update_effect(effect) {
function infinite_loop_guard() {
if (flush_count > 1000) {
flush_count = 0;
e.effect_update_depth_exceeded();
if (DEV) {
try {
e.effect_update_depth_exceeded();
} catch (error) {
// stack is garbage, ignore. Instead add a console.error message.
define_property(error, 'stack', {
value: ''
});
// eslint-disable-next-line no-console
console.error(
'Last ten effects were: ',
dev_effect_stack.slice(-10).map((d) => d.fn)
);
dev_effect_stack = [];
throw error;
}
} else {
e.effect_update_depth_exceeded();
}
}
flush_count++;
}
Expand Down Expand Up @@ -541,6 +564,9 @@ function process_deferred() {
flush_queued_root_effects(previous_queued_root_effects);
if (!is_micro_task_queued) {
flush_count = 0;
if (DEV) {
dev_effect_stack = [];
}
}
}

Expand Down Expand Up @@ -682,6 +708,9 @@ export function flush_sync(fn) {
}

flush_count = 0;
if (DEV) {
dev_effect_stack = [];
}

return result;
} finally {
Expand Down
9 changes: 9 additions & 0 deletions packages/svelte/tests/sourcemaps/samples/effects/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { test } from '../../test';

export default test({
client: [
{ str: '$effect.pre', strGenerated: '$.user_pre_effect' },
{ str: '$effect', strGenerated: '$.user_effect' }
],
server: []
});
9 changes: 9 additions & 0 deletions packages/svelte/tests/sourcemaps/samples/effects/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script lang="ts">
$effect(() => {
foo;
});

$effect.pre(() => {
bar;
});
</script>