Efficient way to update progress of long task while processing a task with Mono&Flux

Viewed 85

I tried to make a method for long task which sends events of progress.

The method implemented by using Flux and Mono.

I figured out two ways to do it.

But I can't decide which approach is more efficient and elegant, or the other one.

Way 1.

var progressEvents = Sinks.Many<Progress> = Sinks.many().multicast()

fun doLongTask(taskFile : Mono<File>) : Mono<Another> {
   return taskFile.map{ file ->
      progressEvents.tryEmit(Progress("1"))
      doSomething(file)    
   }.map{ something ->
      progressEvents.tryEmit(Progress("2"))
      doAnother(something)
   }
}

Way2.

var progressEvents = Sinks.Many<Progress> = Sinks.many().multicast()

fun doLongTask(taskFile : Mono<File>) : Mono<Another> {
   var doSomeMap = taskFile.map{ file ->
      doSomething(file)    
   }.share()
   doSomeMap.map{ something -> Progress("1")}.subscribe{progressEvents::tryEmit}
   var doAnotherMap = doSomeMap.map{ something ->
      doAnother(something)
   }.share()
   doAnotherMap.map{ another -> Progress("2")}.subscribe{progressEvents::tryEmit}
   return doAnotherMap
}
0 Answers
Related