Skip to content

Commit

Permalink
Add sink operator.
Browse files Browse the repository at this point in the history
  • Loading branch information
srdanrasic committed Jun 22, 2019
1 parent 844dd47 commit ef3aa40
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Sources/SignalProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,20 @@ extension SignalProtocol {
return (self as? Signal<Element, Error>) ?? Signal(self.observe)
}
}

extension SignalProtocol {

/// Attaches a subscriber (observer) with closure-based behavior.
public func sink(receiveCompletion: ((Subscribers.Completion<Error>) -> Void)? = nil, receiveValue: @escaping ((Element) -> Void)) -> Disposable {
return observe { event in
switch event {
case .next(let element):
receiveValue(element)
case .failed(let error):
receiveCompletion?(.failure(error))
case .completed:
receiveCompletion?(.finished)
}
}
}
}

0 comments on commit ef3aa40

Please sign in to comment.