Created
November 9, 2021 10:00
-
-
Save prat14k/759c457d462c4b3bd6d2c6041bb30293 to your computer and use it in GitHub Desktop.
An extension for Combine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
import Combine | |
extension UIControl { | |
class Publisher: Combine.Publisher { | |
typealias Output = UIControl.Event | |
typealias Failure = Never | |
let subject = PassthroughSubject<Output, Failure>() | |
init(control: UIControl, events: [UIControl.Event]) { | |
events.forEach { control.addTarget(self, action: #selector(actionHandler), for: $0) } | |
} | |
@objc func actionHandler(sender: UIControl, forEvent event: UIControl.Event) { | |
subject.send(event) | |
} | |
func receive<S: Subscriber>(subscriber: S) where Failure == S.Failure, Output == S.Input { | |
subject.receive(subscriber: subscriber) | |
} | |
} | |
} | |
extension UIControl { | |
func publisher(for events: [UIControl.Event]) -> AnyPublisher<UIControl.Event, Never> { | |
return UIControl.Publisher(control: self, events: events) | |
.eraseToAnyPublisher() | |
} | |
} | |
protocol UIKitCombinable {} | |
extension UIKitCombinable { | |
func assign<Input>(_ keyPath: ReferenceWritableKeyPath<Self, Input>) -> AnySubscriber<Input, Never> { | |
return AnySubscriber(Subscribers.Assign(object: self, keyPath: keyPath)) | |
} | |
} | |
extension NSObject: UIKitCombinable {} | |
precedencegroup BindingPrecedence { | |
associativity: right | |
higherThan: AssignmentPrecedence | |
} | |
infix operator <~ : BindingPrecedence | |
public func <~ <Value, S: Subscriber, P: Publisher>(subscriber: S, publisher: P) | |
where S.Input == Value, P.Output == Value, S.Failure == P.Failure { | |
publisher.subscribe(subscriber) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment