tseisel
03/08/2019, 4:16 PMclass Actor<E>(scope: CoroutineScope) {
private val processor = scope.actor<E> {
consumeEach(::println)
println("Finished processing")
}
suspend fun process(element: E) {
processor.send(element)
}
}
In the following test, the test function never returns due to the actor coroutine still being active :
fun test() = runBlocking<Unit> {
val actor = Actor<String>(this)
actor.process("Foo")
actor.process("Bar")
}
I'd like some utility function that cancels the scope the actor runs in, but only after all messages in the actor has been processed.
Is it possible to do that with coroutineScope or launch or do I have to write my own utility function ?