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

Skip to content
Merged
Show file tree
Hide file tree
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
21 changes: 10 additions & 11 deletions core/shared/src/main/scala/zio/ZIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1244,10 +1244,7 @@ sealed trait ZIO[-R, +E, +A]
final def provideLayer[E1 >: E, R0](
layer: => ZLayer[R0, E1, R]
)(implicit trace: Trace): ZIO[R0, E1, A] =
ZIO.acquireReleaseExitWith(Scope.make)((scope: Scope.Closeable, exit: Exit[Any, Any]) => scope.close(exit)) {
scope =>
layer.build(scope).flatMap(r => self.provideEnvironment(r))
}
ZIO.scopedWith(scope => layer.build(scope).flatMap(r => self.provideEnvironment(r)))

/**
* Transforms the environment being provided to this effect with the specified
Expand Down Expand Up @@ -4468,6 +4465,14 @@ object ZIO extends ZIOCompanionPlatformSpecific with ZIOCompanionVersionSpecific
def scoped[R]: ScopedPartiallyApplied[R] =
new ScopedPartiallyApplied[R]

/**
* Creates a scope, uses it to perform the specified effect, and closes the
* scope as soon as the effect completes, whether by success, failure, or
* interruption.
*/
def scopedWith[R, E, A](f: Scope => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] =
Scope.make.flatMap(scope => f(scope).onExit(scope.close(_)))

/**
* Accesses the current scope and uses it to perform the specified effect.
*/
Expand Down Expand Up @@ -5346,13 +5351,7 @@ object ZIO extends ZIOCompanionPlatformSpecific with ZIOCompanionVersionSpecific
def apply[R1, E, A, B](
resource: ZIO[R with Scope, E, A]
)(use: A => ZIO[R1, E, B])(implicit trace: Trace): ZIO[R with R1, E, B] =
ZIO.acquireReleaseExitWith {
Scope.make
} { (scope: Scope.Closeable, exit: Exit[Any, Any]) =>
scope.close(exit)
} { scope =>
scope.extend[R](resource).flatMap(use)
}
ZIO.scopedWith(_.extend[R](resource).flatMap(use))
}

final class ServiceAtPartiallyApplied[Service](private val dummy: Boolean = true) extends AnyVal {
Expand Down
25 changes: 24 additions & 1 deletion streams-tests/shared/src/test/scala/zio/stream/ZStreamSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,30 @@ object ZStreamSpec extends ZIOBaseSpec {
} yield assert(results)(
equalTo(List("OuterRelease", "InnerRelease", "InnerAcquire", "OuterAcquire"))
)
} @@ nonFlaky
} @@ nonFlaky,
test("preserves the scope") {
for {
ref <- Ref.make[Chunk[String]](Chunk.empty)
scope <- Scope.make
_ <- scope.extend {
ZStream(1, 2)
.flatMapPar(1) { i =>
ZStream.fromZIO {
ZIO.acquireRelease {
ref.update(_ :+ s"acquire $i")
} { _ =>
ref.update(_ :+ s"release $i")
}
}
}
.runDrain
}
before <- ref.getAndSet(Chunk.empty)
_ <- scope.close(Exit.unit)
after <- ref.get
} yield assertTrue(before == Chunk("acquire 1", "acquire 2")) &&
assertTrue(after == Chunk("release 2", "release 1"))
}
),
suite("flatMapParSwitch")(
test("guarantee ordering no parallelism") {
Expand Down
Loading