Skip to content

Instantly share code, notes, and snippets.

@sergdort
Last active July 6, 2017 16:02
Show Gist options
  • Save sergdort/8c6544506c7a81947a06a865fb5a452a to your computer and use it in GitHub Desktop.
Save sergdort/8c6544506c7a81947a06a865fb5a452a to your computer and use it in GitHub Desktop.
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