sealed abstract class ZFiberRef[+EA, +EB, -A, +B] extends Serializable
A FiberRef
is ZIO's equivalent of Java's ThreadLocal
. The value of a
FiberRef
is automatically propagated to child fibers when they are forked
and merged back in to the value of the parent fiber after they are joined.
for { fiberRef <- FiberRef.make("Hello world!") child <- fiberRef.set("Hi!).fork result <- child.join } yield result
Here result
will be equal to "Hi!" since changed made by a child fiber are
merged back in to the value of the parent fiber on join.
By default the value of the child fiber will replace the value of the parent fiber on join but you can specify your own logic for how values should be merged.
for { fiberRef <- FiberRef.make(0, math.max) child <- fiberRef.update(_ + 1).fork _ <- fiberRef.update(_ + 2) _ <- child.join value <- fiberRef.get } yield value
Here value
will be 2 as the value in the joined fiber is lower and we
specified max
as our combining function.
- Self Type
- ZFiberRef[EA, EB, A, B]
- Alphabetic
- By Inheritance
- ZFiberRef
- Serializable
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Abstract Value Members
- abstract def fold[EC, ED, C, D](ea: (EA) => EC, eb: (EB) => ED, ca: (C) => Either[EC, A], bd: (B) => Either[ED, D]): ZFiberRef[EC, ED, C, D]
Folds over the error and value types of the
ZFiberRef
.Folds over the error and value types of the
ZFiberRef
. This is a highly polymorphic method that is capable of arbitrarily transforming the error and value types of theZFiberRef
. For most use cases one of the more specific combinators implemented in terms offold
will be more ergonomic but this method is extremely useful for implementing new combinators. - abstract def foldAll[EC, ED, C, D](ea: (EA) => EC, eb: (EB) => ED, ec: (EB) => EC, ca: (C) => (B) => Either[EC, A], bd: (B) => Either[ED, D]): ZFiberRef[EC, ED, C, D]
Folds over the error and value types of the
ZFiberRef
, allowing access to the state in transforming theset
value.Folds over the error and value types of the
ZFiberRef
, allowing access to the state in transforming theset
value. This is a more powerful version offold
but requires unifying the error types. - abstract def get(implicit trace: ZTraceElement): IO[EB, B]
Reads the value associated with the current fiber.
Reads the value associated with the current fiber. Returns initial value if no value was
set
or inherited from parent. - abstract def initialValue: Either[EB, B]
Returns the initial value or error.
- abstract def locally[R, EC >: EA, C](value: A)(use: ZIO[R, EC, C])(implicit trace: ZTraceElement): ZIO[R, EC, C]
Returns an
IO
that runs withvalue
bound to the current fiber.Returns an
IO
that runs withvalue
bound to the current fiber.Guarantees that fiber data is properly restored via
acquireRelease
. - abstract def locallyManaged(value: A)(implicit trace: ZTraceElement): ZManaged[Any, EA, Unit]
Returns a managed effect that sets the value associated with the curent fiber to the specified value as its
acquire
action and restores it to its original value as itsrelease
action. - abstract def set(value: A)(implicit trace: ZTraceElement): IO[EA, Unit]
Sets the value associated with the current fiber.
Concrete 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
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- def collect[C](pf: PartialFunction[B, C]): ZFiberRef[EA, Option[EB], A, C]
Maps and filters the
get
value of theZFiberRef
with the specified partial function, returning aZFiberRef
with aget
value that succeeds with the result of the partial function if it is defined or else fails withNone
. - def contramap[C](f: (C) => A): ZFiberRef[EA, EB, C, B]
Transforms the
set
value of theZFiberRef
with the specified function. - def contramapEither[EC >: EA, C](f: (C) => Either[EC, A]): ZFiberRef[EC, EB, C, B]
Transforms the
set
value of theZFiberRef
with the specified fallible function. - def dimap[C, D](f: (C) => A, g: (B) => D): ZFiberRef[EA, EB, C, D]
Transforms both the
set
andget
values of theZFiberRef
with the specified functions. - def dimapEither[EC >: EA, ED >: EB, C, D](f: (C) => Either[EC, A], g: (B) => Either[ED, D]): ZFiberRef[EC, ED, C, D]
Transforms both the
set
andget
values of theZFiberRef
with the specified fallible functions. - def dimapError[EC, ED](f: (EA) => EC, g: (EB) => ED): ZFiberRef[EC, ED, A, B]
Transforms both the
set
andget
errors of theZFiberRef
with the specified functions. - final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def filterInput[A1 <: A](f: (A1) => Boolean): ZFiberRef[Option[EA], EB, A1, B]
Filters the
set
value of theZFiberRef
with the specified predicate, returning aZFiberRef
with aset
value that succeeds if the predicate is satisfied or else fails withNone
. - def filterOutput(f: (B) => Boolean): ZFiberRef[EA, Option[EB], A, B]
Filters the
get
value of theZFiberRef
with the specified predicate, returning aZFiberRef
with aget
value that succeeds if the predicate is satisfied or else fails withNone
. - def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def getWith[R, EC >: EB, C](f: (B) => ZIO[R, EC, C])(implicit trace: ZTraceElement): ZIO[R, EC, C]
Gets the value associated with the current fiber and uses it to run the specified effect.
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def map[C](f: (B) => C): ZFiberRef[EA, EB, A, C]
Transforms the
get
value of theZFiberRef
with the specified function. - def mapEither[EC >: EB, C](f: (B) => Either[EC, C]): ZFiberRef[EA, EC, A, C]
Transforms the
get
value of theZFiberRef
with the specified fallible function. - final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- def readOnly: ZFiberRef[EA, EB, Nothing, B]
Returns a read only view of the
ZFiberRef
. - final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- def writeOnly: ZFiberRef[EA, Unit, A, Nothing]
Returns a write only view of the
ZFiberRef
.