Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. import Foundation
  2. import Combine
  3. import SwiftUI
  4.  
  5. extension Cancellable {
  6. func erase() -> AnyCancellable {
  7. return AnyCancellable(self)
  8. }
  9. }
  10.  
  11. @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
  12. class BindablePublisher<Output>: BindableObject {
  13. var bag: [AnyCancellable] = []
  14. let didChange: AnyPublisher<Output, Never>
  15. public internal(set) var value: Output?
  16.  
  17. public init<PublisherType: Publisher>
  18. (_ publisher: PublisherType)
  19. where Output == PublisherType.Output, PublisherType.Failure == Never {
  20. self.didChange = publisher.eraseToAnyPublisher()
  21. self.didChange
  22. .receive(on: RunLoop.main)
  23. .sink { self.value = $0 }
  24. .erase()
  25. .store(in: &bag)
  26. }
  27. }
  28.  
  29. @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
  30. extension Publisher where Failure == Never {
  31. func bindableObject() -> BindablePublisher<Output> {
  32. return BindablePublisher(self)
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement