-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor ZStream to use ZManaged #906
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -292,6 +292,33 @@ final case class ZManaged[-R, +E, +A](reserve: ZIO[R, E, Reservation[R, E, A]]) | |
} | ||
} | ||
|
||
/** | ||
* Creates a `ZManaged` value that acquires the original resource in a fiber, | ||
* and provides that fiber. The finalizer for this value will interrupt the fiber | ||
* and run the original finalizer. | ||
*/ | ||
final def fork: ZManaged[R, Nothing, Fiber[E, A]] = | ||
ZManaged { | ||
for { | ||
finalizer <- Ref.make[ZIO[R, Nothing, _]](UIO.unit) | ||
// The reservation phase of the new `ZManaged` runs uninterruptibly; | ||
// so to make sure the acquire phase of the original `ZManaged` runs | ||
// interruptibly, we need to create an interruptible hole in the region. | ||
fiber <- ZIO.interruptibleMask { restore => | ||
restore { | ||
for { | ||
reservation <- self.reserve | ||
_ <- finalizer.set(reservation.release) | ||
} yield reservation | ||
} >>= (_.acquire) | ||
}.fork | ||
reservation = Reservation( | ||
acquire = UIO.succeed(fiber), | ||
release = fiber.interrupt *> finalizer.get.flatMap(identity(_)) | ||
) | ||
} yield reservation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! 👍 |
||
} | ||
|
||
/** | ||
* Unwraps the optional success of this effect, but can fail with unit value. | ||
*/ | ||
|
@@ -313,6 +340,16 @@ final case class ZManaged[-R, +E, +A](reserve: ZIO[R, E, Reservation[R, E, A]]) | |
reserve.map(token => token.copy(acquire = token.acquire.map(f0))) | ||
} | ||
|
||
/** | ||
* Effectfully maps the resource acquired by this value. | ||
*/ | ||
final def mapM[R1 <: R, E1 >: E, B](f: A => ZIO[R1, E1, B]): ZManaged[R1, E1, B] = | ||
ZManaged[R1, E1, B] { | ||
reserve.map { token => | ||
token.copy(acquire = token.acquire.flatMap(f)) | ||
} | ||
} | ||
|
||
/** | ||
* Returns an effect whose failure is mapped by the specified `f` function. | ||
*/ | ||
|
@@ -682,6 +719,13 @@ object ZManaged { | |
final def fail[E](error: E): ZManaged[Any, E, Nothing] = | ||
halt(Cause.fail(error)) | ||
|
||
/** | ||
* Creates an effect that only executes the `UIO` value as its | ||
* release action. | ||
*/ | ||
final def finalizer(f: UIO[_]): ZManaged[Any, Nothing, Unit] = | ||
ZManaged.reserve(Reservation(ZIO.unit, f)) | ||
|
||
/** | ||
* Returns an effect that performs the outer effect first, followed by the | ||
* inner effect, yielding the value of the inner effect. | ||
|
@@ -883,6 +927,11 @@ object ZManaged { | |
} | ||
} | ||
|
||
/** | ||
* Returns a `ZManaged` that never acquires a resource. | ||
*/ | ||
val never: ZManaged[Any, Nothing, Nothing] = ZManaged.fromEffect(ZIO.never) | ||
|
||
/** | ||
* Reduces an `Iterable[IO]` to a single `IO`, working sequentially. | ||
*/ | ||
|
@@ -1007,7 +1056,7 @@ object ZManaged { | |
* Unwraps a `ZManaged` that is inside a `ZIO`. | ||
*/ | ||
final def unwrap[R, E, A](fa: ZIO[R, E, ZManaged[R, E, A]]): ZManaged[R, E, A] = | ||
ZManaged(fa.flatMap(_.reserve)) | ||
ZManaged.fromEffect(fa).flatten | ||
|
||
/** | ||
* The moral equivalent of `if (p) exp` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jdegoes would love another look at this operator for validation. I used
interruptibleMask
to allow the caller to run the reserve step interruptibly rather than.interruptible
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is clever and looks correct to me.
One thing to keep in mind is that this fiber will inherit the interruptibility settings of its parent, at the moment when it is forked.
Since this is done in
reserve
, I believe we can therefore simplify the code to just useinterruptible
rather thaninterruptibleMask
, because we know what the parent interruptibility status will be (right?).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we can assume that 99% of the cases would use an uninterruptible
reserve
, but, that's not always true, asreserve
can also be run manually for advanced manipulations. Which is why I thoughtinterruptibleMask
is more compositional.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, ok. Works for me. 👍