From f4636f0ea39a9cf59572a96394114950ccea8d50 Mon Sep 17 00:00:00 2001 From: Meng Li Date: Mon, 22 Apr 2019 15:10:21 +0900 Subject: [PATCH] Disposed by Array[Any]. --- Example/RxBinding/ViewController.swift | 8 ++++++++ RxBinding/Classes/DisposedBy.swift | 23 ++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Example/RxBinding/ViewController.swift b/Example/RxBinding/ViewController.swift index 18a1fa7..a2a6825 100644 --- a/Example/RxBinding/ViewController.swift +++ b/Example/RxBinding/ViewController.swift @@ -51,10 +51,18 @@ class ViewController: UIViewController { view.addSubview(characterCountLabel2) createConstraints() + disposeBag ~ [ + viewModel.text <~> textFeild.rx.text, + viewModel.uppercaseText ~> label.rx.text, + viewModel.charactersCount ~> [characterCountLabel1, characterCountLabel2].map { $0.rx.text } + ] + + /** disposeBag ~ viewModel.text <~> textFeild.rx.text ~ viewModel.uppercaseText ~> label.rx.text ~ viewModel.charactersCount ~> [characterCountLabel1, characterCountLabel2].map { $0.rx.text } + */ /** viewModel.text <~> textFeild.rx.text ~ diff --git a/RxBinding/Classes/DisposedBy.swift b/RxBinding/Classes/DisposedBy.swift index 16c980a..45ff89b 100644 --- a/RxBinding/Classes/DisposedBy.swift +++ b/RxBinding/Classes/DisposedBy.swift @@ -39,11 +39,32 @@ extension DisposeBag { disposable.disposed(by: disposeBag) } - @discardableResult public static func ~ (disposeBag: DisposeBag, disposable: Disposable) -> DisposeBag { + @discardableResult + public static func ~ (disposeBag: DisposeBag, disposable: Disposable) -> DisposeBag { disposable.disposed(by: disposeBag) return disposeBag } + public static func ~ (disposeBag: DisposeBag, disposables: [Any]) { + disposables.map { obj -> [Disposable] in + switch obj.self { + case is Disposable: + return [obj as! Disposable] + case is [Disposable]: + return obj as! [Disposable] + default: + return [] + } + }.reduce([]) { $0 + $1 }.forEach { + print($0) + $0.disposed(by: disposeBag) + } + } + + public static func ~ (disposables: [Any], disposeBag: DisposeBag) { + disposeBag ~ disposables + } + } extension Array where Element == Disposable {