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
true
if it succeeded, or returning immediatelyfalse
in 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 returningNone
in 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)
, orNone
if 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
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
- def emptyCancelable: Cancelable
- Attributes
- protected
- Definition Classes
- AsyncVar → GenericVar
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def iEmpty(): Boolean
Returns
true
if the var is empty,false
otherwise.Returns
true
if the var is empty,false
otherwise.- Annotations
- @UnsafeProtocol() @UnsafeBecauseImpure()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def makeCancelable(f: (Id) => Unit, id: Id): Cancelable
- Attributes
- protected
- Definition Classes
- AsyncVar → GenericVar
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def put(a: A): CancelableFuture[Unit]
Fills the
AsyncVar
if it is empty, or blocks (asynchronously) if theAsyncVar
is full, until the given value is next in line to be consumed on take.Fills the
AsyncVar
if it is empty, or blocks (asynchronously) if theAsyncVar
is 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
put
operation 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
AsyncVar
if it is empty, or blocks (asynchronously) if theAsyncVar
is full, until the given value is next in line to be consumed on take.Fills the
AsyncVar
if it is empty, or blocks (asynchronously) if theAsyncVar
is 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.
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- 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 toString(): String
- Definition Classes
- AnyRef → Any
- def tryPut(a: A): Boolean
Tries to put a value in the underlying var, returning
true
if the operation succeeded and thus the var was empty, orfalse
if the var was full and thus the operation failed.Tries to put a value in the underlying var, returning
true
if the operation succeeded and thus the var was empty, orfalse
if 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, orNone
if 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, orNone
if 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
- final def unsafeIsEmpty(): Boolean
- Attributes
- protected[monix]
- Definition Classes
- GenericVar
- final def unsafePut(a: A, await: (Either[Nothing, Unit]) => Unit): Cancelable
- Attributes
- protected[monix]
- Definition Classes
- GenericVar
- final def unsafeRead(await: (Either[Nothing, A]) => Unit): Cancelable
- Attributes
- protected[monix]
- Definition Classes
- GenericVar
- final def unsafeTake(await: (Either[Nothing, A]) => Unit): Cancelable
- Attributes
- protected[monix]
- Definition Classes
- GenericVar
- final def unsafeTryPut(a: A): Boolean
- Attributes
- protected[monix]
- Definition Classes
- GenericVar
- final def unsafeTryRead(): Option[A]
- Attributes
- protected[monix]
- Definition Classes
- GenericVar
- final def unsafeTryTake(): Option[A]
- Attributes
- protected[monix]
- Definition Classes
- GenericVar
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
This is the API documentation for the Monix library.
Package Overview
monix.execution exposes lower level primitives for dealing with asynchronous execution:
Atomic
types, as alternative tojava.util.concurrent.atomic
monix.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
Observable
pattern:Observable
implementationsmonix.tail exposes Iterant for purely functional pull based streaming:
Batch
andBatchCursor
, the alternatives to Scala'sIterable
andIterator
respectively 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.