|
12345678910111213141516171819202122232425262728293031 |
- import Combine
-
- public extension Bus {
- final class Async<Src, Dst> {
- let v = PassthroughSubject<Src, Never>()
- var subscriptions = [AnyCancellable]()
-
- public init(
- _ handler: @escaping ((Src) -> Dst?),
- _ src: String,
- _ dst: String
- ) {
- // Вход.
- Bus.receiveSync(
- [src],
- { [weak self] _, v in self?.v.send(v) },
- &subscriptions
- )
- // Выход.
- Bus.sendSync(
- dst,
- v
- .compactMap { (v: Src) in handler(v) }
- // Асинхронно.
- .receive(on: DispatchQueue.main)
- .eraseToAnyPublisher(),
- &subscriptions
- )
- }
- }
- }
|