Проверка шаблона шины для iOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
675B

  1. import Combine
  2. public extension Bus {
  3. final class Async<Src, Dst> {
  4. let v = PassthroughSubject<Src, Never>()
  5. var subscriptions = [AnyCancellable]()
  6. public init(
  7. _ handler: @escaping ((Src) -> Dst?),
  8. _ src: String,
  9. _ dst: String
  10. ) {
  11. // Вход.
  12. Bus.receiveSync(
  13. [src],
  14. { [weak self] _, v in self?.v.send(v) },
  15. &subscriptions
  16. )
  17. // Выход.
  18. Bus.sendSync(
  19. dst,
  20. v
  21. .compactMap { (v: Src) in handler(v) }
  22. // Асинхронно.
  23. .receive(on: DispatchQueue.main)
  24. .eraseToAnyPublisher(),
  25. &subscriptions
  26. )
  27. }
  28. }
  29. }