Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. import Foundation
  2. import Combine
  3.  
  4. struct MyPublisher: Publisher {
  5. typealias Output = Int
  6. typealias Failure = Error
  7.  
  8. func receive<S>(subscriber: S) where S : Subscriber,
  9. Failure == S.Failure,
  10. Output == S.Input {
  11. subscriber.receive(1)
  12. print("called 1")
  13. subscriber.receive(2)
  14. print("called 2")
  15. subscriber.receive(completion: .finished)
  16. print("called finish")
  17. }
  18. }
  19.  
  20. MyPublisher()
  21. .receive(on: RunLoop.main)
  22. // .throttle(for: .milliseconds(1000), scheduler: RunLoop.main, latest: false)
  23. // .debounce(for: .milliseconds(1000), scheduler: RunLoop.main)
  24. // .delay(for: .milliseconds(1000), scheduler: DispatchQueue.main)
  25. .print()
  26. .sink(receiveCompletion: { completion in
  27. switch completion {
  28. case .finished:
  29. print("finished")
  30. case .failure(let error):
  31. print("error:\(error)")
  32. }
  33. }, receiveValue: { num in
  34. print("\(num)")
  35. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement