Skip to content

Commit

Permalink
Let AsynchronousIo support any domains that support Lift[Unit, ?]
Browse files Browse the repository at this point in the history
  • Loading branch information
Atry committed Dec 27, 2021
1 parent ff4dff6 commit 7ea8e69
Showing 1 changed file with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,29 +147,32 @@ object AsynchronousIo {
}
}

private def completionHandler[Value](successHandler: Value => (Unit !! Throwable)) = {
new CompletionHandler[Value, Throwable => Unit] {
def failed(exc: Throwable, failureHandler: Throwable => Unit): Unit = {
failureHandler(exc)
}
given [Domain, Value](using
fenceDsl: Dsl.Searching[Fence.type, Domain, Unit],
liftUnit: Dsl.Lift[Unit, Domain]
): Dsl.Original[AsynchronousIo[Value], Domain, Value] =
Dsl.Original { (keyword, handler) =>
liftUnit(
keyword.start(
handler,
new CompletionHandler[Value, Value => Domain] {
def failed(
exception: Throwable,
handler: Value => Domain
): Unit = {
fenceDsl(Fence, (_: Unit) => throw exception)
}

def completed(result: Value, failureHandler: Throwable => Unit): Unit = {
val protectedContinuation =
try {
successHandler(result)
} catch {
case NonFatal(e) =>
val () = failureHandler(e)
return
def completed(
result: Value,
handler: Value => Domain
): Unit = {
fenceDsl(Fence, (_: Unit) => handler(result))
}
}
protectedContinuation(failureHandler)
}
}
}
)
)

given [Value]: Dsl.Original[AsynchronousIo[Value], Unit !! Throwable, Value] =
Dsl.Original { (keyword, handler) =>
keyword.start(_, completionHandler(handler))
}

}

0 comments on commit 7ea8e69

Please sign in to comment.