Last active
September 19, 2018 15:19
-
-
Save Athosone/7fae244e09dd57c7ac28227e651adda1 to your computer and use it in GitHub Desktop.
Example of rx extension
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 Observable { | |
// MARK: For completionBlock(Element, Error) template methods | |
public static func fromAsync(_ asyncRequest: @escaping (@escaping (Element?, Error?) -> Void) -> Void) -> Observable<Element> { | |
return Observable.create { (o) -> Disposable in | |
asyncRequest { res, error in | |
if let err = error { | |
o.onError(err) | |
return | |
} | |
guard let element = res else { | |
o.onCompleted() | |
return | |
} | |
o.onNext(element) | |
o.onCompleted() | |
} | |
return Disposables.create {} | |
} | |
} | |
public static func fromAsync<T>(_ asyncRequest: @escaping (T, @escaping (Element?, Error?) -> Void) -> Void) -> (T) -> Observable<Element> { | |
return { param1 in Observable.fromAsync(curry(function: asyncRequest)(param1)) } | |
} | |
public static func fromAsync<T, U>(_ asyncRequest: @escaping (T, U, @escaping(Element?, Error?) -> Void) -> Void) -> (T) -> (U) -> Observable<Element> { | |
return { (param1: T) in | |
return { (param2: U) in | |
Observable.fromAsync(curry(function: asyncRequest)(param1)(param2)) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment