Skip to content

"Cooperative" cancellation #2671

Answered by djspiewak
durban asked this question in Q&A
Dec 19, 2021 · 2 comments · 7 replies
Discussion options

You must be logged in to vote

This might be simpler :-)

def stoppable[F[_]: Async, A](task: AtomicBoolean => F[A])(implicit S: Sync[F]): F[A] =
  Async[F] async { cb =>
    Sync[F] defer {
      val flag = new AtomicBoolean(false)
      val runner = Sync[F].delay(task(flag)).attempt.flatMap(e => Sync[F].delay(cb(e)))
      runner.start.as(Some(Sync[F].delay(flag.set(true))))
    }
  }

Replies: 2 comments 7 replies

Comment options

You must be logged in to vote
5 replies
@durban
Comment options

durban Dec 19, 2021
Collaborator Author

@armanbilge
Comment options

@durban
Comment options

durban Dec 20, 2021
Collaborator Author

@armanbilge
Comment options

@durban
Comment options

durban Dec 24, 2021
Collaborator Author

Comment options

You must be logged in to vote
2 replies
@durban
Comment options

durban Dec 20, 2021
Collaborator Author

@durban
Comment options

durban Dec 28, 2021
Collaborator Author

Answer selected by durban
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants