From 9b2fe3638bd7e01b9fa08c21e4bf2493909636fb Mon Sep 17 00:00:00 2001 From: unloco Date: Mon, 8 Apr 2024 11:46:06 +0100 Subject: [PATCH] Refactor variable names --- src/lib/client/superForm.ts | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) 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;