final class AsyncVar[A] extends GenericVar[A, Cancelable]
Asynchronous mutable location, that is either empty or contains
a value of type A.
It has these fundamental atomic operations:
- put which fills the var if empty, or waits (asynchronously) otherwise until the var is empty again (with the putByCallback overload)
- tryPut which fills the var if empty, returning
trueif it succeeded, or returning immediatelyfalsein case the var was full and thus the operation failed - take which empties the var if full, returning the contained value, or waits (asynchronously) otherwise until there is a value to pull (with the takeByCallback overload)
- tryTake which empties the var if full, returning the
contained value immediately as
Some(a), or otherwise returningNonein case the var was empty and thus the operation failed - read which reads the var if full, but without taking it from the interval var, or waits (asynchronously) until there is a value to read
- tryRead tries reading the var without modifying it in
any way; if full then returns
Some(a), orNoneif empty
The AsyncVar is appropriate for building synchronization
primitives and performing simple inter-thread communications.
If it helps, it's similar with a BlockingQueue(capacity = 1),
except that it doesn't block any threads, all waiting being
callback-based.
Given its asynchronous, non-blocking nature, it can be used on top of Javascript as well.
This is inspired by Control.Concurrent.MVar from Haskell, except that the implementation is made to work with plain Scala futures (and is thus impure).
- Source
- AsyncVar.scala
- Alphabetic
- By Inheritance
- AsyncVar
- GenericVar
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Value Members
- def iEmpty(): Boolean
Returns
trueif the var is empty,falseotherwise.Returns
trueif the var is empty,falseotherwise.- Annotations
- @UnsafeProtocol() @UnsafeBecauseImpure()
- def put(a: A): CancelableFuture[Unit]
Fills the
AsyncVarif it is empty, or blocks (asynchronously) if theAsyncVaris full, until the given value is next in line to be consumed on take.Fills the
AsyncVarif it is empty, or blocks (asynchronously) if theAsyncVaris full, until the given value is next in line to be consumed on take.This operation is atomic.
- returns
a future that will complete when the
putoperation succeeds in filling theAsyncVar, with the given value being next in line to be consumed; note that this is a cancelable future that can be canceled to avoid memory leaks in race conditions
- Annotations
- @UnsafeBecauseImpure()
- See also
putByCallback for the raw, unsafe version that can work with plain callbacks.
- def putByCallback(a: A, await: Callback[Nothing, Unit]): Cancelable
Fills the
AsyncVarif it is empty, or blocks (asynchronously) if theAsyncVaris full, until the given value is next in line to be consumed on take.Fills the
AsyncVarif it is empty, or blocks (asynchronously) if theAsyncVaris full, until the given value is next in line to be consumed on take.This operation is atomic.
- a
is the value to store
- await
is a callback that will be called when the operation succeeded with a result
- returns
a cancelable token that can be used to cancel the computation to avoid memory leaks in race conditions
- Annotations
- @UnsafeProtocol() @UnsafeBecauseImpure()
- See also
put for the safe future-enabled version.
- def read(): CancelableFuture[A]
Tries reading the current value, or waits (asynchronously) until there is a value available.
Tries reading the current value, or waits (asynchronously) until there is a value available.
This operation is atomic.
- returns
a future that might already be completed in case the result is available immediately
- Annotations
- @UnsafeBecauseImpure()
- See also
readByCallback for the raw, unsafe version that can work with plain callbacks.
- def readByCallback(await: Callback[Nothing, A]): Cancelable
Tries reading the current value, or waits (asynchronously) until there is a value available.
Tries reading the current value, or waits (asynchronously) until there is a value available.
This operation is atomic.
- await
is a callback that will be called when the operation succeeded with a result
- returns
a cancelable token that can be used to cancel the computation to avoid memory leaks in race conditions
- Annotations
- @UnsafeProtocol() @UnsafeBecauseImpure()
- See also
read for the safe future-enabled version.
- def take(): CancelableFuture[A]
Empties the var if full, returning the contained value, or blocks (asynchronously) until a value is available.
Empties the var if full, returning the contained value, or blocks (asynchronously) until a value is available.
This operation is atomic.
- Annotations
- @UnsafeBecauseImpure()
- See also
takeByCallback for the raw, unsafe version that can work with plain callbacks.
- def takeByCallback(await: Callback[Nothing, A]): Cancelable
Empties the var if full, returning the contained value, or blocks (asynchronously) until a value is available.
Empties the var if full, returning the contained value, or blocks (asynchronously) until a value is available.
This operation is atomic.
- await
is a callback that will be called when the operation succeeded with a result
- returns
a cancelable token that can be used to cancel the computation to avoid memory leaks in race conditions
- Annotations
- @UnsafeProtocol() @UnsafeBecauseImpure()
- See also
take for the safe future-enabled version.
- def tryPut(a: A): Boolean
Tries to put a value in the underlying var, returning
trueif the operation succeeded and thus the var was empty, orfalseif the var was full and thus the operation failed.Tries to put a value in the underlying var, returning
trueif the operation succeeded and thus the var was empty, orfalseif the var was full and thus the operation failed.- Annotations
- @UnsafeProtocol() @UnsafeBecauseImpure()
- See also
put for the version that can asynchronously wait for the var to become empty
- def tryRead(): Option[A]
Tries reading the current value, without modifying the var in any way:
Tries reading the current value, without modifying the var in any way:
- if full, returns
Some(a) - if empty, returns
None
- Annotations
- @UnsafeProtocol() @UnsafeBecauseImpure()
- if full, returns
- def tryTake(): Option[A]
Tries to take a value from the underlying var, returning
Some(a)if the operation succeeded and thus the var was full, orNoneif the var was empty and thus the operation failed.Tries to take a value from the underlying var, returning
Some(a)if the operation succeeded and thus the var was full, orNoneif the var was empty and thus the operation failed.- Annotations
- @UnsafeProtocol() @UnsafeBecauseImpure()
- See also
take for the version that can asynchronously wait for the var to become full

This is the API documentation for the Monix library.
Package Overview
monix.execution exposes lower level primitives for dealing with asynchronous execution:
Atomictypes, as alternative tojava.util.concurrent.atomicmonix.catnap exposes pure abstractions built on top of the Cats-Effect type classes:
monix.eval is for dealing with evaluation of results, thus exposing Task and Coeval.
monix.reactive exposes the
Observablepattern:Observableimplementationsmonix.tail exposes Iterant for purely functional pull based streaming:
BatchandBatchCursor, the alternatives to Scala'sIterableandIteratorrespectively that we are using within Iterant's encodingYou can control evaluation with type you choose - be it Task, Coeval, cats.effect.IO or your own as long as you provide correct cats-effect or cats typeclass instance.