@@ -389,6 +389,30 @@ try {
389
389
// No Storybook
390
390
}
391
391
392
+ const lifeCycleCallbacks = {
393
+ onDestroy : new Set < ( ) => void > ( ) ,
394
+ beforeNavigate : new Set < ( nav : BeforeNavigate ) => Promise < void > > ( )
395
+ }
396
+ let componentInitialized = false ;
397
+ function initLifeCycleCallbacks ( ) {
398
+ if ( componentInitialized ) return ;
399
+ componentInitialized = true ;
400
+
401
+ onDestroy ( ( ) => {
402
+ for ( const callback of lifeCycleCallbacks . onDestroy ) {
403
+ callback ( ) ;
404
+ }
405
+ lifeCycleCallbacks . onDestroy . clear ( ) ;
406
+ } ) ;
407
+
408
+ beforeNavigate ( ( nav : BeforeNavigate ) => {
409
+ for ( const callback of lifeCycleCallbacks . beforeNavigate ) {
410
+ callback ( nav ) ;
411
+ }
412
+ lifeCycleCallbacks . beforeNavigate . clear ( ) ;
413
+ } ) ;
414
+ }
415
+
392
416
/////////////////////////////////////////////////////////////////////
393
417
394
418
/**
@@ -410,6 +434,8 @@ export function superForm<
410
434
// To check if a full validator is used when switching options.validators dynamically
411
435
let initialValidator : FormOptions < T , M , In > [ 'validators' ] | undefined = undefined ;
412
436
437
+ initLifeCycleCallbacks ( ) ;
438
+
413
439
{
414
440
if ( options . legacy ?? LEGACY_MODE ) {
415
441
if ( options . resetForm === undefined ) options . resetForm = false ;
@@ -525,7 +551,7 @@ export function superForm<
525
551
526
552
///// From here, form is properly initialized /////
527
553
528
- onDestroy ( ( ) => {
554
+ lifeCycleCallbacks . onDestroy . add ( ( ) => {
529
555
Unsubscriptions_unsubscribe ( ) ;
530
556
NextChange_clear ( ) ;
531
557
EnhancedForm_destroy ( ) ;
@@ -1397,7 +1423,7 @@ export function superForm<
1397
1423
///// Store subscriptions ///////////////////////////////////////////////////
1398
1424
1399
1425
if ( browser ) {
1400
- beforeNavigate ( Tainted_check ) ;
1426
+ lifeCycleCallbacks . beforeNavigate . add ( Tainted_check ) ;
1401
1427
1402
1428
// Need to subscribe to catch page invalidation.
1403
1429
Unsubscriptions_add (
0 commit comments