-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added new bind(to: [ObserverType]) operator to bind an observable to …
…a collection of obsevers
- Loading branch information
Matthew Crenshaw
committed
Jul 16, 2018
1 parent
0ca281f
commit 050132d
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// bindCollection+RxCocoa.swift | ||
// RxSwiftExt | ||
// | ||
// Created by Matthew Crenshaw on 7/16/18. | ||
// Copyright © 2018 RxSwift Community. All rights reserved. | ||
// | ||
|
||
import RxSwift | ||
|
||
extension ObservableType { | ||
/** | ||
Creates new shared subscriptions and sends elements to collection of observers. | ||
|
||
- parameter to: Collection of observers that receives events. | ||
- returns: Disposable object that can be used to unsubscribe the observers. | ||
*/ | ||
func bind<O>(to observers: [O]) -> Disposable where O: ObserverType, Self.E == O.E { | ||
let shared = self.share() | ||
let disposables = observers.map(shared.bind(to:)) | ||
return CompositeDisposable(disposables: disposables) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// BindCollectionTests+RxCocoa.swift | ||
// RxSwiftExt | ||
// | ||
// Created by Matthew Crenshaw on 7/16/18. | ||
// Copyright © 2018 RxSwift Community. All rights reserved. | ||
// | ||
|
||
@testable import RxSwiftExt | ||
|
||
import XCTest | ||
import RxSwift | ||
import RxTest | ||
|
||
class BindCollectionTests: XCTestCase { | ||
func testBindCollection() { | ||
let values = ["one", "two", "three", "four"] | ||
|
||
let scheduler = TestScheduler(initialClock: 0) | ||
let observers = [scheduler.createObserver(String.self), | ||
scheduler.createObserver(String.self), | ||
scheduler.createObserver(String.self), | ||
scheduler.createObserver(String.self)] | ||
|
||
_ = Observable.from(values).bind(to: observers) | ||
|
||
scheduler.start() | ||
|
||
let correct = [next(0, "one"), | ||
next(0, "two"), | ||
next(0, "three"), | ||
next(0, "four"), | ||
completed(0)] | ||
|
||
for observer in observers { | ||
XCTAssertEqual(observer.events, correct) | ||
} | ||
} | ||
} |