Created
March 22, 2020 01:26
-
-
Save Nemesisprime/f4f6039fefe8814c459940e3473a38a0 to your computer and use it in GitHub Desktop.
NSControl Publisher
This file contains hidden or 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 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