Is there a way to conditionally execute the parts of the pipeline ? Say in the code below i would want a bool which determines whether the api call and the debounce are executed or not (if true execute the debounce and api call, if false erase without them) ?
var flag: Bool // true: validate university via server,
// false: only validate with operations before debounce and flat map
init(flag: Bool) {
self.flag = flag
validate
.receive(on: RunLoop.main)
.sink { [weak self] value in
self?.loading = false
self?.validated = value
}
//.assign(to: \.validated, on: self)
.store(in: &cancellables)
}
private var validate: AnyPublisher<String?, Never> {
$value
.dropFirst()
.removeDuplicates()
.trim()
.filter { value in
value.contains("@") && value.contains(".")
}
.debounce(for: 1, scheduler: RunLoop.main) // from here on conditionally
.flatMap { value in
return Future { promise in
self.loading = true
Task {
Firebase.validate(email: value) { error in
promise(.success(error != nil ? nil : value))
}
}
}
}
.eraseToAnyPublisher()
}