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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observable.bracket, Iterant/Task API refactorings, fixes #729

Merged
merged 45 commits into from Oct 6, 2018

Conversation

Projects
None yet
2 participants
@alexandru
Member

alexandru commented Sep 29, 2018

Another mega PR 馃槓

Naming and signature changes:

  • Observable: changed convention for naming such that methods suffixed with F now have to mean that F[_] parameters are used:
    • foldLeftF was renamed to foldLeft
    • minF, maxF, sumF, were renamed to min, max, sum, etc.
    • mapTask was renamed to mapEval
    • scanTask was renamed to scanEval
    • mapEval was renamed to mapEvalF
    • scanEval was renamed scanEvalF
  • Observable: all doOn* methods for installing handlers are have been changed to use Task in their signature, instead of side effectful functions
    • doOnError, doOnComplete, doOnEarlyStop, etc... now all use Task
    • doOnErrorTask, doOnCompleteTask, etc. the Task suffixed methods are deprecated
    • doOnErrorEval, doOnCompleteEval have been renamed to the new convention, so doOnErrorF, doOnCompleteF, etc.

Added bracket, bracketCase to Observable, so we now have these methods:

abstract class Observable[+A] {
  //...
  def bracket[B](use: A => Observable[B])(release: A => Task[Unit]): Observable[B]

  def bracketF[F[_], B](use: A => Observable[B])(release: A => F[Unit])
      (implicit F: TaskLike[F]): Observable[B]


  def bracketCase[B](use: A => Observable[B])(release: (A, ExitCase[Throwable]) => Task[Unit]): Observable[B]

  def bracketCaseF[F[_], B](use: A => Observable[B])(release: (A, ExitCase[Throwable]) => F[Unit])
      (implicit F: TaskLike[F]): Observable[B]

  def guarantee(f: Task[Unit]): Observable[A]

  def guaranteeF[F[_]](f: F[Unit])(implicit F: TaskLike[F]): Observable[A]

  def guaranteeCase(f: ExitCase[Throwable] => Task[Unit]): Observable[A]

  def guaranteeCaseF[F[_]](f: ExitCase[Throwable] => F[Unit])
    (implicit F: TaskLike[F]): Observable[A]

The implementation is lawful, Observable now implementing cats.effect.Bracket.

I also added Observable.resource (functions on the companion object):

def resource[A](acquire: Task[A])(release: A => Task[Unit]): Observable[A]

def resourceF[F[_], A](acquire: F[A])(release: A => F[Unit])
  (implicit F: TaskLike[F]): Observable[A]

These builders have changed to make them safer, now being expressed in terms of Observable.fromResource:

def fromIterator[A](task: Task[Iterator[A]]): Observable[A]

def fromIterator[A](resource: Resource[Task, Iterator[A]]): Observable[A]

def fromIteratorUnsafe[A](iterator: Iterator[A]): Observable[A]

def fromInputStream(in: Task[InputStream], chunkSize: Int = 4096): Observable[Array[Byte]]

def fromInputStreamF[F[_]](in: F[InputStream], chunkSize: Int = 4096)
    (implicit F: TaskLike[F]): Observable[Array[Byte]]

def fromInputStreamUnsafe(in: InputStream, chunkSize: Int = 4096): Observable[Array[Byte]]

def fromCharsReader(in: Task[Reader], chunkSize: Int = 4096): Observable[Array[Char]]

def fromCharsReaderF[F[_]](in: F[Reader], chunkSize: Int = 4096)
    (implicit F: TaskLike[F]): Observable[Array[Char]]

def fromCharsReaderUnsafe(in: Reader, chunkSize: Int = 4096): Observable[Array[Char]]

def fromLinesReader(in: Task[BufferedReader]): Observable[String]

def fromLinesReaderF[F[_]](in: F[BufferedReader])(implicit F: TaskLike[F]): Observable[String]

def fromLinesReaderUnsafe(in: BufferedReader): Observable[String]

Note that all of these builders had a faulty implementation after Task was changed to the auto-cancelable model by default. And now they are fixed.

We can also convert from a cats.effect.Resource directly and the conversion does the right thing, meaning that an Observable can be used instead of a Resource in all cases:

def fromResource[F[_], A](resource: Resource[F, A])(implicit F: TaskLike[F]): Observable[A]

And we can build resources via Observable directly:

def resource[A](acquire: Task[A])(release: A => Task[Unit]): Observable[A]

def resourceF[F[_], A](acquire: F[A])(release: A => F[Unit])(implicit F: TaskLike[F]): Observable[A]

def resourceCase[A](acquire: Task[A])(release: (A, ExitCase[Throwable]) => Task[Unit]): Observable[A]

def resourceCaseF[F[_], A](acquire: F[A])(release: (A, ExitCase[Throwable]) => F[Unit])
    (implicit F: TaskLike[F]): Observable[A]

As mentioned, Observable now implements cats.effect.Bracket.

Other changes:

  • add conversion from Function0 in CoevalLike, TaskLike and ObservableLike
  • update Cats to 1.4.0
  • update Scala to 2.12.7
  • get rid of the TaskBinCompat super trait, expressing deprecated Task methods as extension methods
  • fix Task#runAsyncUncancelableOpt (introduced broken in previous PR)
  • added @UnsafeBecauseImpure and UnsafeProtocol on Observable methods

Avasil and others added some commits May 1, 2018

Merge branch 'master' into Observable.bracket
# Conflicts:
#	monix-execution/shared/src/main/scala/monix/execution/BracketResult.scala
#	monix-tail/shared/src/main/scala/monix/tail/internal/IterantBracket.scala
#	monix-tail/shared/src/test/scala/monix/tail/IterantBracketSuite.scala
Merge branch 'master' into Observable.bracket
# Conflicts:
#	monix-reactive/shared/src/main/scala/monix/reactive/Observable.scala
#	monix-tail/shared/src/main/scala/monix/tail/Iterant.scala

@alexandru alexandru referenced this pull request Sep 29, 2018

Closed

WIP: Observable.bracket #667

alexandru added some commits Sep 30, 2018

@alexandru alexandru changed the title from WIP: Observable API refactorings, bracket, fixes to Observable/Iterant/Task API refactorings, Observable.bracket, fixes Oct 1, 2018

@alexandru alexandru changed the title from Observable/Iterant/Task API refactorings, Observable.bracket, fixes to Observable.bracket, Iterant/Task API refactorings, fixes Oct 1, 2018

@alexandru alexandru added this to the 3.0.0-RC2 milestone Oct 1, 2018

alexandru added some commits Oct 1, 2018

@codecov

This comment has been minimized.

codecov bot commented Oct 2, 2018

Codecov Report

Merging #729 into master will decrease coverage by 0.38%.
The diff coverage is 70.25%.

@@            Coverage Diff             @@
##           master     #729      +/-   ##
==========================================
- Coverage   90.84%   90.46%   -0.39%     
==========================================
  Files         398      396       -2     
  Lines       11133    11146      +13     
  Branches     2074     2064      -10     
==========================================
- Hits        10114    10083      -31     
- Misses       1019     1063      +44

alexandru added some commits Oct 5, 2018

@alexandru alexandru merged commit 435d32c into monix:master Oct 6, 2018

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment