Skip to content

Commit c2da1eb

Browse files
fix: keep effect in the graph if it has an abort controller (#16430)
1 parent 93a8a49 commit c2da1eb

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

.changeset/four-kings-drum.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: keep effect in the graph if it has an abort controller

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,9 @@ function flush_queued_effects(effects) {
532532
// here (rather than in `update_effect`) allows us to skip the work for
533533
// immediate effects.
534534
if (effect.deps === null && effect.first === null && effect.nodes_start === null) {
535-
if (effect.teardown === null) {
535+
// if there's no teardown or abort controller we completely unlink
536+
// the effect from the graph
537+
if (effect.teardown === null && effect.ac === null) {
536538
// remove this effect from the graph
537539
unlink_effect(effect);
538540
} else {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
import { getAbortSignal } from 'svelte';
3+
4+
$effect(() => {
5+
const signal = getAbortSignal()
6+
signal.addEventListener('abort', () => console.log('abort'))
7+
})
8+
</script>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
async test({ assert, target, logs }) {
6+
const btn = target.querySelector('button');
7+
flushSync(() => {
8+
btn?.click();
9+
});
10+
assert.deepEqual(logs, ['abort']);
11+
}
12+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script>
2+
import Component from './Component.svelte'
3+
4+
let show = $state(true)
5+
</script>
6+
7+
<button onclick={() => (show = !show)}>click</button>
8+
{#if show}
9+
<Component />
10+
{/if}

0 commit comments

Comments
 (0)