Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- import Combine
- import SwiftUI
- extension Cancellable {
- func erase() -> AnyCancellable {
- return AnyCancellable(self)
- }
- }
- @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
- class BindablePublisher<Output>: BindableObject {
- var bag: [AnyCancellable] = []
- let didChange: AnyPublisher<Output, Never>
- public internal(set) var value: Output?
- public init<PublisherType: Publisher>
- (_ publisher: PublisherType)
- where Output == PublisherType.Output, PublisherType.Failure == Never {
- self.didChange = publisher.eraseToAnyPublisher()
- self.didChange
- .receive(on: RunLoop.main)
- .sink { self.value = $0 }
- .erase()
- .store(in: &bag)
- }
- }
- @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
- extension Publisher where Failure == Never {
- func bindableObject() -> BindablePublisher<Output> {
- return BindablePublisher(self)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement