@@ -39,6 +39,11 @@ public class FormControl<FieldName> where FieldName: Hashable {
3939 return field. value
4040 }
4141
42+ deinit {
43+ currentErrorNotifyTask? . cancel ( )
44+ currentErrorNotifyTask = nil
45+ }
46+
4247 public func unregister( names: [ FieldName ] , options: UnregisterOption = [ ] ) async {
4348 if self . options. shouldUnregister {
4449 names. forEach { fields [ $0] = nil }
@@ -75,7 +80,6 @@ public class FormControl<FieldName> where FieldName: Hashable {
7580 @_implicitSelfCapture onValid: @escaping ( FormValue < FieldName > , FormError < FieldName > ) async throws -> Void ,
7681 @_implicitSelfCapture onInvalid: ( ( FormValue < FieldName > , FormError < FieldName > ) async throws -> Void ) ? = nil
7782 ) async throws {
78- let preservedSubmissionState = instantFormState. submissionState
7983 instantFormState. submissionState = . submitting
8084 let errors : FormError < FieldName >
8185 var isOveralValid : Bool
@@ -161,7 +165,6 @@ public class FormControl<FieldName> where FieldName: Hashable {
161165 instantFormState. isValidating = false
162166 await syncFormState ( )
163167 } else {
164- await syncFormState ( )
165168 isOveralValid = instantFormState. isValid
166169 errors = instantFormState. errors
167170 }
@@ -171,44 +174,34 @@ public class FormControl<FieldName> where FieldName: Hashable {
171174 } else if let onInvalid {
172175 try await onInvalid ( instantFormState. formValues, errors)
173176 }
174- instantFormState. isValid = isOveralValid
175- instantFormState. isSubmitSuccessful = errors. errorFields. isEmpty
176- currentErrorNotifyTask? . cancel ( )
177- if options. delayErrorInNanoseconds == 0 || isOveralValid {
178- currentErrorNotifyTask = nil
179- instantFormState. errors = errors
180- } else {
181- currentErrorNotifyTask = Task {
182- try await Task . sleep ( nanoseconds: options. delayErrorInNanoseconds)
183- instantFormState. errors = errors
184- await syncFormState ( )
185- }
186- }
187- instantFormState. submitCount += 1
188- instantFormState. submissionState = . submitted
189- await syncFormState ( )
177+ await postHandleSubmit ( isOveralValid: isOveralValid, errors: errors, isSubmitSuccessful: errors. errorFields. isEmpty)
190178 } catch {
191- instantFormState. isValid = isOveralValid
192- instantFormState. submissionState = preservedSubmissionState
193- instantFormState. isSubmitSuccessful = false
194- currentErrorNotifyTask? . cancel ( )
195- if options. delayErrorInNanoseconds == 0 || isOveralValid {
196- currentErrorNotifyTask = nil
197- instantFormState. errors = errors
198- } else {
199- currentErrorNotifyTask = Task {
200- try await Task . sleep ( nanoseconds: options. delayErrorInNanoseconds)
201- instantFormState. errors = errors
202- await syncFormState ( )
203- }
204- }
205- instantFormState. submitCount += 1
206- instantFormState. submissionState = . submitted
207- await syncFormState ( )
179+ await postHandleSubmit ( isOveralValid: isOveralValid, errors: errors, isSubmitSuccessful: false )
208180 throw error
209181 }
210182 }
211183
184+ private func postHandleSubmit( isOveralValid: Bool , errors: FormError < FieldName > , isSubmitSuccessful: Bool ) async {
185+ instantFormState. isValid = isOveralValid
186+ instantFormState. submissionState = . submitted
187+ instantFormState. isSubmitSuccessful = isSubmitSuccessful
188+ currentErrorNotifyTask? . cancel ( )
189+ instantFormState. submitCount += 1
190+ if options. delayErrorInNanoseconds == 0 || isOveralValid {
191+ currentErrorNotifyTask = nil
192+ instantFormState. errors = errors
193+ await syncFormState ( )
194+ } else {
195+ await syncFormState ( )
196+ let delayErrorInNanoseconds = options. delayErrorInNanoseconds
197+ currentErrorNotifyTask = Task { [ weak self] in
198+ try await Task . sleep ( nanoseconds: delayErrorInNanoseconds)
199+ self ? . instantFormState. errors = errors
200+ await self ? . syncFormState ( )
201+ }
202+ }
203+ }
204+
212205 public func reset( defaultValues: FormValue < FieldName > , options: ResetOption = [ ] ) async {
213206 for (name, defaultValue) in defaultValues {
214207 if let defaultValue = Optional . some ( defaultValue) . flattened ( ) {
@@ -244,10 +237,7 @@ public class FormControl<FieldName> where FieldName: Hashable {
244237 if options. contains ( . keepErrors) {
245238 await syncFormState ( )
246239 }
247- if instantFormState. isValid {
248- return await updateValid ( )
249- }
250- await syncFormState ( )
240+ return await updateValid ( )
251241 }
252242
253243 public func reset( name: FieldName , defaultValue: Any , options: SingleResetOption = [ ] ) async {
@@ -268,10 +258,7 @@ public class FormControl<FieldName> where FieldName: Hashable {
268258 return await syncFormState ( )
269259 }
270260 instantFormState. errors. remove ( name: name)
271- if instantFormState. isValid {
272- return await updateValid ( )
273- }
274- await syncFormState ( )
261+ await updateValid ( )
275262 }
276263
277264 public func clearErrors( names: [ FieldName ] ) async {
@@ -356,14 +343,16 @@ public class FormControl<FieldName> where FieldName: Hashable {
356343 currentErrorNotifyTask? . cancel ( )
357344 if options. delayErrorInNanoseconds == 0 {
358345 instantFormState. errors = errors
346+ await syncFormState ( )
359347 } else {
360- currentErrorNotifyTask = Task {
361- try await Task . sleep ( nanoseconds: options. delayErrorInNanoseconds)
362- instantFormState. errors = errors
363- await syncFormState ( )
348+ await syncFormState ( )
349+ let delayErrorInNanoseconds = options. delayErrorInNanoseconds
350+ currentErrorNotifyTask = Task { [ weak self] in
351+ try await Task . sleep ( nanoseconds: delayErrorInNanoseconds)
352+ self ? . instantFormState. errors = errors
353+ await self ? . syncFormState ( )
364354 }
365355 }
366- await syncFormState ( )
367356 return isValid
368357 }
369358
@@ -376,11 +365,11 @@ public class FormControl<FieldName> where FieldName: Hashable {
376365extension FormControl {
377366 func updateValid( ) async {
378367 guard instantFormState. isValid else {
379- return
368+ return await syncFormState ( )
380369 }
381370 if let resolver = options. resolver {
382371 let isValid : Bool
383- let result = await resolver ( formState . formValues, options. context, Array ( formState. defaultValues. keys) )
372+ let result = await resolver ( instantFormState . formValues, options. context, Array ( formState. defaultValues. keys) )
384373 switch result {
385374 case . success( let formValues) :
386375 isValid = true
@@ -392,10 +381,11 @@ extension FormControl {
392381 currentErrorNotifyTask = nil
393382 instantFormState. errors = e
394383 } else {
395- currentErrorNotifyTask = Task {
396- try await Task . sleep ( nanoseconds: options. delayErrorInNanoseconds)
397- instantFormState. errors = e
398- await syncFormState ( )
384+ let delayErrorInNanoseconds = options. delayErrorInNanoseconds
385+ currentErrorNotifyTask = Task { [ weak self] in
386+ try await Task . sleep ( nanoseconds: delayErrorInNanoseconds)
387+ self ? . instantFormState. errors = e
388+ await self ? . syncFormState ( )
399389 }
400390 }
401391 }
@@ -424,10 +414,11 @@ extension FormControl {
424414 currentErrorNotifyTask = nil
425415 instantFormState. errors = errors
426416 } else {
427- currentErrorNotifyTask = Task {
428- try await Task . sleep ( nanoseconds: options. delayErrorInNanoseconds)
429- instantFormState. errors = errors
430- await syncFormState ( )
417+ let delayErrorInNanoseconds = options. delayErrorInNanoseconds
418+ currentErrorNotifyTask = Task { [ weak self] in
419+ try await Task . sleep ( nanoseconds: delayErrorInNanoseconds)
420+ self ? . instantFormState. errors = errors
421+ await self ? . syncFormState ( )
431422 }
432423 }
433424 instantFormState. isValid = isValid
0 commit comments