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.
- Source
- CancelablePromise.scala
- See also
- Alphabetic
- By Inheritance
- CancelablePromise
- Promise
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Abstract Value Members
- 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
- abstract def isCompleted: Boolean
- Definition Classes
- Promise
- 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)
- abstract def tryComplete(result: Try[A]): Boolean
- Definition Classes
- Promise
Concrete 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 complete(result: Try[A]): CancelablePromise.this.type
- Definition Classes
- Promise
- def completeWith(other: Future[A]): CancelablePromise.this.type
- Definition Classes
- Promise
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def failure(cause: Throwable): CancelablePromise.this.type
- Definition Classes
- Promise
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- 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 success(value: A): CancelablePromise.this.type
- Definition Classes
- Promise
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- def tryFailure(cause: Throwable): Boolean
- Definition Classes
- Promise
- def trySuccess(value: A): Boolean
- Definition Classes
- Promise
- 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])
Deprecated Value Members
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable]) @Deprecated
- Deprecated
- final def tryCompleteWith(other: Future[A]): CancelablePromise.this.type
- Definition Classes
- Promise
- Annotations
- @deprecated
- Deprecated
(Since version 2.13.0) Since this method is semantically equivalent to
completeWith
, use that instead.
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.