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: pass AbortSignal to effects and allow async signature #14753

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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/popular-walls-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': minor
---

feat: pass AbortSignal to effects and allow async signature
8 changes: 6 additions & 2 deletions packages/svelte/src/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ declare namespace $derived {
* https://svelte.dev/docs/svelte/$effect
* @param fn The function to execute
*/
declare function $effect(fn: () => void | (() => void)): void;
declare function $effect(
fn: (input: { signal: AbortSignal }) => void | (() => void) | Promise<void>
): void;

declare namespace $effect {
/**
Expand All @@ -253,7 +255,9 @@ declare namespace $effect {
* https://svelte.dev/docs/svelte/$effect#$effect.pre
* @param fn The function to execute
*/
export function pre(fn: () => void | (() => void)): void;
export function pre(
fn: (input: { signal: AbortSignal }) => void | (() => void) | Promise<void>
): void;

/**
* The `$effect.tracking` rune is an advanced feature that tells you whether or not the code is running inside a tracking context, such as an effect or inside your template.
Expand Down
17 changes: 16 additions & 1 deletion packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,22 @@ export function update_reaction(reaction) {
component_context = reaction.ctx;

try {
var result = /** @type {Function} */ (0, reaction.fn)();
/** @type {any} */
var result;

if ((flags & EFFECT) !== 0 && /** @type {Function} */ (reaction.fn).length > 0) {
var controller = new AbortController();
var signal = controller.signal;
var inner = /** @type {Function} */ (0, reaction.fn)({ signal });

result = () => {
controller.abort('effect destroyed');
inner?.();
};
} else {
result = /** @type {Function} */ (0, reaction.fn)();
}

var deps = reaction.deps;

if (new_deps !== null) {
Expand Down
8 changes: 6 additions & 2 deletions packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2892,7 +2892,9 @@ declare namespace $derived {
* https://svelte.dev/docs/svelte/$effect
* @param fn The function to execute
*/
declare function $effect(fn: () => void | (() => void)): void;
declare function $effect(
fn: (input: { signal: AbortSignal }) => void | (() => void) | Promise<void>
): void;

declare namespace $effect {
/**
Expand All @@ -2911,7 +2913,9 @@ declare namespace $effect {
* https://svelte.dev/docs/svelte/$effect#$effect.pre
* @param fn The function to execute
*/
export function pre(fn: () => void | (() => void)): void;
export function pre(
fn: (input: { signal: AbortSignal }) => void | (() => void) | Promise<void>
): void;

/**
* The `$effect.tracking` rune is an advanced feature that tells you whether or not the code is running inside a tracking context, such as an effect or inside your template.
Expand Down
Loading