@tanstack/form-core 1.24.3 → 1.24.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,6 +9,11 @@ type ExtractEventNames<T> = T extends `${string}:${infer EventName}`
9
9
  export type BroadcastFormState = {
10
10
  id: string
11
11
  state: AnyFormState
12
+ }
13
+
14
+ export type BroadcastFormApi = {
15
+ id: string
16
+ state: AnyFormState
12
17
  options: AnyFormOptions
13
18
  }
14
19
 
@@ -33,29 +38,20 @@ export type BroadcastFormSubmissionState =
33
38
  successful: true
34
39
  }
35
40
 
36
- export type BroadcastFormUnmounted = {
37
- id: string
38
- }
39
-
40
- export type RequestFormState = {
41
+ export type BroadcastFormId = {
41
42
  id: string
42
43
  }
43
44
 
44
- export type RequestFormReset = {
45
- id: string
46
- }
45
+ type EventMap = {
46
+ 'form-devtools:form-state': BroadcastFormState
47
+ 'form-devtools:form-api': BroadcastFormApi
48
+ 'form-devtools:form-submission': BroadcastFormSubmissionState
47
49
 
48
- export type RequestFormForceReset = {
49
- id: string
50
- }
50
+ 'form-devtools:request-form-state': BroadcastFormId
51
+ 'form-devtools:request-form-reset': BroadcastFormId
52
+ 'form-devtools:request-form-force-submit': BroadcastFormId
51
53
 
52
- type EventMap = {
53
- 'form-devtools:form-state-change': BroadcastFormState
54
- 'form-devtools:form-submission-state-change': BroadcastFormSubmissionState
55
- 'form-devtools:form-unmounted': BroadcastFormUnmounted
56
- 'form-devtools:request-form-state': RequestFormState
57
- 'form-devtools:request-form-reset': RequestFormReset
58
- 'form-devtools:request-form-force-submit': RequestFormForceReset
54
+ 'form-devtools:form-unmounted': BroadcastFormId
59
55
  }
60
56
 
61
57
  export type EventClientEventMap = keyof EventMap
package/src/FormApi.ts CHANGED
@@ -1,5 +1,5 @@
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,