Skip to content

Instantly share code, notes, and snippets.

@inamiy
Created November 7, 2017 12:41
Show Gist options
  • Save inamiy/de1a495ec198b7f9b504b9598999e6ed to your computer and use it in GitHub Desktop.
Save inamiy/de1a495ec198b7f9b504b9598999e6ed to your computer and use it in GitHub Desktop.
Rx + more generic observable
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> { }
@inamiy
Copy link
Author

inamiy commented Apr 1, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment