Class/Object

monix.execution.cancelables

ChainedCancelable

Related Docs: object ChainedCancelable | package cancelables

Permalink

final class ChainedCancelable extends AssignableCancelable

Represents a monix.execution.Cancelable whose underlying cancelable reference can be swapped for another. It can be "chained" to another ChainedCancelable, forwarding all operations to it.

For most purposes it works like a MultiAssignmentCancelable:

val s = ChainedCancelable()
s := c1 // sets the underlying cancelable to c1
s := c2 // swaps the underlying cancelable to c2

s.cancel() // also cancels c2

s := c3 // also cancels c3, because s is already canceled

However it can also be linked to another ChainedCancelable reference, forwarding all requests to it:

val source = ChainedCancelable()
val child1 = ChainedCancelable()
val child2 = ChainedCancelable()

// Hence forth forwards all operations on `child1` to `source`
child1.chainTo(source)

// Also forwarding all `child2` operations to `source`.
// This happens because `child1` was linked to `source` first
// but order matters, as `child2` will be linked directly
// to `source` and not to `child1`, in order for `child1` to
// be garbage collected if it goes out of scope ;-)
child2.chainTo(child1)

// Source will be updated with a new Cancelable ref
child1 := Cancelable(() => println("Cancelling (1)"))

// Source will be updated with another Cancelable ref
child2 := Cancelable(() => println("Cancelling (2)"))

source.cancel()
//=> Cancelling (2)

This implementation is a special purpose AssignableCancelable, much like StackedCancelable, to be used in flatMap implementations that need it.

The problem that it solves in Monix's codebase is that various flatMap implementations need to be memory safe. By "chaining" cancelable references, we allow the garbage collector to get rid of references created in a flatMap loop, the goal being to consume a constant amount of memory. Thus this implementation is used for CancelableFuture.

The implementation is also relaxed about the thread-safety of the forwardTo operation, treating it like a semi-final state and using Java 8 getAndSet platform intrinsics for performance reasons.

If unsure about what to use, then you probably don't need ChainedCancelable. Use MultiAssignmentCancelable or SingleAssignmentCancelable for most purposes.

Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. ChainedCancelable
  2. AssignableCancelable
  3. Cancelable
  4. Serializable
  5. Serializable
  6. AnyRef
  7. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. def :=(value: Cancelable): ChainedCancelable.this.type

    Permalink

    Updates the internal reference of this assignable cancelable to the given value.

    Updates the internal reference of this assignable cancelable to the given value.

    If this cancelable is already canceled, then value is going to be canceled on assignment as well.

    returns

    this

    Definition Classes
    ChainedCancelableAssignableCancelable
  4. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  5. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  6. def cancel(): Unit

    Permalink

    Cancels the unit of work represented by this reference.

    Cancels the unit of work represented by this reference.

    Guaranteed idempotency - calling it multiple times should have the same side-effect as calling it only once. Implementations of this method should also be thread-safe.

    Definition Classes
    ChainedCancelableCancelable
  7. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  8. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  9. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  10. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  11. def forwardTo(other: ChainedCancelable): Unit

    Permalink

    Chains this ChainedCancelable to another reference, such that all operations are forwarded to other.

    Chains this ChainedCancelable to another reference, such that all operations are forwarded to other.

    val source = ChainedCancelable()
    val child1 = ChainedCancelable()
    val child2 = ChainedCancelable()
    
    // Hence forth forwards all operations on `child1` to `source`
    child1.chainTo(source)
    
    // Also forwarding all `child2` operations to `source`
    // (this happens because `child1` was linked to `source` first
    // but order matters ;-))
    child2.chainTo(child1)
    
    // Source will be updated with a new Cancelable ref
    child1 := Cancelable(() => println("Cancelling (1)"))
    
    // Source will be updated with another Cancelable ref
    child2 := Cancelable(() => println("Cancelling (2)"))
    
    source.cancel()
    //=> Cancelling (2)
  12. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  13. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  14. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  15. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  16. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  17. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  18. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  19. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  20. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  22. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AssignableCancelable

Inherited from Cancelable

Inherited from Serializable

Inherited from Serializable

Inherited from AnyRef

Inherited from Any

Ungrouped