Packages

final class IOLocal[A] extends AnyRef

A IOLocal is like a ThreadLocal that is pure and with a flexible scope, being processed in the context of the IO data type.

This data type wraps monix.execution.misc.Local.

Just like a ThreadLocal, usage of a IOLocal is safe, the state of all current locals being transported over async boundaries (aka when threads get forked) by the Task run-loop implementation, but only when the Task reference gets executed with IO.Options.localContextPropagation set to true, or it uses a monix.execution.schedulers.TracingScheduler.

One way to achieve this is with IO.executeWithOptions, a single call is sufficient just before runAsync:

import monix.execution.Scheduler.Implicits.global

val t = Task(42)
t.executeWithOptions(_.enableLocalContextPropagation)
  // triggers the actual execution
  .runToFuture

Another possibility is to use IO.runToFutureOpt or IO.runToFutureOpt instead of runAsync and specify the set of options implicitly:

{
  implicit val options = IO.defaultOptions.enableLocalContextPropagation

  // Options passed implicitly
  val f = t.runToFutureOpt
}

Full example:

import monix.bio.{UIO, IOLocal}

val task: UIO[Unit] =
  for {
    local <- IOLocal(0)
    value1 <- local.read // value1 == 0
    _ <- local.write(100)
    value2 <- local.read // value2 == 100
    value3 <- local.bind(200)(local.read.map(_ * 2)) // value3 == 200 * 2
    value4 <- local.read // value4 == 100
    _ <- local.clear
    value5 <- local.read // value5 == 0
  } yield {
    // Should print 0, 100, 400, 100, 0
    println("value1: " + value1)
    println("value2: " + value2)
    println("value3: " + value3)
    println("value4: " + value4)
    println("value5: " + value5)
  }

// For transporting locals over async boundaries defined by
// Task, any Scheduler will do, however for transporting locals
// over async boundaries managed by Future and others, you need
// a `TracingScheduler` here:
import monix.execution.Scheduler.Implicits.global

// Needs enabling the "localContextPropagation" option
// just before execution
implicit val opts = IO.defaultOptions.enableLocalContextPropagation

// Triggering actual execution
val result = task.runToFutureOpt
Source
IOLocal.scala
Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. IOLocal
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##: Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def bind[E, R](value: A)(task: IO[E, R]): IO[E, R]

    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: UIO[Int] =
      for {
        local <- IOLocal(0)
        value <- local.bind(100)(local.read.map(_ * 2))
      } yield value
    value

    is the value to be set in this local var when the task evaluation is triggered (aka lazily)

    task

    is the IO 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

    See also

    bindL for the version with a lazy value.

  6. def bindClear[E, R](task: IO[E, R]): IO[E, R]

    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: UIO[Int] =
      for {
        local <- IOLocal(0)
        value <- local.bindClear(local.read.map(_ * 2))
      } yield value
    task

    is the IO to wrap, having the local cleared, returning the default as the response to read queries and transported over asynchronous boundaries — on finish the local gets reset to the previous value

  7. def bindL[E, R](value: IO[E, A])(task: IO[E, R]): IO[E, R]

    Binds the local var to a value for the duration of the given task execution, the value itself being lazily evaluated in the IO context.

    Binds the local var to a value for the duration of the given task execution, the value itself being lazily evaluated in the IO context.

    // Should yield 200 on execution, regardless of what value
    // we have in `local` at the time of evaluation
    val task: UIO[Int] =
      for {
        local <- IOLocal(0)
        value <- local.bindL(UIO.eval(100))(local.read.map(_ * 2))
      } yield value
    value

    is the value to be set in this local var when the task evaluation is triggered (aka lazily)

    task

    is the IO 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

    See also

    bind for the version with a strict value.

  8. def clear: UIO[Unit]

    Clears the local value, making it return its default.

  9. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native()
  10. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  11. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  12. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable])
  13. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  14. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  15. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  16. def local: UIO[Local[A]]

    Returns monix.execution.misc.Local instance used in this IOLocal.

    Returns monix.execution.misc.Local instance used in this IOLocal.

    Note that IOLocal.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.

  17. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  18. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  19. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  20. def read: UIO[A]

    Returns the current local value (in the IO context).

  21. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  22. def toString(): String
    Definition Classes
    AnyRef → Any
  23. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  24. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  25. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()
  26. def write(value: A): UIO[Unit]

    Updates the local value.

Inherited from AnyRef

Inherited from Any

Ungrouped