This code does not compile on iOS 13 because flatMap is available only in iOS 14. How to extend SubscribeOn to have flatMap operator?
func f1(_ value: Int) -> Future<Int, Error> {
Future { promise in
print("\(Thread.current): f1() value = \(value)")
promise(.success(value + 1))
}
}
func f2(_ value: Int) -> Future<Int, Error> {
Future { promise in
print("\(Thread.current): f2() value = \(value)")
promise(.success(value + 1))
}
}
let a1 = Just(0)
.subscribe(on: DispatchQueue.global())
.flatMap(f1)
.flatMap(f2)
.receive(on: DispatchQueue.main)
.sink { _ in
print("\(Thread.current): completed")
} receiveValue: { value in
print("\(Thread.current): sink() value = \(value)")
}
UPDATE Here is how my solution looks like now:
extension Publishers.SubscribeOn {
func myFlatMap<P>(_ transform: @escaping (Self.Output) -> P) -> Publishers.FlatMap<P, Publishers.SetFailureType<Self, P.Failure>> where P: Publisher {
Publishers.FlatMap<P, Publishers.SetFailureType<Self, P.Failure>>(
upstream: Publishers.SetFailureType<Self, P.Failure>(upstream: self),
maxPublishers: .unlimited,
transform: transform
)
}
}