8000 Changed implementation TaskLocal#bind in terms of Task#bracket by leandrob13 · Pull Request #603 · monix/monix · GitHub
[go: up one dir, main page]

Skip to content

Changed implementation TaskLocal#bind in terms of Task#bracket - #603

Merged
alexandru merged 8 commits into
monix:masterfrom
leandrob13:fix/taskLocalBindWithBracket
Mar 14, 2018
Merged

alexandru merged 8 commits into
monix:masterfrom
leandrob13:fix/taskLocalBindWithBracket

Conversation

@leandrob13
Copy link
Copy Markdown
Contributor

Addresses #601

Changes the TaskLocal#bind implementation in terms of Task#bracket which handles any cleanup of resources upon success, failure or cancellation.

def bind[R](value: A)(task: Task[R]): Task[R] =
    Task.suspend {
      val saved = ref.value
      ref.update(value)
      task.bracket(r => Task.now(r))(_ => restore(saved))
    }

@codecov
codecov Bot commented Feb 26, 2018 •
Copy link
Copy Markdown

Codecov Report

Merging #603 into master will decrease coverage by 0.03%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master     #603      +/-   ##
==========================================
- Coverage   90.85%   90.81%   -0.04%     
==========================================
  Files         377      377              
  Lines        9979     9975       -4     
  Branches     1876     1882       +6     
==========================================
- Hits         9066     9059       -7     
- Misses        913      916       +3

val saved = ref.value
ref.update(value)
task.doOnFinish(_ => restore(saved))
task.bracket(r => Task.now(r))(_ => restore(saved))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If you want to handle cancellation, task has to be used inside bracket. E.g.

Task.unit.bracket(_ => task)(_ => restore(saved))

Also bindL needs to have similar change, so maybe it's easier to implement bind as bindL(Task.now(value))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@oleg-py understood!

@leandrob13
Copy link
Copy Markdown
Contributor Author

@oleg-py So I redefined bindL like this:

def bindL[R](value: Task[A])(task: Task[R]): Task[R] = {
    val saved = ref.value
    value.bracket { v =>
      ref.update(v)
      task
    }(_ => restore(saved))
  }

Also, took care of bindClear too.

task.doOnFinish(_ => restore(saved))
}
def bindL[R](value: Task[A])(task: Task[R]): Task[R] = {
val saved = ref.value
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This ref.value should be suspended as well ...

Task.eval(ref.value).flatMap { saved =>
  value.bracket {
    ref.update(v)
    task
  } { _ =>
    restore(saved)
  }
}

ref.update(value)
task.doOnFinish(_ => restore(saved))
}
Task.suspend(bindL(Task.now(value))(task))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If you suspend that value as I mentioned below, you don't need to suspend here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@alexandru Understood, will take care of it later today.

@leandrob13
Copy link
Copy Markdown
Contributor Author

@alexandru should I trigger another build?

@alexandru
Copy link
Copy Markdown
Member

Please merge with master.

@alexandru
Copy link
Copy Markdown
Member

Not 100% sure, but I think this is fine. Will merge for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0