Return Mono.empty() if a value is found but without executing other steps - reactive-programming

This problem is very hard to describe as text, so if the title doesn't fit the requirement, sorry for that.
I would like to achieve a specific goal with Project Reactor flux and mono, which seems to be pretty simple at first look.
A code example, in the "blocking-way" will be better than a long description:
fun findGroupToCreateBlocking(userId: UUID, groupLabel: String): Optional<LinkUserToGroup> {
val group = lib.findGroupsOfUser(userId)
.flatMapIterable { it.items }
.filter { it.label == groupLabel }
.toMono()
.blockOptional()
if(group.isPresent) {
return Optional.empty()
}
return lib.searchGroups(groupLabel)
.flatMapIterable { it.items }
.filter { it.label == groupLabel }
.toMono()
.map { LinkUserToGroup(userId, it.id) }
.switchIfEmpty { IllegalStateException("Group $groupLabel not found").toMono() }
.blockOptional()
}
I try to achieve the same thing without the block part of course. I ended up with the following code:
fun findGroupToCreateReactive(userId: UUID, groupLabel: String): Mono<LinkUserToGroup> =
lib.findGroupsOfUser(userId)
.flatMapIterable { it.items }
.filter { it.label == groupLabel }
.toMono()
.map { Optional.of(it) }
.defaultIfEmpty(Optional.empty())
.filter { g -> g.isEmpty }
.flatMap { lib.searchGroups(groupLabel)
.flatMapIterable { it.items }
.toMono()
.map { LinkUserToGroup(userId, it.id) }
.switchIfEmpty { IllegalStateException("Group $groupLabel not found").toMono() }
}
I think (and I'm not the only one 😇) we can do better and not rely on the Optional usage in the middle of the stream... but I didn't find any other solution.
This is the fourth time I fight against this "pattern", so some help would be welcomed!
I've generated a demo project on Gitlab (here) with unit tests for bot reactive and blocking implementation to see if the proposition match the requirement. If you want, you can fork and use the project.

Instead of using a Mono.empty, I used the Flux.hasElement method (like #yossarian), but adding a negation filter. It seems to work as the unit test still pass.
fun findGroupToCreateReactive(userId: UUID, groupLabel: String): Mono<LinkUserToGroup> =
lib.findGroupsOfUser(userId)
.flatMapIterable { it.items }
.map { it.label }
.hasElement(groupLabel)
.filter { g -> !g }
.flatMap { lib.searchGroups(groupLabel)
.flatMapIterable { it.items }
.toMono()
.map { LinkUserToGroup(userId, it.id) }
.switchIfEmpty { IllegalStateException("Group $groupLabel not found").toMono() }
}
Since we want to search groups only if the user doesn't belong to a group, then this is made more explicit with the negation filter.

I didn't find a nice solution with Reactor. However, since you are using Kotlin you might embrace the pattern and create an extension function for it:
fun findGroupToCreateReactive(userId: UUID, groupLabel: String): Mono<LinkUserToGroup> =
lib.findGroupsOfUser(userId)
.flatMapIterable { it.items }
.filter { it.label == groupLabel }
.toMono()
.switchIfEmptyOrEmpty {
lib.searchGroups(groupLabel)
.flatMapIterable { it.items }
.toMono()
.map { LinkUserToGroup(userId, it.id) }
.switchIfEmpty { IllegalStateException("Group $groupLabel not found").toMono() }
}
fun <T> Mono<*>.switchIfEmptyOrEmpty(monoIfEmpty: () -> Mono<T>): Mono<T> =
this.map { Optional.of(it) }
.defaultIfEmpty(Optional.empty())
.filter { g -> g.isEmpty }
.flatMap { monoIfEmpty.invoke() }
Another alternative with hasElement operator:
fun findGroupToCreateReactive(userId: UUID, groupLabel: String): Mono<LinkUserToGroup> =
lib.findGroupsOfUser(userId)
.flatMapIterable { it.items }
.filter { it.label == groupLabel }
.toMono()
.hasElement()
.flatMap { hasElement ->
if (hasElement)
{
return#flatMap Mono.empty<LinkUserToGroup>()
} else
{
lib.searchGroups(groupLabel)
.flatMapIterable { it.items }
.toMono()
.map { LinkUserToGroup(userId, it.id) }
.switchIfEmpty { IllegalStateException("Group $groupLabel not found").toMono() }
}
}
PS: Thanks for the sample repo. That really helped trying out different things!

Related

Observable extension with generic type

Context
I want to wrap the Alamofire.upload into an observable and having info regarding the upload progress.
For that I have created a custom UploadElement that is an enum representing either the progress and the value or the result. So far I have:
enum UploadElement<Result> where Result: Codable {
case progress(Double)
case response(Result)
}
private func buildUploadRequest(url: URL, parts: [Data]) -> Observable<UploadRequest> {
let uploadRequest = manager.upload(
multipartFormData: { multipartFormData in /* build multipart */ },
to: url
)
return Observable.just(uploadRequest)
}
func upload<Result: Codable>(url: URL, parts: [Data]) -> Observable<UploadElement<Result>> {
buildUploadRequest(url: url, parts: parts)
.flatMap { request in
Observable<UploadElement<Result>>.create { observer in
request.response { response in
do {
observer.on(.next(.response(/* decode here */)))
observer.on(.completed)
} catch let error {
observer.on(.error(error))
}
}.uploadProgress { progress in
observer.on(.next(.progress(progress.fractionCompleted)))
}
.resume()
return Disposable.create { request.cancel() }
}
}
}
Now I would like to have an extension on an Observable<UploadEment<Result>> to have a nicer way to be notified.
Basically it would be:
service.upload(url: ..., parts: ...)
.progress { progress in /* */ }
.result { result in /* */ }
.subscribe()
.dispose(by: disposeBag)
To do that I tried:
extension ObservableType where Element == UploadElement<Resource> {
func progress(progressCompletion: #escaping (Double) -> Void) -> Self {
return self.do(onNext: { element in
switch element {
case .progress(let progress): progressCompletion(progress)
case .response: return
}
})
}
func result(resultCompletion: #escaping (Result) -> Void) -> Self {
return self.do(onNext: { element in
switch element {
case .response(let result): resultCompletion(result)
case .progress: return
}
})
}
}
I tried multiple variation of that but the errors that I get are:
Cannot find 'Result in scope'
Reference to generic type ... required argument
Is it possible to achieve something like that?
You just need to move the where clause from class scope down to function scope (shown below).
That said, I don't think breaking out of the monad like this in the middle of a stream is "a nicer way to be notified".
Better would be to break your Observable into two streams and subscribe to each of them:
extension ObservableType {
func progress<Resource>() -> Observable<Double> where Element == UploadElement<Resource> {
self.compactMap { element in
switch element {
case let .progress(progress):
return progress
case .response:
return nil
}
}
}
func result<Resource>() -> Observable<Resource> where Element == UploadElement<Resource> {
self.compactMap { element in
switch element {
case .progress:
return nil
case let .response(resource):
return resource
}
}
}
}
With the above you can now do something like this:
let response = service.upload(url: ..., parts: ...)
.share()
response
.progress()
.subscribe(onNext: { progress in /*...*/ })
.disposed(by: disposeBag)
response
.result()
.subscribe(onNext: { result in /*...*/ })
.dispose(by: disposeBag)
Now you don't have any empty subscribes.
I found something that is working:
extension ObservableType {
func progress<O: Codable>(progressCompletion: #escaping (Double) -> Void) -> Observable<UploadElement<O>> where Element == UploadElement<O> {
return self.do(onNext: { element in
if case .progress(let progress) = element {
progressCompletion(progress)
}
})
}
func response<O: Codable>(responseCompletion: #escaping (O) -> Void) -> Observable<UploadElement<O>> where Element == UploadElement<O> {
return self.do(onNext: { element in
if case .response(let response) = element {
responseCompletion(response)
}
})
}
}
Now I can use the "planned" api:
service.update(data: /* ... */)
.progress { progress in /* */ }
.response { result in /* */ }
.subscribe(
onError: { error in /* */ }
)
.dispose(by: disposeBag)
However as Daniel mentioned this might not be the "nicer way of being notified".

Asynchronous iteration using Swift Combine

I am trying to do multiple async operations, in sequence, on an array of data. However I am having problems with the return values of map.
Here is the test code:
import Combine
func getLength(_ string: String) -> Future<Int,Error> {
return Future<Int,Error>{ promise in
print("Length \(string.count)")
promise(.success(string.count))
}
}
func isEven(_ int: Int) -> Future<Bool,Error> {
return Future<Bool,Error>{ promise in
print("Even \(int % 2 == 0)")
promise(.success(int % 2 == 0))
}
}
let stringList = ["a","bbb","c","dddd"]
func testStrings(_ strings:ArraySlice<String>) -> Future<Void,Error> {
var remaining = strings
if let first = remaining.popFirst() {
return getLength(first).map{ length in
return isEven(length)
}.map{ even in
return testStrings(remaining)
}
} else {
return Future { promise in
promise(.success(()))
}
}
}
var storage = Set<AnyCancellable>()
testStrings(ArraySlice<String>(stringList)).sink { _ in } receiveValue: { _ in print("Done") }.store(in: &storage)
This generates the following error:
error: MyPlayground.playground:26:11: error: cannot convert return expression of type 'Publishers.Map<Future<Int, Error>, Future<Void, Error>>' to return type 'Future<Void, Error>'
}.map{ even in
I thought we could use map to convert from one publisher type to the other, but it seems it's wrapped inside a Publishers.Map. How do I get rid of this?
Thanks!
Well it seems that this works:
import Combine
func getLength(_ string: String) -> Future<Int,Error> {
return Future<Int,Error>{ promise in
print("Length \(string.count)")
promise(.success(string.count))
}
}
func isEven(_ int: Int) -> Future<Bool,Error> {
return Future<Bool,Error>{ promise in
print("Even \(int % 2 == 0)")
promise(.success(int % 2 == 0))
}
}
let stringList = ["a","bbb","c","dddd"]
func testStrings(_ strings:ArraySlice<String>) -> AnyPublisher<Void,Error> {
var remaining = strings
if let first = remaining.popFirst() {
return getLength(first).flatMap{ length in
return isEven(length)
}.flatMap{ even in
return testStrings(remaining)
}.eraseToAnyPublisher()
} else {
return Future<Void,Error> { promise in
promise(.success(()))
}.eraseToAnyPublisher()
}
}
var storage = Set<AnyCancellable>()
testStrings(ArraySlice<String>(stringList)).sink { _ in } receiveValue: { _ in print("Done") }.store(in: &storage)

Transform Mono<Void> in Mono<String> adding a value

My program services offer some delete methods that return Mono<Void>, e.g.: fun delete(clientId: String) : Mono<Void>
After calling .delete("x") I would like to propagate the clientId downstream to do other operations:
userService.get(id).map{ user ->
userService.delete(user.id) //This returns Mono<Void>
.map {
user.id //Never called!!!
}
.map { userId ->
//other calls using the propagated userId
}
}
The problem is since delete returns a Mono<Void>, the following .map {
user.id } is never called. So how can I transform the Mono<Void> into a Mono<String> to propagate the userId?
You can use thenReturn operator:
userService.get(id)
.flatMap { user -> userService.delete(user.id).thenReturn(user.id) }
.flatMap { id -> //other calls using the propagated userId }
I managed to work around it using hasNext that transforms it into a Boolean:
#Test
fun `should`() {
val mono: Mono<String> = "1".toMono()
.flatMap { id ->
val map = Mono.empty<Void>()
.hasElement()
.map {
id + "a"
}.map {
(it + "1")
}
map
}
mono.doOnNext {
println(mono)
}.subscribe()
}

PromiseKit wrapping external closure in Promises

I am using an external library in Swift so I cannot control the return statements. My understanding is that I should wrap these returns in promises in order to use PromiseKit. Is this correct?
Assuming so, I have working code as follows:
private func getChannelImage(for channel: TCHChannel, completion: #escaping (UIImage?, CAProfileError?) -> Void) {
if let members = channel.members {
members.members(completion: { (result, paginator) in
if result.isSuccessful() {
// ... do something
}
else {
completion(nil, CAProfileError.UnknownError)
}
})
}
}
This can be difficult to read. I am trying to simplify this using PromiseKit. First, I want to simplify members.members(completion: { (result, paginator) in to a promise that I can call with the firstly { ... } syntax. I thus try and do as follows:
private func asPromise(members: TCHMembers) -> Promise<TCHMemberPaginator> {
return Promise<TCHMemberPaginator> { fulfill, reject in
members.members(completion: { (result, paginator) in
if result.isSuccesful() {
fulfill(paginator)
} else {
reject()
}
})
}
}
But this approach does not work and I get "Unable to infer closure type in the current context". I'm trying to find a good example of this use case done online but am having trouble. Any thoughts on how to properly return promises?
Assuming the TCHMemberPaginator and TCHMembers as below,
class TCHMemberPaginator {}
class TCHMembers {
func members(completion: (Bool, TCHMemberPaginator?) -> Void) {}
}
Here is the method to return a Promise,
private func asPromise(members: TCHMembers) -> Promise<TCHMemberPaginator> {
return Promise { seal in
members.members(completion: { (result, paginator) in
if result == true, let p = paginator {
seal.fulfill(p)
} else {
seal.reject(NSError())
}
})
}
}

SQLite transaction and RxJava

There is how I save my data with RxJava:
override fun put(note: Note): Observable<Note> {
validateNote(note)
return Observable.just(note)
.doOnNext { dbKeeper.startTransaction() }
.doOnNext { storeHashtags(note) }
.doOnNext { storeImages(note) }
.flatMap { notesDataStore.put(notesMapper.transform(note)) }
.map { notesMapper.transform(it) }
.doOnNext { dbKeeper.setTransactionSuccessful() }
.doOnUnsubscribe { dbKeeper.endTransaction() }
}
And then I use this method like this:
notesManager.put(note)
.switchMap { notesManager.getHashtags() }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe {view.setHashtags(it) }
And doOnUnsubscribe never called as getHashtags() trying to SELECT from db that locked by startTransaction(). Deadlock, heh.
Okay. Let's replace doOnUnsubscribe(...) with doOnTerminate(...).
override fun put(note: Note): Observable<Note> {
validateNote(note)
return Observable.just(note)
.doOnNext { dbKeeper.startTransaction() }
.doOnNext { storeHashtags(note) }
.doOnNext { storeImages(note) }
.map { notesMapper.transform(note) }
.flatMap { notesDataStore.put(it) }
.map { notesMapper.transform(it) }
.doOnNext { dbKeeper.setTransactionSuccessful() }
.doOnTerminate { dbKeeper.endTransaction() }
}
But now transaction won't close if Observable will be interrupted by subscriber.unsubscribe().
What can you recommend to solve my situation?
Additional information:
I use one writableDb instance to write/read data.
I'd say this isn't a good fit for Rx; My recommendation would be to do the transaction using the usual try-finally (pardon the Java):
Observable<Note> put(Note note) {
return just(note)
.doOnNext(n -> {
validateNote(n);
dbKeeper.startTransaction();
try {
storeHashtags(n);
storeImages(n);
notesDataStore.put(notesMapper.transform(n));
dbKeeper.setTransactionSuccessful();
} finally {
dbKeeper.endTransaction();
}
});
}
Edit: Maybe this is more useful:
fun <T> withTransaction(sourceObservable: Observable<T>): Observable<T> {
val latch = AtomicInteger(1)
val maybeEndTransaction = Action0 {
if (latch.decrementAndGet() == 0) {
endTransaction()
}
}
return Observable.empty<T>()
.doOnCompleted { startTransaction() }
.mergeWith(sourceObservable)
.doOnNext { setTransactionSuccessful() }
.doOnTerminate(maybeEndTransaction)
.doOnUnsubscribe(maybeEndTransaction)
}
Use it like this:
override fun put(note: Note): Observable<Note> {
return Observable.just(note)
.doOnNext { validateNote(note) }
.doOnNext { storeHashtags(note) }
.doOnNext { storeImages(note) }
.flatMap { notesDataStore.put(notesMapper.transform(note)) }
.map { notesMapper.transform(it) }
.compose { withTransaction }
}
It will ensure that the transaction ends exactly once; just be careful that you don't switch threads within your original observable chain (unless your transaction amanger can handle that, or you modified your schedulers to associate new threads with existing transactions).