Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow binding to multiple observers #1702

Merged
merged 3 commits into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 34 additions & 28 deletions RxCocoa/Common/Observable+Bind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,47 @@
// Copyright © 2015 Krunoslav Zaher. All rights reserved.
//


import RxSwift

extension ObservableType {

/**
Creates new subscription and sends elements to observer.

In this form it's equivalent to `subscribe` method, but it communicates intent better, and enables
writing more consistent binding code.

- parameter to: Observer that receives events.
- returns: Disposable object that can be used to unsubscribe the observer.
*/
public func bind<O: ObserverType>(to observer: O) -> Disposable where O.E == E {
return self.subscribe(observer)
Creates new subscription and sends elements to observer(s).
In this form, it's equivalent to the `subscribe` method, but it better conveys intent, and enables
writing more consistent binding code.
- parameter to: Observers to receives events.
- returns: Disposable object that can be used to unsubscribe the observers.
*/
public func bind<O: ObserverType>(to observers: O...) -> Disposable where O.E == E {
return self.bind(to: observers)
}

/**
Creates new subscription and sends elements to observer.

In this form it's equivalent to `subscribe` method, but it communicates intent better, and enables
Creates new subscription and sends elements to observer(s).
In this form, it's equivalent to the `subscribe` method, but it better conveys intent, and enables
writing more consistent binding code.
- parameter to: Observers to receives events.
- returns: Disposable object that can be used to unsubscribe the observers.
*/
public func bind<O: ObserverType>(to observers: O...) -> Disposable where O.E == E? {
return self.map { $0 as E? }.bind(to: observers)
}

- parameter to: Observer that receives events.
- returns: Disposable object that can be used to unsubscribe the observer.
/**
Creates new subscription and sends elements to observer(s).
In this form, it's equivalent to the `subscribe` method, but it better conveys intent, and enables
writing more consistent binding code.
- parameter to: Observers to receives events.
- returns: Disposable object that can be used to unsubscribe the observers.
*/
public func bind<O: ObserverType>(to observer: O) -> Disposable where O.E == E? {
return self.map { $0 }.subscribe(observer)
private func bind<O: ObserverType>(to observers: [O]) -> Disposable where O.E == E {
return self.subscribe { event in
observers.forEach { $0.on(event) }
}
}

/**
Subscribes to observable sequence using custom binder function.

- parameter to: Function used to bind elements from `self`.
- returns: Object representing subscription.
*/
Expand All @@ -50,26 +57,25 @@ extension ObservableType {
/**
Subscribes to observable sequence using custom binder function and final parameter passed to binder function
after `self` is passed.

public func bind<R1, R2>(to binder: Self -> R1 -> R2, curriedArgument: R1) -> R2 {
return binder(self)(curriedArgument)
}

- parameter to: Function used to bind elements from `self`.
- parameter curriedArgument: Final argument passed to `binder` to finish binding process.
- returns: Object representing subscription.
*/
public func bind<R1, R2>(to binder: (Self) -> (R1) -> R2, curriedArgument: R1) -> R2 {
return binder(self)(curriedArgument)
}


/**
Subscribes an element handler to an observable sequence.


/**
Subscribes an element handler to an observable sequence.
In case error occurs in debug mode, `fatalError` will be raised.
In case error occurs in release mode, `error` will be logged.

- parameter onNext: Action to invoke for each element in the observable sequence.
- returns: Subscription object used to unsubscribe from the observable sequence.
*/
Expand Down
78 changes: 50 additions & 28 deletions RxRelay/Observable+Bind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,43 @@ import RxSwift

extension ObservableType {
/**
Creates new subscription and sends elements to publish relay.
Creates new subscription and sends elements to publish relay(s).
In case error occurs in debug mode, `fatalError` will be raised.
In case error occurs in release mode, `error` will be logged.
- parameter to: Target publish relays for sequence elements.
- returns: Disposable object that can be used to unsubscribe the observer.
*/
public func bind(to relays: PublishRelay<E>...) -> Disposable {
return bind(to: relays)
}

/**
Creates new subscription and sends elements to publish relay(s).

In case error occurs in debug mode, `fatalError` will be raised.
In case error occurs in release mode, `error` will be logged.

- parameter to: Target publish relay for sequence elements.
- parameter to: Target publish relays for sequence elements.
- returns: Disposable object that can be used to unsubscribe the observer.
*/
public func bind(to relays: PublishRelay<E?>...) -> Disposable {
return self.map { $0 as E? }.bind(to: relays)
}

/**
Creates new subscription and sends elements to publish relay(s).
In case error occurs in debug mode, `fatalError` will be raised.
In case error occurs in release mode, `error` will be logged.
- parameter to: Target publish relays for sequence elements.
- returns: Disposable object that can be used to unsubscribe the observer.
*/
public func bind(to relay: PublishRelay<E>) -> Disposable {
return self.subscribe { e in
private func bind(to relays: [PublishRelay<E>]) -> Disposable {
return subscribe { e in
switch e {
case let .next(element):
relay.accept(element)
relays.forEach {
$0.accept(element)
}
case let .error(error):
rxFatalErrorInDebug("Binding error to publish relay: \(error)")
case .completed:
Expand All @@ -32,50 +56,48 @@ extension ObservableType {
}

/**
Creates new subscription and sends elements to publish relay.

Creates new subscription and sends elements to behavior relay(s).
In case error occurs in debug mode, `fatalError` will be raised.
In case error occurs in release mode, `error` will be logged.

- parameter to: Target publish relay for sequence elements.
- parameter to: Target behavior relay for sequence elements.
- returns: Disposable object that can be used to unsubscribe the observer.
*/
public func bind(to relay: PublishRelay<E?>) -> Disposable {
return self.map { $0 as E? }.bind(to: relay)
public func bind(to relays: BehaviorRelay<E>...) -> Disposable {
return self.bind(to: relays)
}

/**
Creates new subscription and sends elements to behavior relay.
Creates new subscription and sends elements to behavior relay(s).

In case error occurs in debug mode, `fatalError` will be raised.
In case error occurs in release mode, `error` will be logged.

- parameter to: Target behavior relay for sequence elements.
- returns: Disposable object that can be used to unsubscribe the observer.
*/
public func bind(to relay: BehaviorRelay<E>) -> Disposable {
return self.subscribe { e in
switch e {
case let .next(element):
relay.accept(element)
case let .error(error):
rxFatalErrorInDebug("Binding error to behavior relay: \(error)")
case .completed:
break
}
}
public func bind(to relays: BehaviorRelay<E?>...) -> Disposable {
return self.map { $0 as E? }.bind(to: relays)
}

/**
Creates new subscription and sends elements to behavior relay.

Creates new subscription and sends elements to behavior relay(s).
In case error occurs in debug mode, `fatalError` will be raised.
In case error occurs in release mode, `error` will be logged.

- parameter to: Target behavior relay for sequence elements.
- returns: Disposable object that can be used to unsubscribe the observer.
*/
public func bind(to relay: BehaviorRelay<E?>) -> Disposable {
return self.map { $0 as E? }.bind(to: relay)
private func bind(to relays: [BehaviorRelay<E>]) -> Disposable {
return subscribe { e in
switch e {
case let .next(element):
relays.forEach {
$0.accept(element)
}
case let .error(error):
rxFatalErrorInDebug("Binding error to behavior relay: \(error)")
case .completed:
break
}
}
}
}
4 changes: 4 additions & 0 deletions Sources/AllTestz/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1139,10 +1139,14 @@ final class ObservableRelayBindTest_ : ObservableRelayBindTest, RxTestCase {

static var allTests: [(String, (ObservableRelayBindTest_) -> () -> Void)] { return [
("testBindToPublishRelay", ObservableRelayBindTest.testBindToPublishRelay),
("testBindToPublishRelays", ObservableRelayBindTest.testBindToPublishRelays),
("testBindToOptionalPublishRelay", ObservableRelayBindTest.testBindToOptionalPublishRelay),
("testBindToOptionalPublishRelays", ObservableRelayBindTest.testBindToOptionalPublishRelays),
("testBindToPublishRelayNoAmbiguity", ObservableRelayBindTest.testBindToPublishRelayNoAmbiguity),
("testBindToBehaviorRelay", ObservableRelayBindTest.testBindToBehaviorRelay),
("testBindToBehaviorRelays", ObservableRelayBindTest.testBindToBehaviorRelays),
("testBindToOptionalBehaviorRelay", ObservableRelayBindTest.testBindToOptionalBehaviorRelay),
("testBindToOptionalBehaviorRelays", ObservableRelayBindTest.testBindToOptionalBehaviorRelays),
("testBindToBehaviorRelayNoAmbiguity", ObservableRelayBindTest.testBindToBehaviorRelayNoAmbiguity),
] }
}
Expand Down
51 changes: 50 additions & 1 deletion Tests/RxCocoaTests/Observable+BindTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,31 @@ extension ObservableBindTest {
])
}

func testBindToObservers() {
var events1: [Recorded<Event<Int>>] = []
var events2: [Recorded<Event<Int>>] = []

let observer1: AnyObserver<Int> = AnyObserver { event in
events1.append(Recorded(time: 0, value: event))
}

let observer2: AnyObserver<Int> = AnyObserver { event in
events2.append(Recorded(time: 0, value: event))
}

_ = Observable.just(1).bind(to: observer1, observer2)

XCTAssertEqual(events1, [
.next(1),
.completed()
])

XCTAssertEqual(events2, [
.next(1),
.completed()
])
}

func testBindToOptionalObserver() {
var events: [Recorded<Event<Int?>>] = []

Expand All @@ -49,6 +74,31 @@ extension ObservableBindTest {
}
}

func testBindToOptionalObservers() {
var events1: [Recorded<Event<Int?>>] = []
var events2: [Recorded<Event<Int?>>] = []

let observer1: AnyObserver<Int?> = AnyObserver { event in
events1.append(Recorded(time: 0, value: event))
}

let observer2: AnyObserver<Int?> = AnyObserver { event in
events2.append(Recorded(time: 0, value: event))
}

_ = (Observable.just(1) as Observable<Int>).bind(to: observer1, observer2)

XCTAssertEqual(events1, [
.next(1),
.completed()
])

XCTAssertEqual(events2, [
.next(1),
.completed()
])
}

func testBindToOptionalObserverNoAmbiguity() {
var events: [Recorded<Event<Int?>>] = []

Expand Down Expand Up @@ -102,5 +152,4 @@ extension ObservableBindTest {
XCTAssertEqual(result, 4)
d.dispose()
}

}
Loading