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

linked-fields.lib.ts

237 lines6.0 KB

1import type {
2 AnyInternalFieldApi,
3 InternalFieldListenerInstance,
4 InternalFieldValidatorInstance,
5} from './FieldApi.lib'
6import type { AnyInternalFormApi } from '../FormApi/FormApi.lib'
7​
8export interface ListenerWatchFieldOperation {
9 kind: 'listener'
10 sourceField: AnyInternalFieldApi
11 watchingField: AnyInternalFieldApi
12 listenerInstance: InternalFieldListenerInstance
14​
15export interface ValidatorWatchFieldOperation {
16 kind: 'validator'
17 sourceField: AnyInternalFieldApi
18 watchingField: AnyInternalFieldApi
19 validatorInstance: InternalFieldValidatorInstance
21​
22export interface DetachWatchingFieldOptions {
23 pruneSourceField?: boolean
25​
26export function reconcileWatchedListenerFields({
27 listenerInstances,
28 field,
29 form,
30}: {
31 listenerInstances: ReadonlyArray<InternalFieldListenerInstance> | null
32 field: AnyInternalFieldApi
33 form: AnyInternalFormApi
34}): {
35 attach: Array<ListenerWatchFieldOperation>
36 detach: Array<ListenerWatchFieldOperation>
37} {
38 const attach: Array<ListenerWatchFieldOperation> = []
39 const detach: Array<ListenerWatchFieldOperation> = []
40​
41 listenerInstances?.forEach((listenerInstance) => {
42 const previous = listenerInstance.resolvedWatchFields
43 const next = new Map<string, AnyInternalFieldApi>()
44 const names = [...new Set(listenerInstance.definition.watchFields ?? [])]
45​
46 for (const name of names) {
47 const sourceField = form._getOrCreateFieldApi({ name }, 'internal')
48 next.set(name, sourceField)
49​
50 const previousField = previous?.get(name)
51 if (previousField === sourceField) continue
52​
53 if (previousField) {
54 detach.push({
55 kind: 'listener',
56 sourceField: previousField,
57 watchingField: field,
58 listenerInstance,
59 })
60 }
61​
62 attach.push({
63 kind: 'listener',
64 sourceField,
65 watchingField: field,
66 listenerInstance,
67 })
68 }
69​
70 previous?.forEach((sourceField, name) => {
71 if (next.has(name)) return
72​
73 detach.push({
74 kind: 'listener',
75 sourceField,
76 watchingField: field,
77 listenerInstance,
78 })
79 })
80​
81 listenerInstance.resolvedWatchFields = next.size > 0 ? next : null
82 })
83​
84 return { attach, detach }
86​
87export function reconcileWatchedValidatorFields({
88 validatorInstances,
89 field,
90 form,
91}: {
92 validatorInstances: ReadonlyArray<InternalFieldValidatorInstance> | null
93 field: AnyInternalFieldApi
94 form: AnyInternalFormApi
95}): {
96 attach: Array<ValidatorWatchFieldOperation>
97 detach: Array<ValidatorWatchFieldOperation>
98} {
99 const attach: Array<ValidatorWatchFieldOperation> = []
100 const detach: Array<ValidatorWatchFieldOperation> = []
101​
102 validatorInstances?.forEach((validatorInstance) => {
103 const previous = validatorInstance.resolvedWatchFields
104 const next = new Map<string, AnyInternalFieldApi>()
105 const names = [...new Set(validatorInstance.definition.watchFields ?? [])]
106​
107 for (const name of names) {
108 const sourceField = form._getOrCreateFieldApi({ name }, 'internal')
109 next.set(name, sourceField)
110​
111 const previousField = previous?.get(name)
112 if (previousField === sourceField) continue
113​
114 if (previousField) {
115 detach.push({
116 kind: 'validator',
117 sourceField: previousField,
118 watchingField: field,
119 validatorInstance,
120 })
121 }
122​
123 attach.push({
124 kind: 'validator',
125 sourceField,
126 watchingField: field,
127 validatorInstance,
128 })
129 }
130​
131 previous?.forEach((sourceField, name) => {
132 if (next.has(name)) return
133​
134 detach.push({
135 kind: 'validator',
136 sourceField,
137 watchingField: field,
138 validatorInstance,
139 })
140 })
141​
142 validatorInstance.resolvedWatchFields = next.size > 0 ? next : null
143 })
144​
145 return { attach, detach }
147​
148export function attachWatchingListenerField({
149 sourceField,
150 watchingField,
151 listenerInstance,
152}: ListenerWatchFieldOperation) {
153 let watchingFields = sourceField._watchingListenerFields
154 if (!watchingFields) {
155 watchingFields = new Map()
156 sourceField._watchingListenerFields = watchingFields
157 }
158​
159 let instances = watchingFields.get(watchingField)
160 if (!instances) {
161 instances = new Set()
162 watchingFields.set(watchingField, instances)
163 }
164​
165 instances.add(listenerInstance)
167​
168export function detachWatchingListenerField(
169 { sourceField, watchingField, listenerInstance }: ListenerWatchFieldOperation,
170 options: DetachWatchingFieldOptions = {},
171) {
172 const watchingFields = sourceField._watchingListenerFields
173 if (!watchingFields) return
174​
175 const instances = watchingFields.get(watchingField)
176 if (!instances) return
177​
178 instances.delete(listenerInstance)
179​
180 if (instances.size === 0) {
181 watchingFields.delete(watchingField)
182 if (watchingFields.size === 0) {
183 sourceField._watchingListenerFields = null
184 }
185 }
186​
187 if (options.pruneSourceField !== false) {
188 sourceField._pruneIfUnused()
189 }
191​
192export function attachWatchingValidatorField({
193 sourceField,
194 watchingField,
195 validatorInstance,
196}: ValidatorWatchFieldOperation) {
197 let watchingFields = sourceField._watchingValidatorFields
198 if (!watchingFields) {
199 watchingFields = new Map()
200 sourceField._watchingValidatorFields = watchingFields
201 }
202​
203 let instances = watchingFields.get(watchingField)
204 if (!instances) {
205 instances = new Set()
206 watchingFields.set(watchingField, instances)
207 }
208​
209 instances.add(validatorInstance)
211​
212export function detachWatchingValidatorField(
213 {
214 sourceField,
215 watchingField,
216 validatorInstance,
217 }: ValidatorWatchFieldOperation,
218 options?: DetachWatchingFieldOptions,
219) {
220 const watchingFields = sourceField._watchingValidatorFields
221 if (!watchingFields) return
222​
223 const instances = watchingFields.get(watchingField)
224 if (!instances) return
225​
226 instances.delete(validatorInstance)
227 if (instances.size === 0) {
228 watchingFields.delete(watchingField)
229 if (watchingFields.size === 0) {
230 sourceField._watchingValidatorFields = null
231 }
232 }
233​
234 if (options?.pruneSourceField !== false) {
235 sourceField._pruneIfUnused()
236 }