Binds the local var to a value
for the duration of the given
task
execution.
Binds the local var to a value
for the duration of the given
task
execution.
// Should yield 200 on execution, regardless of what value // we have in `local` at the time of evaluation val task: Task[Int] = for { local <- TaskLocal(0) value <- local.bind(100)(local.read.map(_ * 2)) } yield value
is the value to be set in this local var when the task evaluation is triggered (aka lazily)
is the Task to wrap, having the given value
as the response to read queries and transported
over asynchronous boundaries — on finish the local gets
reset to the previous value
bindL for the version with a lazy value
.
Clears the local var to the default for the duration of the
given task
execution.
Clears the local var to the default for the duration of the
given task
execution.
// Should yield 0 on execution, regardless of what value // we have in `local` at the time of evaluation val task: Task[Int] = for { local <- TaskLocal(0) value <- local.bindClear(local.read.map(_ * 2)) } yield value
Binds the local var to a value
for the duration of the given
task
execution, the value
itself being lazily evaluated
in the Task context.
Binds the local var to a value
for the duration of the given
task
execution, the value
itself being lazily evaluated
in the Task context.
// Should yield 200 on execution, regardless of what value // we have in `local` at the time of evaluation val task: Task[Int] = for { local <- TaskLocal(0) value <- local.bindL(Task.eval(100))(local.read.map(_ * 2)) } yield value
is the value to be set in this local var when the task evaluation is triggered (aka lazily)
is the Task to wrap, having the given value
as the response to read queries and transported
over asynchronous boundaries — on finish the local gets
reset to the previous value
bind for the version with a strict value
.
Clears the local value, making it return its default
.
Returns monix.execution.misc.Local instance used in this TaskLocal.
Returns monix.execution.misc.Local instance used in this TaskLocal.
Note that TaskLocal.bind
will restore the original local value
on the thread where the Task's
run-loop ends up so it might lead
to leaving local modified in other thread.
Returns the current local value (in the Task
context).
Updates the local value.
A
TaskLocal
is like a ThreadLocal that is pure and with a flexible scope, being processed in the context of the Task data type.This data type wraps monix.execution.misc.Local.
Just like a
ThreadLocal
, usage of aTaskLocal
is safe, the state of all current locals being transported over async boundaries (aka when threads get forked) by theTask
run-loop implementation, but only when theTask
reference gets executed with Task.Options.localContextPropagation set totrue
.One way to achieve this is with Task.executeWithOptions, a single call is sufficient just before
runAsync
:Another possibility is to use Task.runToFutureOpt or Task.runToFutureOpt instead of
runAsync
and specify the set of options implicitly:Full example: