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
13 changes: 13 additions & 0 deletions core/shared/src/main/scala/zio/ZLayer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ sealed abstract class ZLayer[-RIn, +E, +ROut] { self =>
fold(failureOrDie >>> handler, ZLayer.identity)
}

/**
* Constructs a layer dynamically based on the output of this layer.
*/
final def flatMap[RIn1 <: RIn, E1 >: E, ROut2](
f: ROut => ZLayer[RIn1, E1, ROut2]
): ZLayer[RIn1, E1, ROut2] =
ZLayer.Flatten(self.map(f))

/**
* Feeds the error or output services of this layer into the input of either
* the specified `failure` or `success` layers, resulting in a new layer with
Expand Down Expand Up @@ -284,6 +292,8 @@ sealed abstract class ZLayer[-RIn, +E, +ROut] { self =>

private final def scope: Managed[Nothing, ZLayer.MemoMap => ZManaged[RIn, E, ROut]] =
self match {
case ZLayer.Flatten(self) =>
ZManaged.succeed(memoMap => memoMap.getOrElseMemoize(self).flatMap(memoMap.getOrElseMemoize))
case ZLayer.Fold(self, failure, success) =>
ZManaged.succeed(memoMap =>
memoMap
Expand All @@ -310,6 +320,9 @@ object ZLayer {
@deprecated("use Layer", "1.0.0")
type NoDeps[+E, +B] = ZLayer[Any, E, B]

private final case class Flatten[-RIn, +E, +ROut](
self: ZLayer[RIn, E, ZLayer[RIn, E, ROut]]
) extends ZLayer[RIn, E, ROut]
private final case class Fold[RIn, E, E1, ROut, ROut1](
self: ZLayer[RIn, E, ROut],
failure: ZLayer[(RIn, Cause[E]), E1, ROut1],
Expand Down