Проверка шаблона шины для 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.

Bus.Debounce.swift 692B

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