Last active
July 4, 2019 09:01
-
-
Save Thomvis/0087f98d61025aaf705fff72e3855ce7 to your computer and use it in GitHub Desktop.
Revisions
-
Thomvis revised this gist
Jul 4, 2019 . 1 changed file with 28 additions and 12 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,26 +1,42 @@ class ActionTarget { let handler: () -> Void init(handler: @escaping () -> Void) { self.handler = handler } @IBAction func handle() { handler() } } struct AnySubscription: Subscription { func request(_ demand: Subscribers.Demand) { _request(demand) } func cancel() { _cancel() } var combineIdentifier: CombineIdentifier = CombineIdentifier(NSUUID()) let _request: (Subscribers.Demand) -> Void let _cancel: () -> Void } extension UIControl { func eventPublisher(for event: UIControl.Event) -> AnyPublisher<UIControl, Never> { return AnyPublisher { subscriber in var target: ActionTarget? subscriber.receive(subscription: AnySubscription(_request: { _ in }, _cancel: { target = nil })) target = ActionTarget { _ = subscriber.receive(self) } self.addTarget(target, action: #selector(ActionTarget.handle), for: event) } } } -
Thomvis revised this gist
Jul 2, 2019 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -15,8 +15,9 @@ class ActionTarget { extension UIControl { var eventPublisher: AnyPublisher<UIControl.Event, Never> { return AnyPublisher { subscriber in subscriber.receive(subscription: Subscriptions.empty) let target = ActionTarget { _ = subscriber.receive($0) } self.addTarget(target, action: #selector(ActionTarget.handle), for: .allTouchEvents) -
Thomvis renamed this gist
Jul 2, 2019 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Thomvis created this gist
Jul 2, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ class ActionTarget { let handler: (Any) -> Void init<A>(handler: @escaping (A) -> Void) { self.handler = { handler($0 as! A) } } @objc func handle(_ action: Any) { handler(action) } } extension UIControl { var eventPublisher: AnyPublisher<UIControl.Event, Never> { return AnyPublisher { subscriber in let target = ActionTarget { subscriber.receive($0) } self.addTarget(target, action: #selector(ActionTarget.handle), for: .allTouchEvents) } } }