Created
May 22, 2018 15:17
-
-
Save bobgodwinx/54011068736f7c46c8279738747f1e0c to your computer and use it in GitHub Desktop.
TwowayBinding
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
| //MARK: Two-way Bindings | |
| extension ControlProperty { | |
| func twoWayBindTo(_ variable: Variable<E>) -> Disposable { | |
| let bindUI = variable.asObservable().bind(to: self) | |
| let bindVariable = subscribe( | |
| onNext: {value in | |
| variable.value = value | |
| }, onCompleted: { | |
| bindUI.dispose() | |
| }) | |
| return CompositeDisposable(bindUI, bindVariable) | |
| } | |
| func twoWayBindTo(_ variable: Variable<E>, dueTime: TimeInterval) -> Disposable { | |
| let bindUI = variable.asObservable().bind(to: self) | |
| let bindVariable = debounce(dueTime, scheduler: MainScheduler.instance) | |
| .subscribe( | |
| onNext: {value in | |
| variable.value = value | |
| }, onCompleted: { | |
| bindUI.dispose() | |
| }) | |
| return CompositeDisposable(bindUI, bindVariable) | |
| } | |
| func twoWayBindTo(_ variable: Variable<E?>) -> Disposable { | |
| let bindUI = variable.asObservable().unwrap().bind(to: self) | |
| let bindVariable = subscribe( | |
| onNext: {value in | |
| variable.value = value | |
| }, onCompleted: { | |
| bindUI.dispose() | |
| }) | |
| return CompositeDisposable(bindUI, bindVariable) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment