Skip to content

Commit f3b29d7

Browse files
committed
feat: add debounce to text field input to reduce validation calls
1 parent 3fd01a4 commit f3b29d7

File tree

1 file changed

+9
-1
lines changed
  • Sources/ValidatorUI/Classes/SUI/Managers/FormField/FormField

1 file changed

+9
-1
lines changed

Sources/ValidatorUI/Classes/SUI/Managers/FormField/FormField/FormField.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public final class FormField<Value>: IFormField {
3030
/// The rules applied to the value during validation.
3131
private let rules: [any IValidationRule<Value>]
3232

33+
/// The time the publisher should wait before publishing an element.
34+
private let debounce: TimeInterval
35+
3336
/// The wrapped property value.
3437
public var wrappedValue: Value {
3538
get { value }
@@ -44,14 +47,17 @@ public final class FormField<Value>: IFormField {
4447
/// - wrappedValue: The initial value of the field.
4548
/// - validator: The validator instance to use (defaults to `Validator()`).
4649
/// - rules: The array of validation rules to apply to the value.
50+
/// - debounce: The time the publisher should wait before publishing an element.
4751
public init(
4852
wrappedValue: Value,
4953
validator: IValidator = Validator(),
50-
rules: [any IValidationRule<Value>]
54+
rules: [any IValidationRule<Value>],
55+
debounce: TimeInterval = .zero
5156
) {
5257
value = wrappedValue
5358
self.validator = validator
5459
self.rules = rules
60+
self.debounce = debounce
5561
}
5662

5763
// MARK: IFormField
@@ -66,6 +72,8 @@ public final class FormField<Value>: IFormField {
6672

6773
let publisher = $value
6874
.receive(on: RunLoop.main)
75+
.dropFirst()
76+
.debounce(for: RunLoop.SchedulerTimeType.Stride(debounce), scheduler: RunLoop.main)
6977
.handleEvents(receiveOutput: { subject.send($0) })
7078
.map { self.validator.validate(input: $0, rules: self.rules) }
7179
.eraseToAnyPublisher()

0 commit comments

Comments
 (0)