From c6d9110b78d9f4fb97392fe59871d997d7979eba Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 24 Feb 2025 14:18:02 -0500 Subject: [PATCH] some progress --- .../src/internal/client/reactivity/forks.js | 8 ++--- .../svelte/src/internal/client/runtime.js | 31 +++++++++++++------ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/packages/svelte/src/internal/client/reactivity/forks.js b/packages/svelte/src/internal/client/reactivity/forks.js index 322c678b6c..33a0c0225e 100644 --- a/packages/svelte/src/internal/client/reactivity/forks.js +++ b/packages/svelte/src/internal/client/reactivity/forks.js @@ -10,6 +10,10 @@ const forks = new Set(); /** @type {Fork | null} */ export let active_fork = null; +export function remove_active_fork() { + active_fork = null; +} + let uid = 1; export class Fork { @@ -123,7 +127,3 @@ export class Fork { return active_fork; } } - -export function remove_active_fork() { - active_fork = null; -} diff --git a/packages/svelte/src/internal/client/runtime.js b/packages/svelte/src/internal/client/runtime.js index 2c78e90fbe..9661fdbd2b 100644 --- a/packages/svelte/src/internal/client/runtime.js +++ b/packages/svelte/src/internal/client/runtime.js @@ -696,15 +696,6 @@ function flush_queued_root_effects() { } } } finally { - // TODO this doesn't seem quite right — may run into - // interesting cases where there are multiple roots. - // it'll do for now though - if (fork.settled()) { - fork.remove(); - } - - remove_active_fork(); - is_flushing = false; last_scheduled_effect = null; @@ -759,7 +750,18 @@ function flush_queued_effects(effects) { export function schedule_effect(signal) { if (!is_flushing) { is_flushing = true; - queueMicrotask(flush_queued_root_effects); + queueMicrotask(() => { + flush_queued_root_effects(); + + // TODO this doesn't seem quite right — may run into + // interesting cases where there are multiple roots. + // it'll do for now though + if (active_fork?.settled()) { + active_fork.remove(); + } + + remove_active_fork(); + }); } var effect = (last_scheduled_effect = signal); @@ -895,6 +897,15 @@ export function flushSync(fn) { flush_tasks(); } + // TODO this doesn't seem quite right — may run into + // interesting cases where there are multiple roots. + // it'll do for now though + if (active_fork?.settled()) { + active_fork.remove(); + } + + remove_active_fork(); + return /** @type {T} */ (result); }