1
1
import { Derived , Store , batch } from '@tanstack/store'
2
-
2
+ import { throttle } from '@tanstack/pacer'
3
3
import {
4
4
deleteBy ,
5
5
determineFormLevelErrorSourceAndValue ,
@@ -22,11 +22,6 @@ import {
22
22
} from './standardSchemaValidator'
23
23
import { defaultFieldMeta , metaHelper } from './metaHelper'
24
24
import { formEventClient } from './EventClient'
25
- import type {
26
- RequestFormForceReset ,
27
- RequestFormReset ,
28
- RequestFormState ,
29
- } from './EventClient'
30
25
import type { ValidationLogicFn } from './ValidationLogic'
31
26
import type {
32
27
StandardSchemaV1 ,
@@ -1297,17 +1292,26 @@ export class FormApi<
1297
1292
1298
1293
this . update ( opts || { } )
1299
1294
1295
+ const debouncedDevtoolState = throttle (
1296
+ ( state : AnyFormState ) =>
1297
+ formEventClient . emit ( 'form-state' , {
1298
+ id : this . _formId ,
1299
+ state : state ,
1300
+ } ) ,
1301
+ {
1302
+ wait : 300 ,
1303
+ } ,
1304
+ )
1305
+
1306
+ // devtool broadcasts
1300
1307
this . store . subscribe ( ( ) => {
1301
- formEventClient . emit ( 'form-state-change' , {
1302
- id : this . _formId ,
1303
- state : this . store . state ,
1304
- options : this . options ,
1305
- } )
1308
+ debouncedDevtoolState ( this . store . state )
1306
1309
} )
1307
1310
1311
+ // devtool requests
1308
1312
formEventClient . on ( 'request-form-state' , ( e ) => {
1309
1313
if ( e . payload . id === this . _formId ) {
1310
- formEventClient . emit ( 'form-state-change ' , {
1314
+ formEventClient . emit ( 'form-api ' , {
1311
1315
id : this . _formId ,
1312
1316
state : this . store . state ,
1313
1317
options : this . options ,
@@ -1377,7 +1381,7 @@ export class FormApi<
1377
1381
const { onMount } = this . options . validators || { }
1378
1382
1379
1383
// broadcast form state for devtools on mounting
1380
- formEventClient . emit ( 'form-state-change ' , {
1384
+ formEventClient . emit ( 'form-api ' , {
1381
1385
id : this . _formId ,
1382
1386
state : this . store . state ,
1383
1387
options : this . options ,
@@ -1455,6 +1459,12 @@ export class FormApi<
1455
1459
) ,
1456
1460
)
1457
1461
} )
1462
+
1463
+ formEventClient . emit ( 'form-api' , {
1464
+ id : this . _formId ,
1465
+ state : this . store . state ,
1466
+ options : this . options ,
1467
+ } )
1458
1468
}
1459
1469
1460
1470
/**
@@ -2053,7 +2063,7 @@ export class FormApi<
2053
2063
meta : submitMetaArg ,
2054
2064
} )
2055
2065
2056
- formEventClient . emit ( 'form-submission-state-change ' , {
2066
+ formEventClient . emit ( 'form-submission' , {
2057
2067
id : this . _formId ,
2058
2068
submissionAttempt : this . state . submissionAttempts ,
2059
2069
successful : false ,
@@ -2077,7 +2087,7 @@ export class FormApi<
2077
2087
meta : submitMetaArg ,
2078
2088
} )
2079
2089
2080
- formEventClient . emit ( 'form-submission-state-change ' , {
2090
+ formEventClient . emit ( 'form-submission' , {
2081
2091
id : this . _formId ,
2082
2092
submissionAttempt : this . state . submissionAttempts ,
2083
2093
successful : false ,
@@ -2116,7 +2126,7 @@ export class FormApi<
2116
2126
isSubmitSuccessful : true , // Set isSubmitSuccessful to true on successful submission
2117
2127
} ) )
2118
2128
2119
- formEventClient . emit ( 'form-submission-state-change ' , {
2129
+ formEventClient . emit ( 'form-submission' , {
2120
2130
id : this . _formId ,
2121
2131
submissionAttempt : this . state . submissionAttempts ,
2122
2132
successful : true ,
@@ -2130,7 +2140,7 @@ export class FormApi<
2130
2140
isSubmitSuccessful : false , // Ensure isSubmitSuccessful is false if an error occurs
2131
2141
} ) )
2132
2142
2133
- formEventClient . emit ( 'form-submission-state-change ' , {
2143
+ formEventClient . emit ( 'form-submission' , {
2134
2144
id : this . _formId ,
2135
2145
submissionAttempt : this . state . submissionAttempts ,
2136
2146
successful : false ,
0 commit comments