Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions core/shared/src/main/scala/zio/FiberRef.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,31 @@ final class FiberRef[A] private[zio] (
(result, result)
}

/**
* Returns an `IO` that runs with result of calling the specified function
* bound to the current fiber.
*
* Guarantees that fiber data is properly restored via `bracket`.
*/
def updateLocally[R, E, B](f: A => A)(use: ZIO[R, E, B]): ZIO[R, E, B] =
for {
oldValue <- get
b <- set(f(oldValue)).bracket_(set(oldValue))(use)
} yield b

/**
* Returns an `IO` that runs with result of calling the specified partial
* function bound to the current fiber.
*
* Guarantees that fiber data is properly restored via `bracket`.
*/
def updateSomeLocally[R, E, B](pf: PartialFunction[A, A])(use: ZIO[R, E, B]): ZIO[R, E, B] =
for {
oldValue <- get
value = pf.applyOrElse[A, A](oldValue, identity)
b <- set(value).bracket_(set(oldValue))(use)
} yield b

/**
* Atomically modifies the `FiberRef` with the specified partial function.
* If the function is undefined on the current value it doesn't change it.
Expand Down