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

Skip to content

Conversation

@simpadjo
Copy link
Contributor

@simpadjo simpadjo commented Jun 9, 2020

closes #3651

  1. Add ZSink.managed
  2. Utilize it for ZSink.fromOutputStreamManaged
  3. Also refactor ZSink.fromFile
  4. Reify ZSink.foreachChunk signature

@regis-leray sorry to steal this ticket from you, I need this at work :)
@lvitaly - pinging you as the author of ZSink#fromFile

@simpadjo simpadjo requested a review from iravid as a code owner June 9, 2020 22:00
} yield push
}

def managed[R, E, I, A, L, Z](resource: ZManaged[R, E, A])(fn: A => ZSink[R, E, I, L, Z]): ZSink[R, E, I, L, Z] =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this signature is much simpler 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I started with the original signature from the ticket, then tried to use it in fromFile and it immediately folded. Nested resources are too useful.

}
)
}
writer &> ZSink.count
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice @simpadjo

*/
def foreachChunk[R, E, I](f: Chunk[I] => ZIO[R, E, Any]): ZSink[R, E, I, I, Unit] =
ZSink.fromPush[R, E, I, I, Unit] {
def foreachChunk[R, E, I](f: Chunk[I] => ZIO[R, E, Any]): ZSink[R, E, I, Nothing, Unit] =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

* `resource` will be finalized after the processing.
*/
def managed[R, E, I, A, L, Z](resource: ZManaged[R, E, A])(fn: A => ZSink[R, E, I, L, Z]): ZSink[R, E, I, L, Z] =
ZSink(resource.fold[ZSink[R, E, I, L, Z]](err => ZSink.fail[E, I](err).dropLeftover, m => fn(m)).flatMap(_.push))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are the leftovers dropped in the error case?

Copy link
Contributor Author

@simpadjo simpadjo Jun 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ZSink.fail returns leftovers of type I and we require L.
I don't know how to properly treat allocation errors w/o losing too much of ergonomics.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively we could require ZManaged[R, Nothing, A] but I expect most of the resources to be IO-related.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s require ZSink[R, E, I, I, Z] here. Dropping the leftovers is a bad choice.

)
.position(position)
)
.orDie
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I seem to have missed this orDie here. Why is it required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't (I removed it).
Actually, we could reify Throwable to IOException, but I'm not sure how it would work in case of interruption.

@iravid iravid added the stream ZIO Stream label Jun 13, 2020
iravid
iravid previously approved these changes Jun 16, 2020
@adamgfraser
Copy link
Contributor

@simpadjo Can you fix the compilation errors on Dotty?

@iravid iravid merged commit e7cdbef into zio:master Jun 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stream ZIO Stream

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add ZSink.managed

4 participants