Created
November 7, 2017 12:41
-
-
Save inamiy/de1a495ec198b7f9b504b9598999e6ed to your computer and use it in GitHub Desktop.
Rx + more generic observable
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
protocol EventProtocol { | |
var isStopEvent: Bool { get } | |
} | |
enum InfiniteEvent<Element>: EventProtocol { | |
case element(Element) // many | |
var isStopEvent: Bool { return false } | |
} | |
enum SingleEvent<Element, Error>: EventProtocol { | |
case element(Element) | |
case error(Error) | |
var isStopEvent: Bool { return true } | |
} | |
enum Event<Element, Error>: EventProtocol { | |
case element(Element) | |
case error(Error) | |
case completed | |
var isStopEvent: Bool { | |
switch self { | |
case .element: return false | |
default: return true | |
} | |
} | |
} | |
// ... and tons of other varieties of events! (e.g. non-stopping errors like Bacon.js) | |
class _Observable<Ev: EventProtocol, Element, Error> {} | |
typealias InfiniteObservable<Element, Error> = _Observable<InfiniteEvent<Element>, Element, Error> | |
typealias SingleObservable<Element, Error> = _Observable<SingleEvent<Element, Error>, Element, Error> | |
typealias Observable<Element, Error> = _Observable<Event<Element, Error>, Element, Error> | |
// For InfiniteObservable (UI binding is only allowed from here). | |
extension _Observable where Ev == InfiniteEvent<Element> { } | |
// For SingleObservable. | |
extension _Observable where Ev == SingleEvent<Element, Error> { } | |
// For Observable. | |
extension _Observable where Ev == Event<Element, Error> { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First discussed in ReactiveX/RxSwift#1470
ReactiveSwift proposal: Introduce Generalized Event by inamiy · Pull Request #722 · ReactiveCocoa/ReactiveSwift