Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/fast-doors-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@tanstack/form-devtools': patch
'@tanstack/react-form': patch
'@tanstack/form-core': patch
---

Optimise Optimises event client emissions. Minor layout tweeks
5 changes: 4 additions & 1 deletion examples/react/devtools/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ createRoot(document.getElementById('root')!).render(
<StrictMode>
<App />

<TanStackDevtools plugins={[FormDevtoolsPlugin()]} />
<TanStackDevtools
plugins={[FormDevtoolsPlugin()]}
eventBusConfig={{ debug: true }}
/>
</StrictMode>,
)
4 changes: 2 additions & 2 deletions examples/react/tanstack-start/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"@tanstack/react-devtools": "^0.7.0",
"@tanstack/react-form": "^1.23.7",
"@tanstack/react-form-devtools": "^0.1.7",
"@tanstack/react-router": "^1.131.50",
"@tanstack/react-start": "^1.131.50",
"@tanstack/react-router": "1.131.50",
"@tanstack/react-start": "1.131.50",
"@tanstack/react-store": "^0.7.7",
"react": "^19.0.0",
"react-dom": "^19.0.0"
Expand Down
3 changes: 2 additions & 1 deletion packages/form-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
"src"
],
"dependencies": {
"@tanstack/devtools-event-client": "^0.3.2",
"@tanstack/devtools-event-client": "^0.3.3",
"@tanstack/pacer": "^0.15.3",
"@tanstack/store": "^0.7.7"
},
"devDependencies": {
Expand Down
32 changes: 14 additions & 18 deletions packages/form-core/src/EventClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ type ExtractEventNames<T> = T extends `${string}:${infer EventName}`
export type BroadcastFormState = {
id: string
state: AnyFormState
}

export type BroadcastFormApi = {
id: string
state: AnyFormState
options: AnyFormOptions
}

Expand All @@ -33,29 +38,20 @@ export type BroadcastFormSubmissionState =
successful: true
}

export type BroadcastFormUnmounted = {
id: string
}

export type RequestFormState = {
export type BroadcastFormId = {
id: string
}

export type RequestFormReset = {
id: string
}
type EventMap = {
'form-devtools:form-state': BroadcastFormState
'form-devtools:form-api': BroadcastFormApi
'form-devtools:form-submission': BroadcastFormSubmissionState

export type RequestFormForceReset = {
id: string
}
'form-devtools:request-form-state': BroadcastFormId
'form-devtools:request-form-reset': BroadcastFormId
'form-devtools:request-form-force-submit': BroadcastFormId

type EventMap = {
'form-devtools:form-state-change': BroadcastFormState
'form-devtools:form-submission-state-change': BroadcastFormSubmissionState
'form-devtools:form-unmounted': BroadcastFormUnmounted
'form-devtools:request-form-state': RequestFormState
'form-devtools:request-form-reset': RequestFormReset
'form-devtools:request-form-force-submit': RequestFormForceReset
'form-devtools:form-unmounted': BroadcastFormId
}

export type EventClientEventMap = keyof EventMap
Expand Down
44 changes: 27 additions & 17 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Derived, Store, batch } from '@tanstack/store'

import { throttle } from '@tanstack/pacer'
import {
deleteBy,
determineFormLevelErrorSourceAndValue,
Expand All @@ -22,11 +22,6 @@ import {
} from './standardSchemaValidator'
import { defaultFieldMeta, metaHelper } from './metaHelper'
import { formEventClient } from './EventClient'
import type {
RequestFormForceReset,
RequestFormReset,
RequestFormState,
} from './EventClient'
import type { ValidationLogicFn } from './ValidationLogic'
import type {
StandardSchemaV1,
Expand Down Expand Up @@ -1297,17 +1292,26 @@ export class FormApi<

this.update(opts || {})

const debouncedDevtoolState = throttle(
(state: AnyFormState) =>
formEventClient.emit('form-state', {
id: this._formId,
state: state,
}),
{
wait: 300,
},
)

// devtool broadcasts
this.store.subscribe(() => {
formEventClient.emit('form-state-change', {
id: this._formId,
state: this.store.state,
options: this.options,
})
debouncedDevtoolState(this.store.state)
})

// devtool requests
formEventClient.on('request-form-state', (e) => {
if (e.payload.id === this._formId) {
formEventClient.emit('form-state-change', {
formEventClient.emit('form-api', {
id: this._formId,
state: this.store.state,
options: this.options,
Expand Down Expand Up @@ -1377,7 +1381,7 @@ export class FormApi<
const { onMount } = this.options.validators || {}

// broadcast form state for devtools on mounting
formEventClient.emit('form-state-change', {
formEventClient.emit('form-api', {
id: this._formId,
state: this.store.state,
options: this.options,
Expand Down Expand Up @@ -1455,6 +1459,12 @@ export class FormApi<
),
)
})

formEventClient.emit('form-api', {
id: this._formId,
state: this.store.state,
options: this.options,
})
}

/**
Expand Down Expand Up @@ -2053,7 +2063,7 @@ export class FormApi<
meta: submitMetaArg,
})

formEventClient.emit('form-submission-state-change', {
formEventClient.emit('form-submission', {
id: this._formId,
submissionAttempt: this.state.submissionAttempts,
successful: false,
Expand All @@ -2077,7 +2087,7 @@ export class FormApi<
meta: submitMetaArg,
})

formEventClient.emit('form-submission-state-change', {
formEventClient.emit('form-submission', {
id: this._formId,
submissionAttempt: this.state.submissionAttempts,
successful: false,
Expand Down Expand Up @@ -2116,7 +2126,7 @@ export class FormApi<
isSubmitSuccessful: true, // Set isSubmitSuccessful to true on successful submission
}))

formEventClient.emit('form-submission-state-change', {
formEventClient.emit('form-submission', {
id: this._formId,
submissionAttempt: this.state.submissionAttempts,
successful: true,
Expand All @@ -2130,7 +2140,7 @@ export class FormApi<
isSubmitSuccessful: false, // Ensure isSubmitSuccessful is false if an error occurs
}))

formEventClient.emit('form-submission-state-change', {
formEventClient.emit('form-submission', {
id: this._formId,
submissionAttempt: this.state.submissionAttempts,
successful: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/form-devtools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"src"
],
"dependencies": {
"@tanstack/devtools-ui": "^0.3.5",
"@tanstack/devtools-ui": "^0.4.3",
"@tanstack/form-core": "workspace:*",
"clsx": "^2.1.1",
"dayjs": "^1.11.18",
Expand Down
2 changes: 1 addition & 1 deletion packages/form-devtools/src/components/ActionButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { Accessor } from 'solid-js'
import type { DevtoolsFormState } from '../contexts/eventClientContext'

type ActionButtonsProps = {
selectedInstance: Accessor<DevtoolsFormState | null>
selectedInstance: Accessor<DevtoolsFormState | null | undefined>
}

export function ActionButtons(props: ActionButtonsProps) {
Expand Down
Loading
Loading