object Task extends TaskInstances with Serializable
- Source
- Task.scala
- Alphabetic
- By Inheritance
- Task
- Serializable
- Serializable
- TaskInstances
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Type Members
-
final
case class
Context
(scheduler: Scheduler, connection: StackedCancelable, frameRef: ThreadLocal[FrameIndex], options: Options) extends Product with Serializable
The
Context
under which Task is supposed to be executed.The
Context
under which Task is supposed to be executed.- scheduler
is the Scheduler in charge of evaluation on
runAsync
.- connection
is the StackedCancelable that handles the cancellation on
runAsync
- frameRef
is a thread-local counter that keeps track of the current frame index of the run-loop. The run-loop is supposed to force an asynchronous boundary upon reaching a certain threshold, when the task is evaluated with monix.execution.schedulers.ExecutionModel.BatchedExecution. And this
frameIndexRef
should be reset whenever a real asynchronous boundary happens.- options
is a set of options for customizing the task's behavior upon evaluation.
-
type
FrameIndex = Int
A frame index is a number representing the current run-loop cycle.
A frame index is a number representing the current run-loop cycle. It gets used for automatically forcing asynchronous boundaries, according to the ExecutionModel injected by the Scheduler.
-
type
OnFinish[+A] = (Context, Callback[A]) ⇒ Unit
Type alias representing callbacks for asynchronous tasks.
-
final
case class
Options
(autoCancelableRunLoops: Boolean) extends Product with Serializable
Set of options for customizing the task's behavior.
Set of options for customizing the task's behavior.
- autoCancelableRunLoops
should be set to
true
in case you wantflatMap
driven loops to be auto-cancelable. Defaults tofalse
.
-
class
TypeClassInstances extends Instance[Task] with Instance[Task, Throwable] with Instance[Task] with Instance[Task]
Groups the implementation for the type-classes defined in monix.types.
Groups the implementation for the type-classes defined in monix.types.
- Definition Classes
- TaskInstances
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
-
def
apply[A](f: ⇒ A): Task[A]
Returns a new task that, when executed, will emit the result of the given function, executed asynchronously.
Returns a new task that, when executed, will emit the result of the given function, executed asynchronously.
- f
is the callback to execute asynchronously
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
async[A](register: (Scheduler, Callback[A]) ⇒ Cancelable): Task[A]
Create a
Task
from an asynchronous computation.Create a
Task
from an asynchronous computation.Alias for create.
-
def
chooseFirstOf[A, B](fa: Task[A], fb: Task[B]): Task[Either[(A, CancelableFuture[B]), (CancelableFuture[A], B)]]
Creates a
Task
that upon execution will execute both given tasks (possibly in parallel in case the tasks are asynchronous) and will return the result of the task that manages to complete first, along with a cancelable future of the other task.Creates a
Task
that upon execution will execute both given tasks (possibly in parallel in case the tasks are asynchronous) and will return the result of the task that manages to complete first, along with a cancelable future of the other task.If the first task that completes
-
def
chooseFirstOfList[A](tasks: TraversableOnce[Task[A]]): Task[A]
Creates a
Task
that upon execution will return the result of the first completed task in the given list and then cancel the rest. -
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
- def coeval[A](a: Coeval[A]): Task[A]
-
def
create[A](register: (Scheduler, Callback[A]) ⇒ Cancelable): Task[A]
Create a
Task
from an asynchronous computation, which takes the form of a function with which we can register a callback.Create a
Task
from an asynchronous computation, which takes the form of a function with which we can register a callback.This can be used to translate from a callback-based API to a straightforward monadic version.
Contract:
- execution of the
register
callback is asynchronous, always forking a (logical) thread - execution of the
onSuccess
andonError
callbacks, is also async, however they are executed on the current thread / call-stack if the scheduler is enhanced for execution of trampolined runnables
This asynchrony is needed because create is supposed to be safe or otherwise, depending on the executed logic, one can end up with a stack overflow exception. So this contract happens in order to guarantee safety. In order to bypass this, one can use unsafeCreate, but that's more difficult and meant for people knowing what they are doing.
- register
is a function that will be called when this
Task
is executed, receiving a callback as a parameter, a callback that the user is supposed to call in order to signal the desired outcome of thisTask
.
- execution of the
- val defaultOptions: Options
-
def
defer[A](fa: ⇒ Task[A]): Task[A]
Promote a non-strict value representing a Task to a Task of the same type.
-
def
delay[A](a: ⇒ A): Task[A]
Alias for eval.
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
eval[A](a: ⇒ A): Task[A]
Promote a non-strict value to a Task, catching exceptions in the process.
Promote a non-strict value to a Task, catching exceptions in the process.
Note that since
Task
is not memoized, this will recompute the value each time theTask
is executed. -
def
evalOnce[A](a: ⇒ A): Task[A]
Promote a non-strict value to a Task that is memoized on the first evaluation, the result being then available on subsequent evaluations.
-
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
def
fork[A](fa: Task[A], scheduler: Scheduler): Task[A]
Mirrors the given source
Task
, but upon execution ensure that evaluation forks into a separate (logical) thread.Mirrors the given source
Task
, but upon execution ensure that evaluation forks into a separate (logical) thread.The given Scheduler will be used for execution of the Task, effectively overriding the
Scheduler
that's passed inrunAsync
. Thus you can execute a wholeTask
on a separate thread-pool, useful for example in case of doing I/O.- fa
is the task that will get executed asynchronously
- scheduler
is the scheduler to use for execution
-
def
fork[A](fa: Task[A]): Task[A]
Mirrors the given source
Task
, but upon execution ensure that evaluation forks into a separate (logical) thread.Mirrors the given source
Task
, but upon execution ensure that evaluation forks into a separate (logical) thread.The Scheduler used will be the one that is used to start the run-loop in
runAsync
.- fa
is the task that will get executed asynchronously
-
def
fromFuture[A](f: Future[A]): Task[A]
Converts the given Scala
Future
into aTask
.Converts the given Scala
Future
into aTask
.NOTE: if you want to defer the creation of the future, use in combination with defer.
-
def
fromTry[A](a: Try[A]): Task[A]
Builds a Task instance out of a Scala
Try
. -
def
gather[A, M[X] <: TraversableOnce[X]](in: M[Task[A]])(implicit cbf: CanBuildFrom[M[Task[A]], A, M[A]]): Task[M[A]]
Nondeterministically gather results from the given collection of tasks, returning a task that will signal the same type of collection of results once all tasks are finished.
Nondeterministically gather results from the given collection of tasks, returning a task that will signal the same type of collection of results once all tasks are finished.
This function is the nondeterministic analogue of
sequence
and should behave identically tosequence
so long as there is no interaction between the effects being gathered. However, unlikesequence
, which decides on a total order of effects, the effects in agather
are unordered with respect to each other.Although the effects are unordered, we ensure the order of results matches the order of the input sequence. Also see gatherUnordered for the more efficient alternative.
-
def
gatherUnordered[A](in: TraversableOnce[Task[A]]): Task[List[A]]
Nondeterministically gather results from the given collection of tasks, without keeping the original ordering of results.
Nondeterministically gather results from the given collection of tasks, without keeping the original ordering of results.
If the tasks in the list are set to execute asynchronously, forking logical threads, then the tasks will execute in parallel.
This function is similar to gather, but neither the effects nor the results will be ordered. Useful when you don't need ordering because:
- it has non-blocking behavior (but not wait-free)
- it can be more efficient (compared with gather), but not necessarily (if you care about performance, then test)
- in
is a list of tasks to execute
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
mapBoth[A1, A2, R](fa1: Task[A1], fa2: Task[A2])(f: (A1, A2) ⇒ R): Task[R]
Apply a mapping functions to the results of two tasks, nondeterministically ordering their effects.
Apply a mapping functions to the results of two tasks, nondeterministically ordering their effects.
If the two tasks are synchronous, they'll get executed one after the other, with the result being available asynchronously. If the two tasks are asynchronous, they'll get scheduled for execution at the same time and in a multi-threading environment they'll execute in parallel and have their results synchronized.
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
never[A]: Task[A]
A Task instance that upon evaluation will never complete.
-
implicit
val
nondeterminism: TypeClassInstances
Type-class instances for Task that have nondeterministic effects for Applicative.
Type-class instances for Task that have nondeterministic effects for Applicative.
It can be optionally imported in scope to make
map2
andap
to potentially run tasks in parallel.- Definition Classes
- TaskInstances
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
-
def
now[A](a: A): Task[A]
Returns a
Task
that on execution is always successful, emitting the given strict value. -
def
pure[A](a: A): Task[A]
Lifts a value into the task context.
Lifts a value into the task context. Alias for now.
-
def
raiseError[A](ex: Throwable): Task[A]
Returns a task that on execution is always finishing in error emitting the specified exception.
-
def
sequence[A, M[X] <: TraversableOnce[X]](in: M[Task[A]])(implicit cbf: CanBuildFrom[M[Task[A]], A, M[A]]): Task[M[A]]
Given a
TraversableOnce
of tasks, transforms it to a task signaling the collection, executing the tasks one by one and gathering their results in the same collection.Given a
TraversableOnce
of tasks, transforms it to a task signaling the collection, executing the tasks one by one and gathering their results in the same collection.This operation will execute the tasks one by one, in order, which means that both effects and results will be ordered. See gather and gatherUnordered for unordered results or effects, and thus potential of running in parallel.
It's a simple version of traverse.
-
def
suspend[A](fa: ⇒ Task[A]): Task[A]
Alias for defer.
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
tailRecM[A, B](a: A)(f: (A) ⇒ Task[Either[A, B]]): Task[B]
Keeps calling
f
until it returns aRight
result.Keeps calling
f
until it returns aRight
result.Based on Phil Freeman's Stack Safety for Free.
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
def
traverse[A, B, M[X] <: TraversableOnce[X]](in: M[A])(f: (A) ⇒ Task[B])(implicit cbf: CanBuildFrom[M[A], B, M[B]]): Task[M[B]]
Given a
TraversableOnce[A]
and a functionA => Task[B]
, sequentially apply the function to each element of the collection and gather their results in the same collection.Given a
TraversableOnce[A]
and a functionA => Task[B]
, sequentially apply the function to each element of the collection and gather their results in the same collection.It's a generalized version of sequence.
-
implicit
val
typeClassInstances: TypeClassInstances
Type-class instances for Task.
-
final
val
unit: Task[Unit]
A
Task[Unit]
provided for convenience. -
def
unsafeCreate[A](onFinish: OnFinish[A]): Task[A]
Constructs a lazy Task instance whose result will be computed asynchronously.
Constructs a lazy Task instance whose result will be computed asynchronously.
**WARNING:** Unsafe to use directly, only use if you know what you're doing. For building
Task
instances safely see Task.create.Rules of usage:
- the received
StackedCancelable
can be used to store cancelable references that will be executed upon cancel; everypush
must happen at the beginning, before any execution happens andpop
must happen afterwards when the processing is finished, before signaling the result - the received
FrameRef
indicates the current frame index and must be reset on real asynchronous boundaries (which avoids doing extra async boundaries in batched execution mode) - before execution, an asynchronous boundary is recommended, to avoid stack overflow errors, but can happen using the scheduler's facilities for trampolined execution
- on signaling the result (
onSuccess
,onError
), another async boundary is necessary, but can also happen with the scheduler's facilities for trampolined execution (e.g.asyncOnSuccess
andasyncOnError
)
**WARNING:** note that not only is this builder unsafe, but also unstable, as the OnFinish callback type is exposing volatile internal implementation details. This builder is meant to create optimized asynchronous tasks, but for normal usage prefer Task.create.
- the received
-
def
unsafeStartAsync[A](source: Task[A], context: Context, cb: Callback[A]): Unit
Unsafe utility - starts the execution of a Task with a guaranteed asynchronous boundary, by providing the needed Scheduler, StackedCancelable and Callback.
Unsafe utility - starts the execution of a Task with a guaranteed asynchronous boundary, by providing the needed Scheduler, StackedCancelable and Callback.
DO NOT use directly, as it is UNSAFE to use, unless you know what you're doing. Prefer Task.runAsync and
Task.fork
. -
def
unsafeStartNow[A](source: Task[A], context: Context, cb: Callback[A]): Unit
Unsafe utility - starts the execution of a Task, by providing the needed Scheduler, StackedCancelable and Callback.
Unsafe utility - starts the execution of a Task, by providing the needed Scheduler, StackedCancelable and Callback.
DO NOT use directly, as it is UNSAFE to use, unless you know what you're doing. Prefer Task.runAsync.
-
def
unsafeStartTrampolined[A](source: Task[A], context: Context, cb: Callback[A]): Unit
Unsafe utility - starts the execution of a Task with a guaranteed trampolined asynchronous boundary, by providing the needed Scheduler, StackedCancelable and Callback.
Unsafe utility - starts the execution of a Task with a guaranteed trampolined asynchronous boundary, by providing the needed Scheduler, StackedCancelable and Callback.
DO NOT use directly, as it is UNSAFE to use, unless you know what you're doing. Prefer Task.runAsync and
Task.fork
. -
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
def
wander[A, B, M[X] <: TraversableOnce[X]](in: M[A])(f: (A) ⇒ Task[B])(implicit cbf: CanBuildFrom[M[A], B, M[B]]): Task[M[B]]
Given a
TraversableOnce[A]
and a functionA => Task[B]
, nondeterministically apply the function to each element of the collection and return a task that will signal a collection of the results once all tasks are finished.Given a
TraversableOnce[A]
and a functionA => Task[B]
, nondeterministically apply the function to each element of the collection and return a task that will signal a collection of the results once all tasks are finished.This function is the nondeterministic analogue of
traverse
and should behave identically totraverse
so long as there is no interaction between the effects being gathered. However, unliketraverse
, which decides on a total order of effects, the effects in awander
are unordered with respect to each other.Although the effects are unordered, we ensure the order of results matches the order of the input sequence. Also see wanderUnordered for the more efficient alternative.
It's a generalized version of gather.
-
def
wanderUnordered[A, B, M[X] <: TraversableOnce[X]](in: M[A])(f: (A) ⇒ Task[B]): Task[List[B]]
Given a
TraversableOnce[A]
and a functionA => Task[B]
, nondeterministically apply the function to each element of the collection without keeping the original ordering of the results.Given a
TraversableOnce[A]
and a functionA => Task[B]
, nondeterministically apply the function to each element of the collection without keeping the original ordering of the results.This function is similar to wander, but neither the effects nor the results will be ordered. Useful when you don't need ordering because:
- it has non-blocking behavior (but not wait-free)
- it can be more efficient (compared with wander), but not necessarily (if you care about performance, then test)
It's a generalized version of gatherUnordered.
-
def
zip2[A1, A2, R](fa1: Task[A1], fa2: Task[A2]): Task[(A1, A2)]
Pairs two Task instances.
-
def
zip3[A1, A2, A3](fa1: Task[A1], fa2: Task[A2], fa3: Task[A3]): Task[(A1, A2, A3)]
Pairs three Task instances.
-
def
zip4[A1, A2, A3, A4](fa1: Task[A1], fa2: Task[A2], fa3: Task[A3], fa4: Task[A4]): Task[(A1, A2, A3, A4)]
Pairs four Task instances.
-
def
zip5[A1, A2, A3, A4, A5](fa1: Task[A1], fa2: Task[A2], fa3: Task[A3], fa4: Task[A4], fa5: Task[A5]): Task[(A1, A2, A3, A4, A5)]
Pairs five Task instances.
-
def
zip6[A1, A2, A3, A4, A5, A6](fa1: Task[A1], fa2: Task[A2], fa3: Task[A3], fa4: Task[A4], fa5: Task[A5], fa6: Task[A6]): Task[(A1, A2, A3, A4, A5, A6)]
Pairs six Task instances.
-
def
zipList[A](sources: Task[A]*): Task[List[A]]
Gathers the results from a sequence of tasks into a single list.
Gathers the results from a sequence of tasks into a single list. The effects are not ordered, but the results are.
-
def
zipMap2[A1, A2, R](fa1: Task[A1], fa2: Task[A2])(f: (A1, A2) ⇒ R): Task[R]
Pairs two Task instances, creating a new instance that will apply the given mapping function to the resulting pair.
-
def
zipMap3[A1, A2, A3, R](fa1: Task[A1], fa2: Task[A2], fa3: Task[A3])(f: (A1, A2, A3) ⇒ R): Task[R]
Pairs three Task instances, applying the given mapping function to the result.
-
def
zipMap4[A1, A2, A3, A4, R](fa1: Task[A1], fa2: Task[A2], fa3: Task[A3], fa4: Task[A4])(f: (A1, A2, A3, A4) ⇒ R): Task[R]
Pairs four Task instances, applying the given mapping function to the result.
-
def
zipMap5[A1, A2, A3, A4, A5, R](fa1: Task[A1], fa2: Task[A2], fa3: Task[A3], fa4: Task[A4], fa5: Task[A5])(f: (A1, A2, A3, A4, A5) ⇒ R): Task[R]
Pairs five Task instances, applying the given mapping function to the result.
-
def
zipMap6[A1, A2, A3, A4, A5, A6, R](fa1: Task[A1], fa2: Task[A2], fa3: Task[A3], fa4: Task[A4], fa5: Task[A5], fa6: Task[A6])(f: (A1, A2, A3, A4, A5, A6) ⇒ R): Task[R]
Pairs six Task instances, applying the given mapping function to the result.
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.eval is for dealing with evaluation of results, thus exposing Task and Coeval.
monix.reactive exposes the
Observable
pattern:Observable
implementationsmonix.types implements type-class shims, to be translated to type-classes provided by libraries such as Cats or Scalaz.
monix.cats is the optional integration with the Cats library, providing translations for the types described in
monix.types
.monix.scalaz is the optional integration with the Scalaz library, providing translations for the types described in
monix.types
.