Skip to content

Commit

Permalink
Drop handleCompletion from CompletionGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
m8nmueller committed Dec 10, 2023
1 parent 59c518f commit e7a94a8
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 18 deletions.
9 changes: 1 addition & 8 deletions shared/src/main/scala/async/Async.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,7 @@ object Async:
inline def current(using async: Async): Async = async

def group[T](body: Async ?=> T)(using async: Async): T =
withNewCompletionGroup(CompletionGroup(async.group.handleCompletion).link())(body)

def withCompletionHandler[T](handler: Cancellable => Async ?=> Unit)(body: Async ?=> T)(using async: Async): T =
val combined = (c: Cancellable) =>
(async: Async) ?=>
handler(c)
async.group.handleCompletion(c)
withNewCompletionGroup(CompletionGroup(combined).link())(body)
withNewCompletionGroup(CompletionGroup().link())(body)

/** Runs a body within another completion group. When the body returns, the group is cancelled and its completion
* awaited with the `Unlinked` group.
Expand Down
6 changes: 0 additions & 6 deletions shared/src/main/scala/async/Cancellable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ trait Cancellable:
def unlink(): this.type =
link(CompletionGroup.Unlinked)

/** Signal completion of this cancellable to its group. */
def signalCompletion()(using Async): this.type =
this.group.handleCompletion(this)
this.unlink()
this

end Cancellable

object Cancellable:
Expand Down
6 changes: 2 additions & 4 deletions shared/src/main/scala/async/CompletionGroup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import scala.util.Success

/** A group of cancellable objects that are completed together. Cancelling the group means cancelling all its
* uncompleted members.
* @param handleCompletion
* a function that gets applied to every member when it is completed or cancelled
*/
class CompletionGroup(val handleCompletion: Cancellable => Async ?=> Unit = _ => {}) extends Cancellable.Tracking:
class CompletionGroup extends Cancellable.Tracking:
private val members: mutable.Set[Cancellable] = mutable.Set()
private var canceled: Boolean = false
private var cancelWait: Option[Promise[Unit]] = None
Expand All @@ -26,7 +24,7 @@ class CompletionGroup(val handleCompletion: Cancellable => Async ?=> Unit = _ =>
synchronized:
if members.nonEmpty && cancelWait.isEmpty then cancelWait = Some(Promise())
cancelWait.foreach(cWait => cWait.future.await)
signalCompletion()
unlink()

/** Add given member to the members set. If the group has already been cancelled, cancels that member immediately. */
def add(member: Cancellable): Unit =
Expand Down

0 comments on commit e7a94a8

Please sign in to comment.