Skip to content

Commit

Permalink
collectors
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperancinha committed Feb 2, 2024
1 parent 2a1a371 commit cdf30a1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ object Main {
org.jesperancinha.ktd.java.ValidationUsersEmail.main(emptyArray())
CancellingCounterCoroutines.main()
RunningFold.main()
TicketCollectors.main()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.jesperancinha.ktd

import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.runBlocking
import kotlin.random.Random

class TicketCollectors {
companion object {
@JvmStatic
fun main(args: Array<String> = emptyArray()) = runBlocking {
flow {
repeat(10) {
emit(Random.nextInt(10, 25))
delay(2)
}
}.collect { value ->
processTemperature(value)
}
println("Finished processing collect!")

flow {
repeat(10) {
emit(Random.nextInt(10, 25))
delay(2)
}
}.collectLatest { value ->
processTemperature(value)
}
println("Finished processing collectLatest!")

}

private suspend fun processTemperature(value: Int) {
println("Processing-> $value")
delay(Random.nextLong(1, 3))
println("Processed!-> $value")

}
}
}

0 comments on commit cdf30a1

Please sign in to comment.