diff --git a/src/lib/client/superForm.ts b/src/lib/client/superForm.ts index d9ef46df..593d243c 100644 --- a/src/lib/client/superForm.ts +++ b/src/lib/client/superForm.ts @@ -389,25 +389,27 @@ try { // No Storybook } -const onDestroyCallbacks = new Set<() => void>(); -const beforeNavigateCallbacks = new Set<(nav: BeforeNavigate) => Promise>(); -let lifeCycleHandlersInited = false; +const lifeCycleCallbacks = { + onDestroy: new Set<() => void>(), + beforeNavigate: new Set<(nav: BeforeNavigate) => Promise>() +} +let componentInitialized = false; function initLifeCycleCallbacks() { - if (lifeCycleHandlersInited) return; - lifeCycleHandlersInited = true; + if (componentInitialized) return; + componentInitialized = true; onDestroy(() => { - for (const callback of onDestroyCallbacks) { + for (const callback of lifeCycleCallbacks.onDestroy) { callback(); } - onDestroyCallbacks.clear(); + lifeCycleCallbacks.onDestroy.clear(); }); beforeNavigate((nav: BeforeNavigate) => { - for (const callback of beforeNavigateCallbacks) { + for (const callback of lifeCycleCallbacks.beforeNavigate) { callback(nav); } - beforeNavigateCallbacks.clear(); + lifeCycleCallbacks.beforeNavigate.clear(); }); } @@ -548,7 +550,7 @@ export function superForm< ///// From here, form is properly initialized ///// - onDestroyCallbacks.add(() => { + lifeCycleCallbacks.onDestroy.add(() => { Unsubscriptions_unsubscribe(); NextChange_clear(); EnhancedForm_destroy(); @@ -1380,7 +1382,7 @@ export function superForm< // Tainted check const defaultMessage = 'Leave page? Changes that you made may not be saved.'; let forceRedirection = false; - beforeNavigateCallbacks.add(async (nav: BeforeNavigate) => { + lifeCycleCallbacks.beforeNavigate.add(async (nav: BeforeNavigate) => { if (options.taintedMessage && !Data.submitting && !forceRedirection) { if (Tainted_isTainted()) { const { taintedMessage } = options;