Skip to content
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

Implement Node.js stream destroy w/ interruptWhen #3322

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Changes from 1 commit
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
Next Next commit
Refactor toReadable
armanbilge committed Oct 20, 2023
commit 603ff05781d71b00fb1eb4a44fd675af21d51437
42 changes: 21 additions & 21 deletions io/js/src/main/scala/fs2/io/ioplatform.scala
Original file line number Diff line number Diff line change
@@ -109,30 +109,30 @@ private[fs2] trait ioplatform {
/** `Pipe` that converts a stream of bytes to a stream that will emit a single `Readable`,
* that ends whenever the resulting stream terminates.
*/
def toReadable[F[_]](implicit F: Async[F]): Pipe[F, Byte, Readable] =
in =>
Stream
.resource(mkDuplex(in))
.flatMap { case (duplex, out) =>
Stream
.emit(duplex)
.merge(out.drain)
.concurrently(
Stream.eval(
F.async_[Unit](cb =>
duplex.end { e =>
cb(e.filterNot(_ == null).toLeft(()).leftMap(js.JavaScriptException))
}
)
)
)
}
.adaptError { case IOException(ex) => ex }
def toReadable[F[_]: Async]: Pipe[F, Byte, Readable] =
in => Stream.resource(toReadableResource(in))

/** Like [[toReadable]] but returns a `Resource` rather than a single element stream.
*/
def toReadableResource[F[_]: Async](s: Stream[F, Byte]): Resource[F, Readable] =
s.through(toReadable).compile.resource.lastOrError
def toReadableResource[F[_]](s: Stream[F, Byte])(implicit F: Async[F]): Resource[F, Readable] =
mkDuplex(s)
.flatMap { case (duplex, out) =>
out
.concurrently(
Stream.eval(
F.async_[Unit](cb =>
duplex.end { e =>
cb(e.filterNot(_ == null).toLeft(()).leftMap(js.JavaScriptException))
}
)
)
)
.compile
.drain
.background
.as(duplex)
}
.adaptError { case IOException(ex) => ex }

/** Writes all bytes to the specified `Writable`.
*/