Last active
July 6, 2017 16:02
-
-
Save sergdort/8c6544506c7a81947a06a865fb5a452a to your computer and use it in GitHub Desktop.
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
public class Observable<Element>: ObservableType { | |
public typealias E = Element | |
private let _subscribeHandler: (Observer<Element>) -> Disposable | |
public init(_ subscribtionClosure: @escaping (Observer<Element>) -> Disposable) { | |
_subscribeHandler = subscribtionClosure | |
} | |
public func subscribe<O : ObserverType>(observer: O) -> Disposable where O.E == E { | |
let composite = CompositeDisposable() | |
let subscription = _subscribeHandler(Observer { (event) in | |
observer.on(event: event) | |
switch event { | |
case .error, .completed: | |
composite.dispose() | |
default: | |
break | |
} | |
}) | |
composite.add(disposable: subscription) | |
return composite | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment