Skip to content

Commit

Permalink
Update RxCocoa multi-observer bind
Browse files Browse the repository at this point in the history
  • Loading branch information
freak4pc authored and kzaher committed Apr 11, 2019
1 parent cca8e4b commit 345bcaa
Showing 1 changed file with 34 additions and 28 deletions.
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

0 comments on commit 345bcaa

Please sign in to comment.