Zack
Zack

Reputation: 551

How can I connect a Producer to an Actor?

How can I connect a producer, created with the produce coroutine builder, to an actor, created with the actor coroutine builder?

Basically, I'd like the send channel of the producer to be the receive channel of the actor. Is there a simple way to do that?

I couldn't find anything in the documentation allowing me to explicitly specify the channel to be used by the producer or actor.

Upvotes: 4

Views: 326

Answers (1)

Roman  Elizarov
Roman Elizarov

Reputation: 28648

Assuming the following definitions:

val producer = produce<T> { ... }
val actor = actor<T> { ... }

You can write the following code to launch a helper coroutine that sends all produced messages to your actor:

launch { producer.toChannel(actor) }

If you want to wait until this copying job is done, you can simply use producer.toChannel(actor) for your coroutine. toChannel is a terminal operation that waits until the processing is complete.

Upvotes: 2

Related Questions