Skip to content

Instantly share code, notes, and snippets.

@ytyubox
Last active December 21, 2021 08:31
Show Gist options
  • Save ytyubox/d0e64db68eaea4cd594c38672e88e3bc to your computer and use it in GitHub Desktop.
Save ytyubox/d0e64db68eaea4cd594c38672e88e3bc to your computer and use it in GitHub Desktop.
UIControlSubscription.swift
import UIKit
import Combine
protocol CombineCompatible { }
extension UIControl: CombineCompatible { }
extension CombineCompatible where Self: UIControl {
func publisher(for events: UIControl.Event) -> UIControlPublisher<UIControl> {
return UIControlPublisher(control: self, events: events)
}
}
import UIKit
import Combine
struct UIControlPublisher<Control: UIControl>: Publisher {
typealias Output = Control
typealias Failure = Never
let control: Control
let controlEvents: UIControl.Event
init(control: Control, events: UIControl.Event) {
self.control = control
self.controlEvents = events
}
func receive<S>(subscriber: S) where S : Subscriber, S.Failure == UIControlPublisher.Failure, S.Input == UIControlPublisher.Output {
let subscription = UIControlSubscription(subscriber: subscriber, control: control, event: controlEvents)
subscriber.receive(subscription: subscription)
}
}
import UIKit
import Combine
final class UIControlSubscription<SubscriberType: Subscriber,
Control: UIControl>: Subscription
where SubscriberType.Input == Control {
private var subscriber: SubscriberType?
private let control: Control
init(subscriber: SubscriberType, control: Control, event: UIControl.Event) {
self.subscriber = subscriber
self.control = control
control.addTarget(self, action: #selector(eventHandler), for: event)
}
func request(_ demand: Subscribers.Demand) {
// We do nothing here as we only want to send events when they occur.
// See, for more info: https://developer.apple.com/documentation/combine/subscribers/demand
}
func cancel() {
subscriber = nil
}
@objc private func eventHandler() {
_ = subscriber?.receive(control)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment