Channel

gears.async.Channel
See theChannel companion object
trait Channel[T] extends SendableChannel[T], ReadableChannel[T], Closeable

A generic channel that can be sent to, received from and closed.

Attributes

See also

SyncChannel, BufferedChannel and UnboundedChannel for channel implementations.

Example
// send from one Future, read from multiple
val ch = SyncChannel[Int]()
val sender = Future:
 for i <- 1 to 20 do
   ch.send(i)
   ch.close()
val receivers = (1 to 5).map: n =>
 Future:
   boundary:
     while true:
       ch.read() match
         case Right(k) => println(s"Receiver $n got: $k")
         case Left(_) => boundary.break()
receivers.awaitAll
Companion
object
Graph
Supertypes
trait Closeable
trait AutoCloseable
trait ReadableChannel[T]
trait SendableChannel[T]
class Object
trait Matchable
class Any
Show all
Known subtypes
trait BufferedChannel[T]
trait UnboundedChannel[T]
trait SyncChannel[T]

Members list

Value members

Concrete methods

final inline def asCloseable: Closeable

Restrict this channel to close-only.

Restrict this channel to close-only.

Attributes

final inline def asReadable: ReadableChannel[T]

Restrict this channel to read-only.

Restrict this channel to read-only.

Attributes

final inline def asSendable: SendableChannel[T]

Restrict this channel to send-only.

Restrict this channel to send-only.

Attributes

Inherited methods

def read()(using Async): Res[T]

Read an item from the channel, suspending until the item has been received. Returns Failure(ChannelClosedException) if the channel was closed.

Read an item from the channel, suspending until the item has been received. Returns Failure(ChannelClosedException) if the channel was closed.

Attributes

Inherited from:
ReadableChannel
def send(x: T)(using Async): Unit

Send x over the channel, suspending until the item has been sent or, if the channel is buffered, queued.

Send x over the channel, suspending until the item has been sent or, if the channel is buffered, queued.

Attributes

Throws
ChannelClosedException

if the channel was closed.

Inherited from:
SendableChannel

Inherited and Abstract methods

def close(): Unit

Attributes

Inherited from:
Closeable
def sendSource(x: T): Source[Res[Unit]]

Create an Async.Source representing the send action of value x.

Create an Async.Source representing the send action of value x.

Note that each listener attached to and accepting an Unit value corresponds to x being sent once.

To create an Async.Source that sends the item exactly once regardless of listeners attached, wrap the send operation inside a gears.async.Future:

val sendOnce = Future(ch.send(x))

Attributes

Returns

an Async.Source that resolves with Right(()) when x is sent to the channel, or Left(Closed) if the channel is already closed. This source will perform a send operation every time a listener is attached to it, or every time it is Async$.awaited on.

Inherited from:
SendableChannel

Inherited and Abstract fields

val readSource: Source[Res[T]]

An Async.Source corresponding to items being sent over the channel. Note that each listener attached to and accepting a Right value corresponds to one value received over the channel.

An Async.Source corresponding to items being sent over the channel. Note that each listener attached to and accepting a Right value corresponds to one value received over the channel.

To create an Async.Source that reads exactly one item regardless of listeners attached, wrap the read operation inside a gears.async.Future.

val readOnce = Future(ch.read(x))

Attributes

Inherited from:
ReadableChannel