Skip to content

Commit

Permalink
Add holdResource and holdOptionResource
Browse files Browse the repository at this point in the history
  • Loading branch information
SystemFw committed Jan 25, 2019
1 parent 45d02c9 commit 7a387c8
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions core/shared/src/main/scala/fs2/Stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,28 @@ final class Stream[+F[_], +O] private (private val free: FreeC[Algebra[Nothing,
def holdOption[F2[x] >: F[x]: Concurrent, O2 >: O]: Stream[F2, Signal[F2, Option[O2]]] =
map(Some(_): Option[O2]).hold(None)

/**
* Like [[hold]] but returns a `Resource` rather than a single element stream.
*/
def holdResource[F2[x] >: F[x], O2 >: O](initial: O2)(
implicit F: Concurrent[F2]): Resource[F2, Signal[F2, O2]] =
Stream
.eval(SignallingRef[F2, O2](initial))
.flatMap { sig =>
Stream(sig).concurrently(evalMap(sig.set))
}
.compile
.resource
.lastOrError
.widen[Signal[F2, O2]] // TODO remove when Resource becomes covariant

/**
* Like [[holdResource]] but does not require an initial value,
* and hence all output elements are wrapped in `Some`.
*/
def holdOptionResource[F2[x] >: F[x]: Concurrent, O2 >: O]: Resource[F2, Signal[F2, Option[O2]]] =
map(Some(_): Option[O2]).holdResource(None)

/**
* Determinsitically interleaves elements, starting on the left, terminating when the end of either branch is reached naturally.
*
Expand Down

0 comments on commit 7a387c8

Please sign in to comment.