Open History to see the last commit
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
13}
14β
15export interface ValidatorWatchFieldOperation {
16 kind: 'validator'
17 sourceField: AnyInternalFieldApi
18 watchingField: AnyInternalFieldApi
19 validatorInstance: InternalFieldValidatorInstance
20}
21β
22export interface DetachWatchingFieldOptions {
23 pruneSourceField?: boolean
24}
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 }
85}
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 }
146}
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)
166}
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 }
190}
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)
210}
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 }
237}