Skip to content
Open
Changes from all 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
96 changes: 55 additions & 41 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ import type {
import type { DeepKeys, DeepKeysOfType, DeepValue } from './util-types'
import type { Updater } from './utils'

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

/**
* @private
*/
Expand Down Expand Up @@ -1305,47 +1316,6 @@ export class FormApi<
this.handleSubmit = this.handleSubmit.bind(this)

this.update(opts || {})

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

// devtool broadcasts
this.store.subscribe(() => {
debouncedDevtoolState(this.store.state)
})

// devtool requests
formEventClient.on('request-form-state', (e) => {
if (e.payload.id === this._formId) {
formEventClient.emit('form-api', {
id: this._formId,
state: this.store.state,
options: this.options,
})
}
})

formEventClient.on('request-form-reset', (e) => {
if (e.payload.id === this._formId) {
this.reset()
}
})

formEventClient.on('request-form-force-submit', (e) => {
if (e.payload.id === this._formId) {
this._devtoolsSubmissionOverride = true
this.handleSubmit()
this._devtoolsSubmissionOverride = false
}
})
}

get formId(): string {
Expand Down Expand Up @@ -1380,7 +1350,51 @@ export class FormApi<
mount = () => {
const cleanupFieldMetaDerived = this.fieldMetaDerived.mount()
const cleanupStoreDerived = this.store.mount()

// devtool broadcasts
const cleanupDevtoolBroadcast = this.store.subscribe(() => {
debouncedDevtoolState(this.store.state, this._formId)
})

// devtool requests
const cleanupFormStateListener = formEventClient.on(
'request-form-state',
(e) => {
if (e.payload.id === this._formId) {
formEventClient.emit('form-api', {
id: this._formId,
state: this.store.state,
options: this.options,
})
}
},
)

const cleanupFormResetListener = formEventClient.on(
'request-form-reset',
(e) => {
if (e.payload.id === this._formId) {
this.reset()
}
},
)

const cleanupFormForceSubmitListener = formEventClient.on(
'request-form-force-submit',
(e) => {
if (e.payload.id === this._formId) {
this._devtoolsSubmissionOverride = true
this.handleSubmit()
this._devtoolsSubmissionOverride = false
}
},
)

const cleanup = () => {
cleanupFormForceSubmitListener()
cleanupFormResetListener()
cleanupFormStateListener()
cleanupDevtoolBroadcast()
cleanupFieldMetaDerived()
cleanupStoreDerived()

Expand Down
Loading