Can't figure out how to stop processing Flux on first match.
This what I have right now:
findAll(): Flux<Object>
findStorageId(Relation r): Mono<Long> | Mono.empty()
isPassing(Relation r): boolean
findAll().flatMap(p -> {
return Flux.fromStream(p.getRelations().stream()).flatMap(r -> {
return isPassing(r) ? findStorageId(r) : Mono.empty();
});
})
.handle((Long storageId, SynchronousSink<Long> sink) -> {
if (storageId != null) {
sink.next(storageId);
sink.complete();
}
})
.next()
.switchIfEmpty(Mono.error(new RuntimeException("Can't find storageId.")));
I'm trying to understand how I can interrupt processing of flux when first storageId is found. Right now I see, that first flatMap continues to work after finding first match.