Firstly, let’s see the code.
import Foundation
import Combine
struct Feedback<State, Event> {
let run: (AnyPublisher<State, Never>) -> AnyPublisher<Event, Never>
}
extension Feedback {
init<Effect: Publisher>
(effects: @escaping (State) -> Effect)
where Effect.Output == Event, Effect.Failure == Never
{
self.run = { statePub -> AnyPublisher<Event, Never> in
statePub
.map { effects($0) }
.switchToLatest()
.eraseToAnyPublisher()
}
}
}
Secodly, let’s see the diagram.
Finally, let’s see the explanation.
To initialize Feedback, we need to pass a variable called effects
which is to turn State into Effect.
Once initialized, the run function is settled since effects is one part in the pipeline.
You can call the run
function and pass a AnyPublisher<State, Never>
to get a AnyPublisher<Event, Never>