Skip to content

Instantly share code, notes, and snippets.

@Nemesisprime
Created March 22, 2020 01:26
Show Gist options
  • Save Nemesisprime/f4f6039fefe8814c459940e3473a38a0 to your computer and use it in GitHub Desktop.
Save Nemesisprime/f4f6039fefe8814c459940e3473a38a0 to your computer and use it in GitHub Desktop.
NSControl Publisher
import Cocoa
import Combine
class ControlPublisher<T: NSControl>: Publisher {
typealias Control = NSControl
typealias Output = Control
typealias Failure = Never
let subject = PassthroughSubject<Output, Failure>()
init(control: NSControl) {
control.target = self
control.action = #selector(controlAction)
}
@objc private func controlAction(sender: NSControl) {
subject.send(sender)
}
func receive<S>(subscriber: S) where S :
Subscriber,
ControlPublisher.Failure == S.Failure,
ControlPublisher.Output == S.Input {
subject.receive(subscriber: subscriber)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment