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.
this
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.
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)
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 OrderedCancelable:
However it can also be linked to another
ChainedCancelable
reference, forwarding all requests to it: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 aflatMap
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 OrderedCancelable or SingleAssignCancelable for most purposes.