package cancelables
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()
- Source
- package.scala
- Alphabetic
- By Inheritance
- cancelables
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Type Members
- trait AssignableCancelable extends Cancelable
Represents a class of cancelables that can hold an internal reference to another cancelable (and thus has to support the assignment operator).
Represents a class of cancelables that can hold an internal reference to another cancelable (and thus has to support the assignment operator).
Examples are the OrderedCancelable and the SingleAssignCancelable.
On assignment, if this cancelable is already canceled, then no assignment should happen and the update reference should be canceled as well.
- trait BooleanCancelable extends Cancelable
Represents a Cancelable that can be queried for the canceled status.
- final class ChainedCancelable extends AssignableCancelable
Represents a monix.execution.Cancelable whose underlying cancelable reference can be swapped for another.
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:
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 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.
- final class CompositeCancelable extends BooleanCancelable
Represents a composite of multiple cancelables.
Represents a composite of multiple cancelables. In case it is canceled, all contained cancelables will be canceled too, e.g...
val s = CompositeCancelable() s += c1 s += c2 s += c3 // c1, c2, c3 will also be canceled s.cancel()
Additionally, once canceled, on appending of new cancelable references, those references will automatically get canceled too:
val s = CompositeCancelable() s.cancel() // c1 gets canceled, because s is already canceled s += c1 // c2 gets canceled, because s is already canceled s += c2
Adding and removing references from this composite is thread-safe.
- final class MultiAssignCancelable extends Multi
Represents a Cancelable whose underlying cancelable reference can be swapped for another.
Represents a Cancelable whose underlying cancelable reference can be swapped for another.
Example:
val s = MultiAssignmentCancelable() 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
Also see:
- SerialCancelable, which is similar, except that it cancels the old cancelable upon assigning a new cancelable
- SingleAssignCancelable that is effectively a forward reference that can be assigned at most once
- OrderedCancelable that's very similar with
MultiAssignCancelable
, but with the capability of forcing ordering on concurrent updates
- final class OrderedCancelable extends Multi
Represents a Cancelable whose underlying cancelable reference can be swapped for another and that has the capability to force ordering of updates.
Represents a Cancelable whose underlying cancelable reference can be swapped for another and that has the capability to force ordering of updates.
For the most part it's very similar with MultiAssignCancelable:
val s = OrderedCancelable() 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 also has the capability of doing orderedUpdate:
val s = OrderedCancelable() ec.execute(new Runnable { def run() = s.orderedUpdate(ref2, 2) }) // The ordered updates are guarding against reversed ordering // due to the created thread being able to execute before the // following update s.orderedUpdate(ref1, 1)
Also see:
- SerialCancelable, which is similar, except that it cancels the old cancelable upon assigning a new cancelable
- SingleAssignCancelable that is effectively a forward reference that can be assigned at most once
- MultiAssignCancelable that's very similar with
OrderedCancelable
, but simpler, without the capability of doing ordered updates and possibly more efficient
- final class RefCountCancelable extends BooleanCancelable
Represents a
Cancelable
that only executes the canceling logic when all dependent cancelable objects have been canceled.Represents a
Cancelable
that only executes the canceling logic when all dependent cancelable objects have been canceled.The given callback gets called after our
RefCountCancelable
is canceled and after all dependent cancelables have been canceled along with the main cancelable.In other words, lets say for example that we have
acquired
2 children. In order for the cancelable to get canceled, we need to:- cancel both children
- cancel the main
RefCountCancelable
The implementation is thread-safe and cancellation order doesn't matter.
- final class SerialCancelable extends Multi
Represents a monix.execution.Cancelable whose underlying cancelable can be swapped for another cancelable which causes the previous underlying cancelable to be canceled.
Represents a monix.execution.Cancelable whose underlying cancelable can be swapped for another cancelable which causes the previous underlying cancelable to be canceled.
Example:
val s = SerialCancelable() s := c1 // sets the underlying cancelable to c1 s := c2 // cancels c1 and swaps the underlying cancelable to c2 s.cancel() // also cancels c2 s := c3 // also cancels c3, because s is already canceled
Also see OrderedCancelable, which is similar, but doesn't cancel the old cancelable upon assignment.
- final class SingleAssignCancelable extends Bool
Represents a monix.execution.Cancelable that can be assigned only once to another cancelable reference.
Represents a monix.execution.Cancelable that can be assigned only once to another cancelable reference.
Similar to monix.execution.cancelables.OrderedCancelable, except that in case of multi-assignment, it throws a
java.lang.IllegalStateException
.If the assignment happens after this cancelable has been canceled, then on assignment the reference will get canceled too.
Useful in case you need a forward reference.
- sealed abstract class StackedCancelable extends BooleanCancelable
Represents a composite of cancelables that are stacked, so you can push a new reference, or pop an existing one and when it gets canceled, then the whole stack gets canceled.
Represents a composite of cancelables that are stacked, so you can push a new reference, or pop an existing one and when it gets canceled, then the whole stack gets canceled.
Similar in spirit with CompositeCancelable, except that you can only pull out references in a FIFO fashion.
Used in the implementation of
monix.eval.Task
.
Deprecated Type Members
- type MultiAssignmentCancelable = OrderedCancelable
DEPRECATED — renamed to OrderedCancelable.
DEPRECATED — renamed to OrderedCancelable.
- Annotations
- @deprecated
- Deprecated
(Since version 3.0.0) Renamed to OrderedCancelable
- type SingleAssignmentCancelable = SingleAssignCancelable
DEPRECATED — renamed to SingleAssignCancelable.
DEPRECATED — renamed to SingleAssignCancelable.
- Annotations
- @deprecated
- Deprecated
(Since version 3.0.0) Renamed to SingleAssignCancelable
Value Members
- object AssignableCancelable extends Serializable
- object BooleanCancelable extends Serializable
- object ChainedCancelable extends Serializable
- object CompositeCancelable extends Serializable
- object MultiAssignCancelable extends Serializable
- object OrderedCancelable extends Serializable
- object RefCountCancelable extends Serializable
- object SerialCancelable extends Serializable
- object SingleAssignCancelable extends Serializable
- object StackedCancelable extends Serializable
Deprecated Value Members
- val MultiAssignmentCancelable: OrderedCancelable.type
DEPRECATED — renamed to OrderedCancelable.
DEPRECATED — renamed to OrderedCancelable.
- Annotations
- @deprecated
- Deprecated
(Since version 3.0.0) Renamed to OrderedCancelable
- val SingleAssignmentCancelable: SingleAssignCancelable.type
DEPRECATED — renamed to SingleAssignCancelable.
DEPRECATED — renamed to SingleAssignCancelable.
- Annotations
- @deprecated
- Deprecated
(Since version 3.0.0) Renamed to SingleAssignCancelable
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.