Async

gears.async.Async
See theAsync companion trait
opaque object Async

Attributes

Companion
trait
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
Async.type

Members list

Type members

Classlikes

abstract class OriginalSource[+T] extends Source[T]

An original source has a standard definition of onComplete in terms of poll and addListener.

An original source has a standard definition of onComplete in terms of poll and addListener.

Implementations should be the resource owner to handle listener queue and completion using an object monitor on the instance.

Attributes

Supertypes
trait Source[T]
class Object
trait Matchable
class Any
Known subtypes
trait Future[T]
trait Promise[T]
object Source

Attributes

Companion
trait
Supertypes
class Object
trait Matchable
class Any
Self type
Source.type
trait Source[+T]

An asynchronous data source. Sources can be persistent or ephemeral. A persistent source will always pass same data to calls of Source!.poll and Source!.onComplete. An ephemeral source can pass new data in every call.

An asynchronous data source. Sources can be persistent or ephemeral. A persistent source will always pass same data to calls of Source!.poll and Source!.onComplete. An ephemeral source can pass new data in every call.

Attributes

See also

An example of a persistent source is gears.async.Future.

An example of an ephemeral source is gears.async.Channel.

Companion
object
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class OriginalSource[T]
trait Future[T]
trait Promise[T]

Types

opaque type SelectCase[T]

Cases for handling async sources in a select. SelectCase can be constructed by extension methods handle of Source.

Cases for handling async sources in a select. SelectCase can be constructed by extension methods handle of Source.

Attributes

See also

Source.handle (and its operator alias ~~>)

Async.select where SelectCase is used.

opaque type Spawn

Async.Spawn is a special subtype of Async, also capable of spawning runnable Futures.

Async.Spawn is a special subtype of Async, also capable of spawning runnable Futures.

Most functions should not take Spawn as a parameter, unless the function explicitly wants to spawn "dangling" runnable Futures. Instead, functions should take Async and spawn scoped futures within Async.group.

Attributes

Value members

Concrete methods

def blocking[T](body: Spawn ?=> T)(using support: AsyncSupport, scheduler: support.Scheduler): T

Execute asynchronous computation body on currently running thread. The thread will suspend when the computation waits.

Execute asynchronous computation body on currently running thread. The thread will suspend when the computation waits.

Attributes

inline def current(using async: Async): Async

Returns the currently executing Async context. Equivalent to summon[Async].

Returns the currently executing Async context. Equivalent to summon[Async].

Attributes

def either[T1, T2](src1: Source[T1], src2: Source[T2]): Source[Either[T1, T2]]

Race two sources, wrapping them respectively in Left and Right cases.

Race two sources, wrapping them respectively in Left and Right cases.

Attributes

Returns

a new Source that resolves with Left if src1 returns an item, Right if src2 returns an item, whichever comes first.

See also

race and select for racing more than two sources.

def group[T](body: Spawn ?=> T)(using Async): T

Runs body inside a spawnable context where it is allowed to spawn concurrently runnable Futures. When the body returns, all spawned futures are cancelled and waited for.

Runs body inside a spawnable context where it is allowed to spawn concurrently runnable Futures. When the body returns, all spawned futures are cancelled and waited for.

Attributes

def race[T](sources: Source[T]*): Source[T]

Creates a source that "races" a list of sources.

Creates a source that "races" a list of sources.

Listeners attached to this source is resolved with the first item arriving from one of the sources. If multiple sources are available at the same time, one of the items will be returned with no priority. Items that are not returned are '''not''' consumed from the upstream sources.

Attributes

See also

raceWithOrigin for a race source that also returns the upstream origin of the item.

Async.select for a convenient syntax to race sources and awaiting them with Async.

def raceWithOrigin[T](sources: Source[T]*): Source[(T, Source[T])]

Like race, but the returned value includes a reference to the upstream source that the item came from.

Like race, but the returned value includes a reference to the upstream source that the item came from.

Attributes

See also

Async.select for a convenient syntax to race sources and awaiting them with Async.

def select[T](cases: SelectCase[T]*)(using Async): T

Race a list of sources with the corresponding handler functions, once an item has come back. Like race, select guarantees exactly one of the sources are polled. Unlike transformValuesWith, the handler in select is run in the same async context as the calling context of select.

Race a list of sources with the corresponding handler functions, once an item has come back. Like race, select guarantees exactly one of the sources are polled. Unlike transformValuesWith, the handler in select is run in the same async context as the calling context of select.

Attributes

See also

Source.handle (and its operator alias ~~>) for methods to create SelectCases.

Example
// Race a channel read with a timeout
val ch = SyncChannel[Int]()
// ...
val timeout = Future(sleep(1500.millis))
Async.select(
 ch.readSrc.handle: item =>
   Some(item * 2),
 timeout ~~> _ => None
)

Extensions

Extensions

extension [T](src: Source[Try[T]])
inline def await(using Async): T

Waits for an item to arrive from the source, then automatically unwraps it. Suspends until an item returns.

Waits for an item to arrive from the source, then automatically unwraps it. Suspends until an item returns.

Attributes

See also

awaitResult for non-unwrapping await.

extension [E, T](src: Source[Either[E, T]])
inline def await(using Async): T

Waits for an item to arrive from the source, then automatically unwraps it. Suspends until an item returns.

Waits for an item to arrive from the source, then automatically unwraps it. Suspends until an item returns.

Attributes

See also

awaitResult for non-unwrapping await.

extension [T](src: Source[T])
def transformValuesWith[U](f: T => U): Source[U]

Create a new source that requires the original source to run the given transformation function on every value received.

Create a new source that requires the original source to run the given transformation function on every value received.

Note that f is always run on the computation that produces the values from the original source, so this is very likely to run sequentially and be a performance bottleneck.

Value parameters

f

the transformation function to be run on every value. f is run before the item is passed to the Listener.

Attributes

extension [T](src: Source[T])
inline def handle[U](f: T => U): SelectCase[U]

Attach a handler to src, creating a SelectCase.

Attach a handler to src, creating a SelectCase.

Attributes

See also

Async.select where SelectCase is used.

inline def ~~>[U](f: T => U): SelectCase[U]

Alias for handle

Alias for handle

Attributes

See also

Async.select where SelectCase is used.