Packages

  • package root
    Definition Classes
    root
  • package monix
    Definition Classes
    root
  • package execution
    Definition Classes
    monix
  • package annotations
    Definition Classes
    execution
  • package atomic

    A small toolkit of classes that support compare-and-swap semantics for safe mutation of variables.

    A small toolkit of classes that support compare-and-swap semantics for safe mutation of variables.

    On top of the JVM, this means dealing with lock-free thread-safe programming. Also works on top of Javascript, with Scala.js, for API compatibility purposes and because it's a useful way to box a value.

    The backbone of Atomic references is this method:

    def compareAndSet(expect: T, update: T): Boolean

    This method atomically sets a variable to the update value if it currently holds the expect value, reporting true on success or false on failure. The classes in this package also contain methods to get and unconditionally set values.

    Building a reference is easy with the provided constructor, which will automatically return the most specific type needed (in the following sample, that's an AtomicDouble, inheriting from AtomicNumber[A]):

    val atomicNumber = Atomic(12.2)
    
    atomicNumber.incrementAndGet()
    // => 13.2

    These also provide useful helpers for atomically mutating of values (i.e. transform, transformAndGet, getAndTransform, etc...) or of numbers of any kind (incrementAndGet, getAndAdd, etc...).

    Definition Classes
    execution
  • package cancelables

    Cancelables represent asynchronous units of work or other things scheduled for execution and whose execution can be canceled.

    Cancelables represent asynchronous units of work or other things scheduled for execution and whose execution can be canceled.

    One use-case is the scheduling done by monix.execution.Scheduler, in which the scheduling methods return a Cancelable, allowing the canceling of the scheduling.

    Example:

    val s = ConcurrentScheduler()
    val task = s.scheduleRepeated(10.seconds, 50.seconds, {
      doSomething()
    })
    
    // later, cancels the scheduling ...
    task.cancel()
    Definition Classes
    execution
  • package exceptions
    Definition Classes
    execution
  • package internal
    Definition Classes
    execution
  • package misc
    Definition Classes
    execution
  • package rstreams

    Package exposing utilities for working with the Reactive Streams specification.

    Package exposing utilities for working with the Reactive Streams specification.

    Definition Classes
    execution
  • package schedulers
    Definition Classes
    execution
  • Ack
  • AsyncQueue
  • AsyncSemaphore
  • AsyncVar
  • BufferCapacity
  • Callback
  • Cancelable
  • CancelableFuture
  • CancelablePromise
  • ChannelType
  • ExecutionModel
  • FutureUtils
  • Scheduler
  • UncaughtExceptionReporter
  • compat

sealed abstract class CancelablePromise[A] extends Promise[A]

CancelablePromise is a scala.concurrent.Promise implementation that allows listeners to unsubscribe from receiving future results.

It does so by:

  • adding a low-level subscribe method, that allows for callbacks to be subscribed
  • returning CancelableFuture in its future method implementation, allowing created future objects to unsubscribe (being the high-level subscribe that should be preferred for most usage)

Being able to unsubscribe listeners helps with avoiding memory leaks in case of listeners or futures that are being timed-out due to promises that take a long time to complete.

See also

subscribe

future

Linear Supertypes
Promise[A], AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. CancelablePromise
  2. Promise
  3. AnyRef
  4. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Abstract Value Members

  1. abstract def future: CancelableFuture[A]

    Returns a future that can unsubscribe from this promise's notifications via cancelation.

    Returns a future that can unsubscribe from this promise's notifications via cancelation.

    val promise = CancelablePromise[Int]()
    
    val future1 = promise.future
    val future2 = promise.future
    
    for (r <- future1) println(s"Future1 completed with: $$r")
    for (r <- future2) println(s"Future2 completed with: $$r")
    
    // Unsubscribing from the future notification, but only for future1
    future1.cancel()
    
    // Completing our promise
    promise.success(99)
    
    //=> Future2 completed with: 99

    Note that in the above example future1 becomes non-terminating after cancellation. By unsubscribing its listener, it will never complete.

    This helps with avoiding memory leaks for futures that are being timed-out due to promises that take a long time to complete.

    Definition Classes
    CancelablePromise → Promise
  2. abstract def isCompleted: Boolean
    Definition Classes
    Promise
  3. abstract def subscribe(cb: (Try[A]) ⇒ Unit): Cancelable

    Low-level subscription method that registers a callback to be called when this promise will complete.

    Low-level subscription method that registers a callback to be called when this promise will complete.

    val promise = CancelablePromise[Int]()
    
    def subscribe(n: Int): Cancelable =
      promise.subscribe {
        case Success(str) =>
          println(s"Callback ($$n) completed with: $$str")
        case Failure(e) =>
          println(s"Callback ($$n) completed with: $$e")
      }
    
    val token1 = subscribe(1)
    val token2 = subscribe(2)
    
    // Unsubscribing from the future notification
    token1.cancel()
    
    // Completing our promise
    promise.success(99)
    
    //=> Callback (2) completed with: 99

    UNSAFE PROTOCOL: the implementation does not protect against stack-overflow exceptions. There's no point in doing it for such low level methods, because this is useful as middleware and different implementations will have different ways to deal with stack safety (e.g. monix.eval.Task).

    cb

    is a callback that will be called when the promise completes with a result, assuming that the returned cancelable token isn't canceled

    returns

    a cancelable token that can be used to unsubscribe the given callback, in order to prevent memory leaks, at which point the callback will never be called (if it wasn't called already)

  4. abstract def tryComplete(result: Try[A]): Boolean
    Definition Classes
    Promise

Concrete Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def clone(): AnyRef
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )
  6. def complete(result: Try[A]): CancelablePromise.this.type
    Definition Classes
    Promise
  7. final def completeWith(other: Future[A]): CancelablePromise.this.type
    Definition Classes
    Promise
  8. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  9. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  10. def failure(cause: Throwable): CancelablePromise.this.type
    Definition Classes
    Promise
  11. def finalize(): Unit
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  12. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  13. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  14. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  15. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  16. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  17. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  18. def success(value: A): CancelablePromise.this.type
    Definition Classes
    Promise
  19. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  20. def toString(): String
    Definition Classes
    AnyRef → Any
  21. final def tryCompleteWith(other: Future[A]): CancelablePromise.this.type
    Definition Classes
    Promise
  22. def tryFailure(cause: Throwable): Boolean
    Definition Classes
    Promise
  23. def trySuccess(value: A): Boolean
    Definition Classes
    Promise
  24. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  25. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  26. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )

Inherited from Promise[A]

Inherited from AnyRef

Inherited from Any

Ungrouped