1
1
import { Derived , Store , batch } from '@tanstack/store'
2
+ import { throttle } from '@tanstack/pacer'
2
3
import { v4 as uuidv4 } from 'uuid'
3
4
import {
4
5
deleteBy ,
@@ -21,11 +22,6 @@ import {
21
22
} from './standardSchemaValidator'
22
23
import { defaultFieldMeta , metaHelper } from './metaHelper'
23
24
import { formEventClient } from './EventClient'
24
- import type {
25
- RequestFormForceReset ,
26
- RequestFormReset ,
27
- RequestFormState ,
28
- } from './EventClient'
29
25
import type { ValidationLogicFn } from './ValidationLogic'
30
26
import type {
31
27
StandardSchemaV1 ,
@@ -1296,17 +1292,26 @@ export class FormApi<
1296
1292
1297
1293
this . update ( opts || { } )
1298
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
1299
1307
this . store . subscribe ( ( ) => {
1300
- formEventClient . emit ( 'form-state-change' , {
1301
- id : this . _formId ,
1302
- state : this . store . state ,
1303
- options : this . options ,
1304
- } )
1308
+ debouncedDevtoolState ( this . store . state )
1305
1309
} )
1306
1310
1311
+ // devtool requests
1307
1312
formEventClient . on ( 'request-form-state' , ( e ) => {
1308
1313
if ( e . payload . id === this . _formId ) {
1309
- formEventClient . emit ( 'form-state-change ' , {
1314
+ formEventClient . emit ( 'form-api ' , {
1310
1315
id : this . _formId ,
1311
1316
state : this . store . state ,
1312
1317
options : this . options ,
@@ -1376,7 +1381,7 @@ export class FormApi<
1376
1381
const { onMount } = this . options . validators || { }
1377
1382
1378
1383
// broadcast form state for devtools on mounting
1379
- formEventClient . emit ( 'form-state-change ' , {
1384
+ formEventClient . emit ( 'form-api ' , {
1380
1385
id : this . _formId ,
1381
1386
state : this . store . state ,
1382
1387
options : this . options ,
@@ -1454,6 +1459,12 @@ export class FormApi<
1454
1459
) ,
1455
1460
)
1456
1461
} )
1462
+
1463
+ formEventClient . emit ( 'form-api' , {
1464
+ id : this . _formId ,
1465
+ state : this . store . state ,
1466
+ options : this . options ,
1467
+ } )
1457
1468
}
1458
1469
1459
1470
/**
@@ -2035,7 +2046,7 @@ export class FormApi<
2035
2046
meta : submitMetaArg ,
2036
2047
} )
2037
2048
2038
- formEventClient . emit ( 'form-submission-state-change ' , {
2049
+ formEventClient . emit ( 'form-submission' , {
2039
2050
id : this . _formId ,
2040
2051
submissionAttempt : this . state . submissionAttempts ,
2041
2052
successful : false ,
@@ -2059,7 +2070,7 @@ export class FormApi<
2059
2070
meta : submitMetaArg ,
2060
2071
} )
2061
2072
2062
- formEventClient . emit ( 'form-submission-state-change ' , {
2073
+ formEventClient . emit ( 'form-submission' , {
2063
2074
id : this . _formId ,
2064
2075
submissionAttempt : this . state . submissionAttempts ,
2065
2076
successful : false ,
@@ -2098,7 +2109,7 @@ export class FormApi<
2098
2109
isSubmitSuccessful : true , // Set isSubmitSuccessful to true on successful submission
2099
2110
} ) )
2100
2111
2101
- formEventClient . emit ( 'form-submission-state-change ' , {
2112
+ formEventClient . emit ( 'form-submission' , {
2102
2113
id : this . _formId ,
2103
2114
submissionAttempt : this . state . submissionAttempts ,
2104
2115
successful : true ,
@@ -2112,7 +2123,7 @@ export class FormApi<
2112
2123
isSubmitSuccessful : false , // Ensure isSubmitSuccessful is false if an error occurs
2113
2124
} ) )
2114
2125
2115
- formEventClient . emit ( 'form-submission-state-change ' , {
2126
+ formEventClient . emit ( 'form-submission' , {
2116
2127
id : this . _formId ,
2117
2128
submissionAttempt : this . state . submissionAttempts ,
2118
2129
successful : false ,
0 commit comments