Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

ValidationSourceInstance.lib.ts

105 lines3.2 KB

1import type { ValidatorScope } from './validation.public'
2​
3/** Internal validation sources ordered by their contribution to derived errors. */
4export const validationSourceScopes = {
5 field: 0,
6 group: 1,
7 form: 2,
8 onSubmit: 3,
9} as const
10​
11export type InternalValidationSourceScope = ValidatorScope | 'onSubmit'
12​
13export type InternalValidationSourceScopePriority =
14 (typeof validationSourceScopes)[InternalValidationSourceScope]
15​
16export interface InternalValidationSourceInstanceOptions<out TOwner> {
17 owner: TOwner
18 scope: InternalValidationSourceScope
19 index?: number
21​
22export type AnyInternalValidationSourceInstance =
23 InternalValidationSourceInstance<any, any>
24​
25/** Stable identity and ordering metadata for one internal validation source. */
26export class InternalValidationSourceInstance<TOwner, TErrorTarget = unknown> {
27 /** The validation boundary that owns this source. */
28 readonly owner: TOwner
29 /** Numeric priority used to order errors across validation scopes. */
30 readonly scope: InternalValidationSourceScopePriority
31 /** This source's position within its scope's validation pipeline. */
32 readonly index: number
33 /** Targets currently receiving errors routed from this source. */
34 errorTargets: Set<TErrorTarget> | null = null
35 /** Whether this source has been permanently disposed. */
36 disposed = false
37​
38 /** Creates a stable source with fixed ownership and ordering metadata. */
39 constructor({
40 owner,
41 scope,
42 index = 0,
43 }: InternalValidationSourceInstanceOptions<TOwner>) {
44 this.owner = owner
45 this.scope = validationSourceScopes[scope]
46 this.index = index
47 }
48​
49 /** Records a target receiving errors from this source. */
50 addErrorTarget(errorTarget: TErrorTarget): void {
51 if (this.disposed) return
52​
53 if (!this.errorTargets) {
54 this.errorTargets = new Set()
55 }
56 this.errorTargets.add(errorTarget)
57 }
58​
59 /** Stops tracking a target that no longer receives errors from this source. */
60 deleteErrorTarget(errorTarget: TErrorTarget): void {
61 if (this.disposed) return
62​
63 this.errorTargets?.delete(errorTarget)
64 if (this.errorTargets?.size === 0) {
65 this.errorTargets = null
66 }
67 }
68​
69 /** Clears transient state while preserving this source's stable identity. */
70 resetRuntime(): void {
71 if (this.disposed) return
72​
73 this._resetRuntime()
74 this.errorTargets = null
75 }
76​
77 /** Permanently disposes this source and releases its transient state. */
78 dispose(onBeforeDispose?: (instance: this) => void): void {
79 if (this.disposed) return
80​
81 onBeforeDispose?.(this)
82 this._disposeRuntime()
83 this.errorTargets = null
84 this.disposed = true
85 }
86​
87 /** Releases source-specific transient state before common state is cleared. */
88 protected _resetRuntime(): void {}
89​
90 /** Releases source-specific permanent state during disposal. */
91 protected _disposeRuntime(): void {
92 this._resetRuntime()
93 }
95​
96/** Orders sources first by scope priority and then by pipeline position. */
97export function compareValidationSources(
98 left: AnyInternalValidationSourceInstance,
99 right: AnyInternalValidationSourceInstance,
100): number {
101 if (left.scope === right.scope) {
102 return left.index - right.index
103 }
104 return left.scope - right.scope