Skip to content

Commit

Permalink
chore: fix timeout issues
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Oct 22, 2024
1 parent e07a6ba commit ed8a452
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/form-core/src/FieldApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ export class FieldApi<
* @private
*/
prevState!: FieldState<TData>
timeoutId: ReturnType<typeof setTimeout> | null
timeoutIds: Record<ValidationCause, ReturnType<typeof setTimeout> | null>

/**
* Initializes a new `FieldApi` instance.
Expand All @@ -448,7 +448,7 @@ export class FieldApi<
) {
this.form = opts.form as never
this.name = opts.name as never
this.timeoutId = null
this.timeoutIds = {} as Record<ValidationCause, never>
if (opts.defaultValue !== undefined) {
this.form.setFieldValue(this.name, opts.defaultValue as never, {
dontUpdateMeta: true,
Expand Down Expand Up @@ -892,11 +892,11 @@ export class FieldApi<
let rawError!: ValidationError | undefined
try {
rawError = await new Promise((rawResolve, rawReject) => {
if (this.timeoutId) {
clearTimeout(this.timeoutId)
if (this.timeoutIds[validateObj.cause]) {
clearTimeout(this.timeoutIds[validateObj.cause]!)
}

this.timeoutId = setTimeout(async () => {
this.timeoutIds[validateObj.cause] = setTimeout(async () => {
if (controller.signal.aborted) return rawResolve(undefined)
try {
rawResolve(
Expand Down
1 change: 1 addition & 0 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ export class FormApi<
const fieldInstance = field.instance
// Validate the field
fieldValidationPromises.push(
// Remember, `validate` is either a sync operation or a promise
Promise.resolve().then(() => fieldInstance.validate(cause)),
)
// If any fields are not touched
Expand Down

0 comments on commit ed8a452

Please sign in to comment.