Last active
August 22, 2018 08:07
-
-
Save mortenbekditlevsen/194ab2f0c4dd6438c7e9ae90963c8b63 to your computer and use it in GitHub Desktop.
DatabaseQuery+rx.swift
This file contains hidden or 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
extension Reactive where Base: DatabaseQuery { | |
func observeSingleEvent<T>(of type: DataEventType) -> Single<T> where T: Decodable { | |
return Single.create { single in | |
self.base.observeSingleEvent(of: type, with: { (result: DecodeResult<T>) in | |
single(result.asSingleEvent) | |
}) | |
return Disposables.create() | |
} | |
} | |
func observe<T>(eventType: DataEventType) -> Observable<DecodeResult<T>> where T: Decodable { | |
return Observable.create { observer in | |
let handle = self.base.observe(eventType: eventType, with: { (result: DecodeResult<T>) in | |
observer.onNext(result) | |
}) | |
return Disposables.create { | |
self.base.removeObserver(withHandle: handle) | |
} | |
} | |
} | |
} | |
extension DecodeResult { | |
// A small convenience to re-wrap a `DecodeResult` as a `SingleEvent` | |
var asSingleEvent: SingleEvent<Value> { | |
switch self { | |
case .success(let v): | |
return .success(v) | |
case .failure(let e): | |
return .error(e) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment