Skip to content

Commit

Permalink
Cleaning up DefaultPromise.onComplete
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorklang committed Apr 15, 2012
1 parent 48fbd60 commit 4677612
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/library/scala/concurrent/impl/Promise.scala
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -110,20 +110,14 @@ object Promise {
} }


def onComplete[U](func: Either[Throwable, T] => U): this.type = { def onComplete[U](func: Either[Throwable, T] => U): this.type = {
@tailrec //Returns null if callback was added and the result if the promise was already completed @tailrec //Tries to add the callback, if already completed, it dispatches the callback to be executed
def tryAddCallback(): Either[Throwable, T] = def dispatchOrAddCallback(): Unit =
getState match { getState match {
case r: Either[_, _] => r.asInstanceOf[Either[Throwable, T]] case r: Either[_, _] => Future.dispatchFuture(executor, () => notifyCompleted(func, r.asInstanceOf[Either[Throwable, T]]))
case listeners: List[_] => if (updateState(listeners, func :: listeners)) null else tryAddCallback() case listeners: List[_] => if (updateState(listeners, func :: listeners)) () else dispatchOrAddCallback()
} }

dispatchOrAddCallback()

this
tryAddCallback() match {
case null => this
case completed =>
Future.dispatchFuture(executor, () => notifyCompleted(func, completed))
this
}
} }


private final def notifyCompleted(func: Either[Throwable, T] => Any, result: Either[Throwable, T]) { private final def notifyCompleted(func: Either[Throwable, T] => Any, result: Either[Throwable, T]) {
Expand Down

0 comments on commit 4677612

Please sign in to comment.