Skip to content

Commit

Permalink
Update RxRelay multi-relay 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 345bcaa commit a06cd3e
Showing 1 changed file with 50 additions and 28 deletions.
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
}
}
}
}

0 comments on commit a06cd3e

Please sign in to comment.